gorm/utils.go

103 lines
1.8 KiB
Go
Raw Normal View History

2013-10-25 14:04:48 +04:00
package gorm
2013-10-26 05:49:40 +04:00
import (
"bytes"
"errors"
"fmt"
2013-11-14 14:59:11 +04:00
"reflect"
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]
}
func FieldValueByName(name string, value interface{}) (i interface{}, err error) {
2014-07-15 07:48:30 +04:00
data := reflect.Indirect(reflect.ValueOf(value))
name = SnakeToUpperCamel(name)
2014-07-15 07:48:30 +04:00
if data.Kind() == reflect.Struct {
if field := data.FieldByName(name); field.IsValid() {
i = field.Interface()
2014-07-15 07:48:30 +04:00
} else {
2015-02-17 03:34:01 +03:00
return nil, fmt.Errorf("struct has no field with name %s", name)
2014-07-15 07:48:30 +04:00
}
} else {
return nil, errors.New("value must be of kind struct")
2014-07-15 07:48:30 +04:00
}
return
2014-07-15 07:48:30 +04:00
}
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
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
}
buf := bytes.NewBufferString("")
2013-11-17 04:28:30 +04:00
for i, v := range u {
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())
2014-09-02 15:03:01 +04:00
smap.Set(u, s)
2013-11-17 04:28:30 +04:00
return s
}
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
}
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()
2014-09-02 15:03:01 +04:00
umap.Set(s, u)
2013-11-17 04:28:30 +04:00
return u
}
func parseTagSetting(str string) map[string]string {
tags := strings.Split(str, ";")
setting := map[string]string{}
for _, value := range tags {
v := strings.Split(value, ":")
k := strings.TrimSpace(strings.ToUpper(v[0]))
if len(v) == 2 {
setting[k] = v[1]
} else {
setting[k] = k
}
}
return setting
}