fix reflect insert (remove gid field)

This commit is contained in:
c9s 2022-06-08 12:03:58 +08:00
parent cca813e5e5
commit e7dfd4a654
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 26 additions and 1 deletions

View File

@ -40,6 +40,10 @@ func placeholdersOf(record interface{}) []string {
for i := 0; i < rt.NumField(); i++ {
fieldType := rt.Field(i)
if tag, ok := fieldType.Tag.Lookup("db"); ok {
if tag == "gid" {
continue
}
dbFields = append(dbFields, ":"+tag)
}
}
@ -61,6 +65,10 @@ func fieldsNamesOf(record interface{}) []string {
for i := 0; i < rt.NumField(); i++ {
fieldType := rt.Field(i)
if tag, ok := fieldType.Tag.Lookup("db"); ok {
if tag == "gid" {
continue
}
dbFields = append(dbFields, tag)
}
}
@ -68,6 +76,23 @@ func fieldsNamesOf(record interface{}) []string {
return dbFields
}
func ParseStructTag(s string) (string, map[string]string) {
opts := make(map[string]string)
ss := strings.Split(s, ",")
if len(ss) > 1 {
for _, opt := range ss[1:] {
aa := strings.SplitN(opt, "=", 2)
if len(aa) == 2 {
opts[aa[0]] = aa[1]
} else {
opts[aa[0]] = ""
}
}
}
return ss[0], opts
}
type ReflectCache struct {
tableNames map[string]string
fields map[string][]string

View File

@ -58,7 +58,7 @@ func Test_fieldsNamesOf(t *testing.T) {
{
name: "MarginInterest",
args: args{record: &types.MarginInterest{}},
want: []string{"gid", "exchange", "asset", "principle", "interest", "interest_rate", "isolated_symbol", "time"},
want: []string{"exchange", "asset", "principle", "interest", "interest_rate", "isolated_symbol", "time"},
},
}
for _, tt := range tests {