2014-07-25 12:51:54 +04:00
|
|
|
package gorm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"regexp"
|
|
|
|
"runtime"
|
2015-08-16 10:35:58 +03:00
|
|
|
"unicode"
|
|
|
|
"unicode/utf8"
|
2014-07-25 12:51:54 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
func fileWithLineNum() string {
|
2014-09-02 15:03:01 +04:00
|
|
|
for i := 2; i < 15; i++ {
|
2014-07-25 12:51:54 +04:00
|
|
|
_, file, line, ok := runtime.Caller(i)
|
|
|
|
if ok && (!regexp.MustCompile(`jinzhu/gorm/.*.go`).MatchString(file) || regexp.MustCompile(`jinzhu/gorm/.*test.go`).MatchString(file)) {
|
2015-02-17 17:55:14 +03:00
|
|
|
return fmt.Sprintf("%v:%v", file, line)
|
2014-07-25 12:51:54 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func isBlank(value reflect.Value) bool {
|
|
|
|
return reflect.DeepEqual(value.Interface(), reflect.Zero(value.Type()).Interface())
|
|
|
|
}
|
|
|
|
|
|
|
|
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 convertInterfaceToMap(values interface{}) map[string]interface{} {
|
|
|
|
attrs := map[string]interface{}{}
|
|
|
|
|
|
|
|
switch value := values.(type) {
|
|
|
|
case map[string]interface{}:
|
|
|
|
for k, v := range value {
|
2015-02-18 05:19:34 +03:00
|
|
|
attrs[ToDBName(k)] = v
|
2014-07-25 12:51:54 +04:00
|
|
|
}
|
|
|
|
case []interface{}:
|
|
|
|
for _, v := range value {
|
|
|
|
for key, value := range convertInterfaceToMap(v) {
|
|
|
|
attrs[key] = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case interface{}:
|
|
|
|
reflectValue := reflect.ValueOf(values)
|
|
|
|
|
|
|
|
switch reflectValue.Kind() {
|
|
|
|
case reflect.Map:
|
|
|
|
for _, key := range reflectValue.MapKeys() {
|
2015-02-18 05:19:34 +03:00
|
|
|
attrs[ToDBName(key.Interface().(string))] = reflectValue.MapIndex(key).Interface()
|
2014-07-25 12:51:54 +04:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
scope := Scope{Value: values}
|
|
|
|
for _, field := range scope.Fields() {
|
2015-01-14 01:57:47 +03:00
|
|
|
if !field.IsBlank && !field.IsIgnored {
|
2014-09-02 15:03:01 +04:00
|
|
|
attrs[field.DBName] = field.Field.Interface()
|
2014-07-25 12:51:54 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return attrs
|
|
|
|
}
|
2015-08-16 10:35:58 +03:00
|
|
|
|
|
|
|
func toString(a interface{}) string {
|
|
|
|
return fmt.Sprintf("%v", a)
|
|
|
|
}
|
|
|
|
|
|
|
|
func strInSlice(a string, list []string) bool {
|
|
|
|
for _, b := range list {
|
|
|
|
if b == a {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func upFL(s string) string {
|
|
|
|
if s == "" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
r, n := utf8.DecodeRuneInString(s)
|
|
|
|
return string(unicode.ToUpper(r)) + s[n:]
|
|
|
|
}
|