48 lines
1.2 KiB
Go
48 lines
1.2 KiB
Go
package model
|
|
|
|
import "reflect"
|
|
|
|
// ToSearchableMap convert attrs to searchable map
|
|
func ToSearchableMap(attrs ...interface{}) (result interface{}) {
|
|
if len(attrs) > 1 {
|
|
if str, ok := attrs[0].(string); ok {
|
|
result = map[string]interface{}{str: attrs[1]}
|
|
}
|
|
} else if len(attrs) == 1 {
|
|
if attr, ok := attrs[0].(map[string]interface{}); ok {
|
|
result = attr
|
|
}
|
|
|
|
if attr, ok := attrs[0].(interface{}); ok {
|
|
result = attr
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
func indirect(reflectValue reflect.Value) reflect.Value {
|
|
for reflectValue.Kind() == reflect.Ptr {
|
|
reflectValue = reflectValue.Elem()
|
|
}
|
|
return reflectValue
|
|
}
|
|
|
|
func isBlank(value reflect.Value) bool {
|
|
switch value.Kind() {
|
|
case reflect.String:
|
|
return value.Len() == 0
|
|
case reflect.Bool:
|
|
return !value.Bool()
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
return value.Int() == 0
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
|
return value.Uint() == 0
|
|
case reflect.Float32, reflect.Float64:
|
|
return value.Float() == 0
|
|
case reflect.Interface, reflect.Ptr:
|
|
return value.IsNil()
|
|
}
|
|
|
|
return reflect.DeepEqual(value.Interface(), reflect.Zero(value.Type()).Interface())
|
|
}
|