gorm/schema/utils.go

59 lines
1.1 KiB
Go
Raw Normal View History

2020-01-31 07:22:37 +03:00
package schema
import (
"reflect"
2020-02-01 13:02:19 +03:00
"regexp"
2020-01-31 07:22:37 +03:00
"strings"
)
2020-02-21 19:02:05 +03:00
func ParseTagSetting(str string, sep string) map[string]string {
settings := map[string]string{}
names := strings.Split(str, sep)
for i := 0; i < len(names); i++ {
j := i
if len(names[j]) > 0 {
for {
if names[j][len(names[j])-1] == '\\' {
i++
names[j] = names[j][0:len(names[j])-1] + sep + names[i]
names[i] = ""
} else {
break
}
2020-01-31 07:22:37 +03:00
}
}
2020-02-21 19:02:05 +03:00
values := strings.Split(names[j], ":")
k := strings.TrimSpace(strings.ToUpper(values[0]))
if len(values) >= 2 {
settings[k] = strings.Join(values[1:], ":")
} else if k != "" {
settings[k] = k
}
2020-01-31 07:22:37 +03:00
}
2020-02-21 19:02:05 +03:00
return settings
2020-01-31 07:22:37 +03:00
}
func checkTruth(val string) bool {
if strings.ToLower(val) == "false" {
return false
}
return true
}
func toColumns(val string) (results []string) {
if val != "" {
for _, v := range strings.Split(val, ",") {
results = append(results, strings.TrimSpace(v))
}
}
return
}
2020-02-01 13:02:19 +03:00
func removeSettingFromTag(tag reflect.StructTag, name string) reflect.StructTag {
return reflect.StructTag(regexp.MustCompile(`(?i)(gorm:.*?)(`+name+`:.*?)(;|("))`).ReplaceAllString(string(tag), "${1}${4}"))
}