Skip to content

Commit 70e59fb

Browse files
authored
fix: Pointers to slice are handled correctly (#11)
1 parent 4a270ce commit 70e59fb

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

helpers/slice.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,18 @@ func InterfaceSlice(slice interface{}) []interface{} {
99
return nil
1010
}
1111
s := reflect.ValueOf(slice)
12-
// Keep the distinction between nil and empty slice input
13-
if s.Kind() == reflect.Ptr && s.Elem().Kind() == reflect.Slice && s.Elem().IsNil() {
14-
return nil
12+
//handle slice behind pointer
13+
if s.Kind() == reflect.Ptr && s.Elem().Kind() == reflect.Slice {
14+
// Keep the distinction between nil and empty slice input
15+
if s.Elem().IsNil() {
16+
return nil
17+
}
18+
19+
ret := make([]interface{}, s.Elem().Len())
20+
for i := 0; i < s.Elem().Len(); i++ {
21+
ret[i] = s.Elem().Index(i).Interface()
22+
}
23+
return ret
1524
}
1625
if s.Kind() != reflect.Slice {
1726
return []interface{}{slice}

helpers/slice_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func TestInterfaceSlice(t *testing.T) {
1818
{Name: "empty", Value: []interface{}{}, Want: []interface{}{}},
1919
{Name: "empty_string_array", Value: []string{}, Want: []interface{}{}},
2020
{Name: "string_ptr_array", Value: []*string{&someStringPtr}, Want: []interface{}{&someStringPtr}},
21-
//{Name: "string_array_ptr", Value: &[]string{"a"}, Want: []interface{}{"a"}}, // TODO: support this?
21+
{Name: "string_array_ptr", Value: &[]string{"a"}, Want: []interface{}{"a"}},
2222
}
2323
for _, tc := range cases {
2424
t.Run(tc.Name, func(t *testing.T) {

0 commit comments

Comments
 (0)