Skip to content

Commit cc79e3d

Browse files
charmfocuswukun30
andauthored
feat(sqlx): add field tag (-) skip logic in unwrapFields (#5010)
Co-authored-by: wukun30 <[email protected]>
1 parent f11b78c commit cc79e3d

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

core/stores/sqlx/orm.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,11 @@ func unwrapFields(v reflect.Value) []reflect.Value {
279279
if child.Kind() == reflect.Struct && childType.Anonymous {
280280
fields = append(fields, unwrapFields(child)...)
281281
} else {
282+
key := parseTagName(childType)
283+
if key == "-" {
284+
continue
285+
}
286+
282287
fields = append(fields, child)
283288
}
284289
}

core/stores/sqlx/orm_test.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,39 @@ func TestUnmarshalRowStructWithTags(t *testing.T) {
370370
})
371371
}
372372

373+
func TestUnmarshalRowStructWithTagsIgnoreFields(t *testing.T) {
374+
dbtest.RunTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
375+
value := new(struct {
376+
Age int `db:"age"`
377+
Name string
378+
Ignore bool
379+
})
380+
381+
rs := sqlmock.NewRows([]string{"name", "age"}).FromCSVString("liao,5")
382+
mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
383+
384+
assert.ErrorIs(t, query(context.Background(), db, func(rows *sql.Rows) error {
385+
return unmarshalRow(value, rows, true)
386+
}, "select name, age from users where user=?", "anyone"), ErrNotMatchDestination)
387+
})
388+
389+
dbtest.RunTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
390+
value := new(struct {
391+
Age int `db:"age"`
392+
Name string
393+
Ignore bool `db:"-"`
394+
})
395+
396+
rs := sqlmock.NewRows([]string{"name", "age"}).FromCSVString("liao,5")
397+
mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
398+
399+
assert.Nil(t, query(context.Background(), db, func(rows *sql.Rows) error {
400+
return unmarshalRow(value, rows, true)
401+
}, "select name, age from users where user=?", "anyone"))
402+
assert.Equal(t, 5, value.Age)
403+
})
404+
}
405+
373406
func TestUnmarshalRowStructWithTagsWrongColumns(t *testing.T) {
374407
value := new(struct {
375408
Age *int `db:"age"`
@@ -999,6 +1032,58 @@ func TestUnmarshalRowsStructWithTags(t *testing.T) {
9991032
})
10001033
}
10011034

1035+
func TestUnmarshalRowsStructWithTagsIgnoreFields(t *testing.T) {
1036+
expect := []struct {
1037+
Name string
1038+
Age int64
1039+
Ignore bool
1040+
}{
1041+
{
1042+
Name: "first",
1043+
Age: 2,
1044+
Ignore: false,
1045+
},
1046+
{
1047+
Name: "second",
1048+
Age: 3,
1049+
Ignore: false,
1050+
},
1051+
}
1052+
1053+
dbtest.RunTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
1054+
var value []struct {
1055+
Age int64 `db:"age"`
1056+
Name string `db:"name"`
1057+
Ignore bool
1058+
}
1059+
1060+
rs := sqlmock.NewRows([]string{"name", "age"}).FromCSVString("first,2\nsecond,3")
1061+
mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
1062+
assert.ErrorIs(t, query(context.Background(), db, func(rows *sql.Rows) error {
1063+
return unmarshalRows(&value, rows, true)
1064+
}, "select name, age from users where user=?", "anyone"), ErrNotMatchDestination)
1065+
})
1066+
1067+
dbtest.RunTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
1068+
var value []struct {
1069+
Age int64 `db:"age"`
1070+
Name string `db:"name"`
1071+
Ignore bool `db:"-"`
1072+
}
1073+
1074+
rs := sqlmock.NewRows([]string{"name", "age"}).FromCSVString("first,2\nsecond,3")
1075+
mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
1076+
assert.Nil(t, query(context.Background(), db, func(rows *sql.Rows) error {
1077+
return unmarshalRows(&value, rows, true)
1078+
}, "select name, age from users where user=?", "anyone"))
1079+
1080+
for i, each := range expect {
1081+
assert.Equal(t, each.Name, value[i].Name)
1082+
assert.Equal(t, each.Age, value[i].Age)
1083+
}
1084+
})
1085+
}
1086+
10021087
func TestUnmarshalRowsStructAndEmbeddedAnonymousStructWithTags(t *testing.T) {
10031088
type Embed struct {
10041089
Value int64 `db:"value"`

0 commit comments

Comments
 (0)