From e7dfd4a654d957df614796452ad3dc96fe13750e Mon Sep 17 00:00:00 2001 From: c9s Date: Wed, 8 Jun 2022 12:03:58 +0800 Subject: [PATCH] fix reflect insert (remove gid field) --- pkg/service/reflect.go | 25 +++++++++++++++++++++++++ pkg/service/reflect_test.go | 2 +- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/pkg/service/reflect.go b/pkg/service/reflect.go index 89f548ed6..b60c0c337 100644 --- a/pkg/service/reflect.go +++ b/pkg/service/reflect.go @@ -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 diff --git a/pkg/service/reflect_test.go b/pkg/service/reflect_test.go index fa19056b7..9eb525ae9 100644 --- a/pkg/service/reflect_test.go +++ b/pkg/service/reflect_test.go @@ -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 {