You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
872 B

1 year ago
package builder
import (
"fmt"
"git.diulo.com/mogfee/kit/stringx"
"reflect"
"strings"
)
const dbTag = "db"
func RawFieldNames(in any) []string {
out := make([]string, 0)
v := reflect.ValueOf(in)
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
if v.Kind() != reflect.Struct {
panic(fmt.Errorf("ToMap only accepts structs; got %T", v))
}
typ := v.Type()
for i := 0; i < v.NumField(); i++ {
fi := typ.Field(i)
if !stringx.IsFirstUpper(fi.Name) {
continue
}
tagv := fi.Tag.Get(dbTag)
switch tagv {
case "-":
continue
case "":
out = append(out, fmt.Sprintf("`%s`", fi.Name))
default:
if strings.Contains(tagv, ",") {
tagv = strings.TrimSpace(strings.Split(tagv, ",")[0])
}
if tagv == "-" {
continue
}
if len(tagv) == 0 {
tagv = fi.Name
}
out = append(out, fmt.Sprintf("`%s`", tagv))
}
}
return out
}