diff --git a/columns.go b/columns.go index fb9d8fe..a42e58c 100644 --- a/columns.go +++ b/columns.go @@ -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: diff --git a/columns_test.go b/columns_test.go index fc95a1f..d525226 100644 --- a/columns_test.go +++ b/columns_test.go @@ -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"` diff --git a/values_test.go b/values_test.go index 557cf6f..ac4138d 100644 --- a/values_test.go +++ b/values_test.go @@ -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