From 960027f97eacdebf48f4b486c0869f696bc44aef Mon Sep 17 00:00:00 2001 From: ilija Date: Fri, 8 Nov 2024 15:38:14 +0100 Subject: [PATCH 1/2] Improve getMapsFromPath to handle ptrs to array/slice and add a test --- pkg/codec/utils.go | 6 +++++- pkg/codec/utils_test.go | 27 +++++++++++++++++++++++---- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/pkg/codec/utils.go b/pkg/codec/utils.go index 9c4e83a1b..ffef87a7e 100644 --- a/pkg/codec/utils.go +++ b/pkg/codec/utils.go @@ -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() @@ -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) } } diff --git a/pkg/codec/utils_test.go b/pkg/codec/utils_test.go index 335bb7a0d..dcf6bbc2c 100644 --- a/pkg/codec/utils_test.go +++ b/pkg/codec/utils_test.go @@ -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) @@ -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) { From dbe7c4083f4ed74ca9b1764016329432cf92e819 Mon Sep 17 00:00:00 2001 From: ilija Date: Fri, 8 Nov 2024 15:43:35 +0100 Subject: [PATCH 2/2] minor improvement --- pkg/codec/utils.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/codec/utils.go b/pkg/codec/utils.go index ffef87a7e..650ef7976 100644 --- a/pkg/codec/utils.go +++ b/pkg/codec/utils.go @@ -344,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).IsValid() && reflect.ValueOf(v).IsZero() { + valueOfV := reflect.ValueOf(v) + if k != p && valueOfV.IsValid() && valueOfV.IsZero() { delete(m, k) } }