2013-10-25 14:04:48 +04:00
|
|
|
package gorm
|
2013-10-26 05:49:40 +04:00
|
|
|
|
|
|
|
import (
|
2013-10-26 10:23:02 +04:00
|
|
|
"bytes"
|
2013-11-14 14:59:11 +04:00
|
|
|
"database/sql"
|
2014-01-26 08:41:37 +04:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2013-11-14 14:59:11 +04:00
|
|
|
"reflect"
|
2014-01-26 08:41:37 +04:00
|
|
|
"regexp"
|
|
|
|
"runtime"
|
2013-10-26 05:49:40 +04:00
|
|
|
"strings"
|
2013-12-12 16:57:10 +04:00
|
|
|
"sync"
|
2013-10-26 05:49:40 +04:00
|
|
|
)
|
|
|
|
|
2013-12-15 06:09:19 +04:00
|
|
|
type safeMap struct {
|
|
|
|
m map[string]string
|
|
|
|
l *sync.RWMutex
|
2013-12-12 16:57:10 +04:00
|
|
|
}
|
2013-12-15 06:09:19 +04:00
|
|
|
|
|
|
|
func (s *safeMap) Set(key string, value string) {
|
|
|
|
s.l.Lock()
|
|
|
|
defer s.l.Unlock()
|
|
|
|
s.m[key] = value
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *safeMap) Get(key string) string {
|
|
|
|
s.l.RLock()
|
|
|
|
defer s.l.RUnlock()
|
|
|
|
return s.m[key]
|
|
|
|
}
|
|
|
|
|
2014-07-15 07:48:30 +04:00
|
|
|
func FieldByName(name string, value interface{}, withAddr ...bool) (interface{}, bool) {
|
|
|
|
data := reflect.Indirect(reflect.ValueOf(value))
|
2014-07-21 10:06:54 +04:00
|
|
|
name = SnakeToUpperCamel(name)
|
2014-07-15 07:48:30 +04:00
|
|
|
|
|
|
|
if data.Kind() == reflect.Struct {
|
|
|
|
if field := data.FieldByName(name); field.IsValid() {
|
|
|
|
if len(withAddr) > 0 && field.CanAddr() {
|
|
|
|
return field.Addr().Interface(), true
|
|
|
|
} else {
|
|
|
|
return field.Interface(), true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if data.Kind() == reflect.Slice {
|
|
|
|
elem := data.Type().Elem()
|
|
|
|
if elem.Kind() == reflect.Ptr {
|
|
|
|
return nil, reflect.New(data.Type().Elem().Elem()).Elem().FieldByName(name).IsValid()
|
|
|
|
} else {
|
|
|
|
return nil, reflect.New(data.Type().Elem()).Elem().FieldByName(name).IsValid()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2013-12-15 06:09:19 +04:00
|
|
|
func newSafeMap() *safeMap {
|
|
|
|
return &safeMap{l: new(sync.RWMutex), m: make(map[string]string)}
|
|
|
|
}
|
|
|
|
|
|
|
|
var smap = newSafeMap()
|
|
|
|
var umap = newSafeMap()
|
2013-11-17 04:28:30 +04:00
|
|
|
|
2014-07-21 10:06:54 +04:00
|
|
|
func ToSnake(u string) string {
|
2013-12-15 06:09:19 +04:00
|
|
|
if v := smap.Get(u); v != "" {
|
2013-11-17 04:28:30 +04:00
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2013-10-26 10:23:02 +04:00
|
|
|
buf := bytes.NewBufferString("")
|
2013-11-17 04:28:30 +04:00
|
|
|
for i, v := range u {
|
2013-10-26 10:23:02 +04:00
|
|
|
if i > 0 && v >= 'A' && v <= 'Z' {
|
|
|
|
buf.WriteRune('_')
|
|
|
|
}
|
|
|
|
buf.WriteRune(v)
|
|
|
|
}
|
2013-11-17 04:28:30 +04:00
|
|
|
|
|
|
|
s := strings.ToLower(buf.String())
|
2013-12-15 06:09:19 +04:00
|
|
|
go smap.Set(u, s)
|
2013-11-17 04:28:30 +04:00
|
|
|
return s
|
2013-10-26 10:23:02 +04:00
|
|
|
}
|
|
|
|
|
2014-07-21 10:06:54 +04:00
|
|
|
func SnakeToUpperCamel(s string) string {
|
2013-12-15 06:09:19 +04:00
|
|
|
if v := umap.Get(s); v != "" {
|
2013-11-17 04:28:30 +04:00
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2013-10-26 10:40:37 +04:00
|
|
|
buf := bytes.NewBufferString("")
|
|
|
|
for _, v := range strings.Split(s, "_") {
|
|
|
|
if len(v) > 0 {
|
|
|
|
buf.WriteString(strings.ToUpper(v[:1]))
|
|
|
|
buf.WriteString(v[1:])
|
|
|
|
}
|
|
|
|
}
|
2013-11-17 04:28:30 +04:00
|
|
|
|
|
|
|
u := buf.String()
|
2013-12-15 06:09:19 +04:00
|
|
|
go umap.Set(s, u)
|
2013-11-17 04:28:30 +04:00
|
|
|
return u
|
2013-10-26 10:40:37 +04:00
|
|
|
}
|
|
|
|
|
2013-10-31 08:59:04 +04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-01-26 08:41:37 +04:00
|
|
|
func fileWithLineNum() string {
|
|
|
|
for i := 1; i < 15; i++ {
|
|
|
|
_, file, line, ok := runtime.Caller(i)
|
|
|
|
if ok && (!regexp.MustCompile(`jinzhu/gorm/.*.go`).MatchString(file) || regexp.MustCompile(`jinzhu/gorm/.*test.go`).MatchString(file)) {
|
|
|
|
return fmt.Sprintf("%v:%v", strings.TrimPrefix(file, os.Getenv("GOPATH")+"src/"), line)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2014-07-22 09:32:22 +04:00
|
|
|
func setFieldValue(field reflect.Value, value interface{}) (result bool) {
|
|
|
|
result = false
|
2013-11-14 14:59:11 +04:00
|
|
|
if field.IsValid() && field.CanAddr() {
|
2014-07-22 09:32:22 +04:00
|
|
|
result = true
|
|
|
|
if scanner, ok := field.Addr().Interface().(sql.Scanner); ok {
|
|
|
|
scanner.Scan(value)
|
|
|
|
} else if reflect.TypeOf(value).ConvertibleTo(field.Type()) {
|
|
|
|
field.Set(reflect.ValueOf(value).Convert(field.Type()))
|
|
|
|
} else {
|
|
|
|
result = false
|
2013-11-14 14:59:11 +04:00
|
|
|
}
|
|
|
|
}
|
2014-07-22 09:32:22 +04:00
|
|
|
return
|
2013-11-14 14:59:11 +04:00
|
|
|
}
|
2013-11-15 07:36:27 +04:00
|
|
|
|
|
|
|
func isBlank(value reflect.Value) bool {
|
2013-12-04 09:07:28 +04:00
|
|
|
return reflect.DeepEqual(value.Interface(), reflect.Zero(value.Type()).Interface())
|
2013-11-15 07:36:27 +04:00
|
|
|
}
|
2014-01-27 18:36:08 +04:00
|
|
|
|
|
|
|
func convertInterfaceToMap(values interface{}) map[string]interface{} {
|
|
|
|
attrs := map[string]interface{}{}
|
|
|
|
|
|
|
|
switch value := values.(type) {
|
|
|
|
case map[string]interface{}:
|
|
|
|
for k, v := range value {
|
2014-07-21 10:06:54 +04:00
|
|
|
attrs[ToSnake(k)] = v
|
2014-01-27 18:36:08 +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() {
|
2014-07-21 10:06:54 +04:00
|
|
|
attrs[ToSnake(key.Interface().(string))] = reflectValue.MapIndex(key).Interface()
|
2014-01-27 18:36:08 +04:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
scope := Scope{Value: values}
|
|
|
|
for _, field := range scope.Fields() {
|
|
|
|
if !field.IsBlank {
|
|
|
|
attrs[field.DBName] = field.Value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return attrs
|
|
|
|
}
|