feat(1765): remove useless keys from the outpu yml

Signed-off-by: Martin Jirku <martin@jirku.sk>
This commit is contained in:
Martin Jirku 2023-12-07 08:30:32 +01:00
parent af06917dbb
commit 3a818f4b01
No known key found for this signature in database
GPG Key ID: 9924FEEE8B35126B
2 changed files with 51 additions and 1 deletions

View File

@ -280,6 +280,34 @@ func marshal(obj runtime.Object, jsonFormat bool, indent int) (data []byte, err
return
}
// remove empty map[string]interface{} strings from the object
//
// Note: this function uses recursion, use it only objects created by the unmarshalled json.
// Passing cyclic structures to removeEmptyInterfaces will result in a stack overflow.
func removeEmptyInterfaces(obj interface{}) interface{} {
switch v := obj.(type) {
case []interface{}:
for i, val := range v {
if valMap, ok := val.(map[string]interface{}); (ok && len(valMap) == 0) || val == nil {
v = append(v[:i], v[i+1:]...)
} else {
v[i] = removeEmptyInterfaces(val)
}
}
case map[string]interface{}:
for k, val := range v {
if valMap, ok := val.(map[string]interface{}); (ok && len(valMap) == 0) || val == nil {
delete(v, k)
} else {
v[k] = removeEmptyInterfaces(val)
}
}
default:
return v
}
return obj
}
// Convert JSON to YAML.
func jsonToYaml(j []byte, spaces int) ([]byte, error) {
// Convert the JSON to an object.
@ -293,7 +321,7 @@ func jsonToYaml(j []byte, spaces int) ([]byte, error) {
if err != nil {
return nil, err
}
jsonObj = removeEmptyInterfaces(jsonObj)
var b bytes.Buffer
encoder := yaml.NewEncoder(&b)
encoder.SetIndent(spaces)

View File

@ -1145,3 +1145,25 @@ func TestNamespaceGenerationBlank(t *testing.T) {
}
}
}
// Test empty interfaces removal
func TestRemoveEmptyInterfaces(t *testing.T) {
type Obj = map[string]interface{}
var testCases = []struct {
input interface{}
output interface{}
}{
{Obj{"useless": Obj{}}, Obj{}},
{Obj{"usefull": Obj{"usefull": "usefull"}}, Obj{"usefull": Obj{"usefull": "usefull"}}},
{Obj{"usefull": Obj{"usefull": "usefull", "uselessdeep": Obj{}, "uselessnil": nil}}, Obj{"usefull": Obj{"usefull": "usefull"}}},
{"test", "test"},
}
for _, tc := range testCases {
t.Run(fmt.Sprintf("Test removeEmptyInterfaces(%s)", tc.input), func(t *testing.T) {
result := removeEmptyInterfaces(tc.input)
if !reflect.DeepEqual(result, tc.output) {
t.Errorf("Expected %v, got %v", tc.output, result)
}
})
}
}