FILE / Unmei/Backend
v1/utils.go
Исходный файл и его история в репозитории.
66 lines
1.3 KiB
Go
66 lines
1.3 KiB
Go
package v1
|
|
|
|
import (
|
|
"reflect"
|
|
"strings"
|
|
)
|
|
|
|
func StringOrDefault(a, b string) string {
|
|
if len(a) == 0 {
|
|
return b
|
|
}
|
|
return a
|
|
}
|
|
|
|
func GetStructForDatabase(s interface{}) []string {
|
|
out := make([]string, 0)
|
|
el := reflect.ValueOf(s).Elem()
|
|
for i := 0; i < el.NumField(); i++ {
|
|
t := el.Type().Field(i)
|
|
tag := t.Tag
|
|
db := tag.Get("db")
|
|
if len(db) == 0 {
|
|
out = append(out, strings.ToLower(t.Name))
|
|
} else {
|
|
out = append(out, db)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
func GetNamedForDatabase(s interface{}) []string {
|
|
out := make([]string, 0)
|
|
el := reflect.ValueOf(s).Elem()
|
|
for i := 0; i < el.NumField(); i++ {
|
|
t := el.Type().Field(i)
|
|
tag := t.Tag
|
|
db := tag.Get("db")
|
|
if len(db) == 0 {
|
|
out = append(out, ":"+strings.ToLower(t.Name))
|
|
} else {
|
|
out = append(out, ":"+db)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
func GetStructAndNamedForDatabase(s interface{}) [][]string {
|
|
out := make([][]string, 0)
|
|
el := reflect.ValueOf(s).Elem()
|
|
for i := 0; i < el.NumField(); i++ {
|
|
t := el.Type().Field(i)
|
|
tag := t.Tag
|
|
ignore := tag.Get("ignore")
|
|
if ignore != "1" {
|
|
db := tag.Get("db")
|
|
if len(db) == 0 {
|
|
out = append(out, []string{strings.ToLower(t.Name), ":" + strings.ToLower(t.Name)})
|
|
} else {
|
|
out = append(out, []string{db, ":" + db})
|
|
}
|
|
}
|
|
}
|
|
return out
|
|
|
|
}
|