1 Star 0 Fork 15

wenxuefeng/gosql

forked from fifsky/gosql 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
util.go 3.12 KB
一键复制 编辑 原始数据 按行查看 历史
fifsky 提交于 2020-01-02 17:22 . fix auto create time cannot be compared
package gosql
import (
"reflect"
"sort"
"time"
)
//inSlice
func inSlice(k string, s []string) bool {
for _, v := range s {
if k == v {
return true
}
}
return false
}
//IsZero assert value is zero value
func IsZero(val reflect.Value) bool {
if !val.IsValid() {
return true
}
kind := val.Kind()
switch kind {
case reflect.String:
return val.Len() == 0
case reflect.Bool:
return val.Bool() == false
case reflect.Float32, reflect.Float64:
return val.Float() == 0
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return val.Int() == 0
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return val.Uint() == 0
case reflect.Ptr, reflect.Chan, reflect.Func, reflect.Interface, reflect.Slice, reflect.Map:
return val.IsNil()
case reflect.Array:
for i := 0; i < val.Len(); i++ {
if !IsZero(val.Index(i)) {
return false
}
}
return true
case reflect.Struct:
if t, ok := val.Interface().(time.Time); ok {
return t.IsZero()
} else {
valid := val.FieldByName("Valid")
if valid.IsValid() {
va, ok := valid.Interface().(bool)
return ok && !va
}
return reflect.DeepEqual(val.Interface(), reflect.Zero(val.Type()).Interface())
}
default:
return reflect.DeepEqual(val.Interface(), reflect.Zero(val.Type()).Interface())
}
}
//zeroValueFilter filter zero value and keep the specified zero value
func zeroValueFilter(fields map[string]reflect.Value, zv []string) map[string]interface{} {
m := make(map[string]interface{})
for k, v := range fields {
v = reflect.Indirect(v)
if inSlice(k, zv) || !IsZero(v) {
m[k] = v.Interface()
}
}
return m
}
// structAutoTime auto set created_at updated_at
func structAutoTime(fields map[string]reflect.Value, f []string) {
for k, v := range fields {
v = reflect.Indirect(v)
if v.IsValid() && inSlice(k, f) && IsZero(v) {
switch v.Kind() {
case reflect.String:
v.SetString(time.Now().Format("2006-01-02 15:04:05"))
case reflect.Struct:
// truncate 1 sec, Otherwise the data you create and the data you get will never be compared
v.Set(reflect.ValueOf(time.Now().Truncate(1 * time.Second)))
}
}
}
}
// structToMap
func structToMap(fields map[string]reflect.Value) map[string]interface{} {
m := make(map[string]interface{})
for k, v := range fields {
v = reflect.Indirect(v)
m[k] = v.Interface()
}
return m
}
// fillPrimaryKey is created fill primary key
func fillPrimaryKey(v reflect.Value, value int64) {
v = reflect.Indirect(v)
if v.IsValid() {
switch v.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
v.SetInt(value)
}
}
}
// sortedParamKeys Sorts the param names given - map iteration order is explicitly random in Go
// but we need params in a defined order to avoid unexpected results.
func sortedParamKeys(params map[string]interface{}) []string {
sortedKeys := make([]string, len(params))
i := 0
for k := range params {
sortedKeys[i] = k
i++
}
sort.Strings(sortedKeys)
return sortedKeys
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/wen_xue_feng/gosql.git
git@gitee.com:wen_xue_feng/gosql.git
wen_xue_feng
gosql
gosql
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385