initial commit on github
This commit is contained in:
+65
@@ -0,0 +1,65 @@
|
||||
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
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user