Skip to content

Commit

Permalink
fix: Columns and Values should recognize Slice values too (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
max-stytch authored Aug 23, 2023
1 parent 9c197bf commit 644ce11
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
2 changes: 1 addition & 1 deletion columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func supportedColumnType(v reflect.Value) bool {
reflect.Uint64, reflect.Float32, reflect.Float64, reflect.Interface,
reflect.String:
return true
case reflect.Ptr:
case reflect.Ptr, reflect.Slice, reflect.Array:
ptrVal := reflect.New(v.Type().Elem())
return supportedColumnType(ptrVal.Elem())
default:
Expand Down
20 changes: 20 additions & 0 deletions columns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,26 @@ func TestColumnsReturnsStructTagsWithPointers(t *testing.T) {
assert.EqualValues(t, []string{"name"}, cols)
}

func TestColumnsReturnsStructTagsWithArrays(t *testing.T) {
type personGetFilter struct {
PersonIDs *string `db:"id"`
}

cols, err := Columns(&personGetFilter{})
assert.NoError(t, err)
assert.EqualValues(t, []string{"id"}, cols)
}

func TestColumnsReturnsStructTagsWithPointersToArrays(t *testing.T) {
type personGetFilter struct {
PersonIDs *[]string `db:"id"`
}

cols, err := Columns(&personGetFilter{})
assert.NoError(t, err)
assert.EqualValues(t, []string{"id"}, cols)
}

func TestColumnsWorkWithValidSqlValueTypes(t *testing.T) {
type coupon struct {
Value int `db:"value"`
Expand Down
36 changes: 36 additions & 0 deletions values_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,42 @@ func TestValuesReturnsNilPointers(t *testing.T) {
assert.EqualValues(t, []interface{}{(*string)(nil)}, vals)
}

func TestValuesScansSliceDBTags(t *testing.T) {
type person struct {
Names []string `db:"n"`
}

p := &person{Names: []string{"Brett", "The Big B"}}
vals, err := Values([]string{"n"}, p)
require.NoError(t, err)

assert.EqualValues(t, []interface{}{[]string{"Brett", "The Big B"}}, vals)
}

func TestValuesScansNilSliceDBTags(t *testing.T) {
type person struct {
Names []string `db:"n"`
}

p := &person{}
vals, err := Values([]string{"n"}, p)
require.NoError(t, err)

assert.EqualValues(t, []interface{}{[]string(nil)}, vals)
}

func TestValuesScansPointerToSliceDBTags(t *testing.T) {
type personUpdate struct {
Names *[]string `db:"n"`
}
names := []string{"Jack", "J Man"}
p := &personUpdate{Names: &names}
vals, err := Values([]string{"n"}, p)
require.NoError(t, err)

assert.EqualValues(t, []interface{}{&names}, vals)
}

func TestValuesScansNestedFields(t *testing.T) {
type Address struct {
Street string
Expand Down

0 comments on commit 644ce11

Please sign in to comment.