Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve getMapsFromPath to handle ptrs to array/slice and map cleanup #926

Merged
merged 2 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 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,8 @@ 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() {
valueOfV := reflect.ValueOf(v)
if k != p && valueOfV.IsValid() && valueOfV.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
Loading