From 683d793719d00b8323eda29b7f7b35e0dfbea509 Mon Sep 17 00:00:00 2001 From: Archer Date: Sat, 24 Dec 2022 21:27:32 +0800 Subject: [PATCH] RawFieldNames should ignore the field whose name is start with a dash (#2725) --- core/stores/builder/builder.go | 4 ++++ core/stores/builder/builder_test.go | 30 +++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/core/stores/builder/builder.go b/core/stores/builder/builder.go index 1849a42c..828d1ec0 100644 --- a/core/stores/builder/builder.go +++ b/core/stores/builder/builder.go @@ -45,9 +45,13 @@ func RawFieldNames(in interface{}, postgresSql ...bool) []string { // `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 } diff --git a/core/stores/builder/builder_test.go b/core/stores/builder/builder_test.go index 69932483..8741b7d6 100644 --- a/core/stores/builder/builder_test.go +++ b/core/stores/builder/builder_test.go @@ -39,3 +39,33 @@ func TestFieldNamesWithTagOptions(t *testing.T) { assert.Equal(t, expected, out) }) } + +type mockedUserWithDashTag struct { + ID string `db:"id" json:"id,omitempty"` + UserName string `db:"user_name" json:"userName,omitempty"` + Mobile string `db:"-" json:"mobile,omitempty"` +} + +func TestFieldNamesWithDashTag(t *testing.T) { + t.Run("new", func(t *testing.T) { + var u mockedUserWithDashTag + out := RawFieldNames(&u) + expected := []string{"`id`", "`user_name`"} + assert.Equal(t, expected, out) + }) +} + +type mockedUserWithDashTagAndOptions struct { + ID string `db:"id" json:"id,omitempty"` + UserName string `db:"user_name,type=varchar,length=255" json:"userName,omitempty"` + Mobile string `db:"-,type=varchar,length=255" json:"mobile,omitempty"` +} + +func TestFieldNamesWithDashTagAndOptions(t *testing.T) { + t.Run("new", func(t *testing.T) { + var u mockedUserWithDashTagAndOptions + out := RawFieldNames(&u) + expected := []string{"`id`", "`user_name`"} + assert.Equal(t, expected, out) + }) +}