gorm/utils.go

42 lines
687 B
Go

package gorm
import (
"bytes"
"strings"
)
var smap = map[string]string{}
func ToDBColumnName(u string) string {
if v, ok := smap[u]; ok {
return v
}
buf := bytes.NewBufferString("")
for i, v := range u {
if i > 0 && v >= 'A' && v <= 'Z' {
buf.WriteRune('_')
}
buf.WriteRune(v)
}
s := strings.ToLower(buf.String())
smap[u] = s
return s
}
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
}