Skip to content

Commit

Permalink
Improve getMapsFromPath to handle ptrs to array/slice and add a test
Browse files Browse the repository at this point in the history
  • Loading branch information
ilija42 committed Nov 8, 2024
1 parent 3b320ad commit df17c87
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
6 changes: 5 additions & 1 deletion pkg/codec/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,10 @@ func getMapsFromPath(valueMap map[string]any, path []string) ([]map[string]any,
}

iItem := reflect.ValueOf(item)
if iItem.Kind() == reflect.Ptr {
iItem = iItem.Elem()
}

switch iItem.Kind() {
case reflect.Array, reflect.Slice:
length := iItem.Len()
Expand All @@ -340,7 +344,7 @@ func getMapsFromPath(valueMap map[string]any, path []string) ([]map[string]any,

// cleanup empty values for non path keys
for k, v := range m {
if k != p && reflect.ValueOf(v).IsZero() {
if k != p && reflect.ValueOf(v).IsValid() && reflect.ValueOf(v).IsZero() {
delete(m, k)
}
}
Expand Down
27 changes: 23 additions & 4 deletions pkg/codec/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,36 @@ func TestGetMapsFromPath(t *testing.T) {
TestASlice []testA
}

type testC struct {
TestAPtrSlice *[]testA
}

type testStruct struct {
A testA
B testB
C, D int
C testC
D, F int
}

testMap := map[string]any{"A": map[string]any{"B": []testStruct{{B: testB{TestASlice: []testA{{IntSlice: []int{3, 2, 0}}, {IntSlice: []int{0, 1, 2}}}}, C: 10, D: 100}, {C: 20, D: 200}}}}
ptrSlice := &[]testA{{IntSlice: []int{4, 3, 2}}, {IntSlice: []int{1, 2, 3}}}
testMap := map[string]any{"A": map[string]any{"B": []testStruct{{B: testB{TestASlice: []testA{{IntSlice: []int{3, 2, 0}}, {IntSlice: []int{0, 1, 2}}}}, C: testC{TestAPtrSlice: ptrSlice}, D: 10, F: 100}, {D: 20, F: 200}}}}
t.Parallel()
actual, err := getMapsFromPath(testMap, []string{"A"})
require.NoError(t, err)
assert.Equal(t, []map[string]any{{"B": []testStruct{{B: testB{TestASlice: []testA{{IntSlice: []int{3, 2, 0}}, {IntSlice: []int{0, 1, 2}}}}, C: 10, D: 100}, {C: 20, D: 200}}}}, actual)
assert.Equal(t, []map[string]any{{"B": []testStruct{{B: testB{TestASlice: []testA{{IntSlice: []int{3, 2, 0}}, {IntSlice: []int{0, 1, 2}}}}, C: testC{TestAPtrSlice: ptrSlice}, D: 10, F: 100}, {D: 20, F: 200}}}}, actual)

actual, err = getMapsFromPath(testMap, []string{"A", "B"})
require.NoError(t, err)
assert.Equal(t, []map[string]any{{"A": map[string]any{"IntSlice": []int(nil)}, "B": map[string]any{"TestASlice": []testA{{IntSlice: []int{3, 2, 0}}, {IntSlice: []int{0, 1, 2}}}}, "C": 10, "D": 100}, {"A": map[string]any{"IntSlice": []int(nil)}, "B": map[string]any{"TestASlice": []testA(nil)}, "C": 20, "D": 200}}, actual)
assert.Equal(t, []map[string]interface{}{
{
"A": map[string]interface{}{"IntSlice": []int(nil)},
"B": map[string]interface{}{"TestASlice": []testA{{IntSlice: []int{3, 2, 0}}, {IntSlice: []int{0, 1, 2}}}},
"C": map[string]interface{}{"TestAPtrSlice": ptrSlice}, "D": 10, "F": 100},
{
"A": map[string]interface{}{"IntSlice": []int(nil)},
"B": map[string]interface{}{"TestASlice": []testA(nil)}, "C": map[string]interface{}{"TestAPtrSlice": (*[]testA)(nil)}, "D": 20, "F": 200,
},
}, actual)

actual, err = getMapsFromPath(testMap, []string{"A", "B", "B"})
require.NoError(t, err)
Expand All @@ -48,6 +63,10 @@ func TestGetMapsFromPath(t *testing.T) {
actual, err = getMapsFromPath(testMap, []string{"A", "B", "B", "TestASlice"})
require.NoError(t, err)
assert.Equal(t, []map[string]any{{"IntSlice": []int{3, 2, 0}}, {"IntSlice": []int{0, 1, 2}}}, actual)

actual, err = getMapsFromPath(testMap, []string{"A", "B", "C", "TestAPtrSlice"})
require.NoError(t, err)
assert.Equal(t, []map[string]any{{"IntSlice": []int{4, 3, 2}}, {"IntSlice": []int{1, 2, 3}}}, actual)
}

func TestFitsInNBitsSigned(t *testing.T) {
Expand Down

0 comments on commit df17c87

Please sign in to comment.