50 lines
809 B
Go
50 lines
809 B
Go
|
package sqlfs
|
||
|
|
||
|
import (
|
||
|
"reflect"
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
|
var dbTag = "db"
|
||
|
|
||
|
// RawFieldNames converts golang struct field into slice string.
|
||
|
func RawFieldNames[T any]() []string {
|
||
|
|
||
|
var out []string
|
||
|
|
||
|
var a T
|
||
|
v := reflect.ValueOf(a)
|
||
|
typ := v.Type()
|
||
|
|
||
|
for i := 0; i < v.NumField(); i++ {
|
||
|
// gets us a StructField
|
||
|
fi := typ.Field(i)
|
||
|
tagv := fi.Tag.Get(dbTag)
|
||
|
switch tagv {
|
||
|
case "-":
|
||
|
continue
|
||
|
default:
|
||
|
// get tag name with the tag opton, e.g.:
|
||
|
// `db:"id"`
|
||
|
// `db:"id,type=char,length=16"`
|
||
|
// `db:",type=char,length=16"`
|
||
|
// `db:"-,type=char,length=16"`
|
||
|
if strings.Contains(tagv, ",") {
|
||
|
tagv = strings.TrimSpace(strings.Split(tagv, ",")[0])
|
||
|
}
|
||
|
|
||
|
if tagv == "-" {
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
if len(tagv) == 0 {
|
||
|
tagv = fi.Name
|
||
|
}
|
||
|
|
||
|
out = append(out, tagv)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return out
|
||
|
}
|