forked from LaconicNetwork/kompose
feat(1765): update tests, move unit test to proper file, improve removeEmptyInterfaces algo
Signed-off-by: Martin Jirku <martin@jirku.sk>
This commit is contained in:
@@ -294,18 +294,30 @@ func removeEmptyInterfaces(obj interface{}) interface{} {
|
||||
v[i] = removeEmptyInterfaces(val)
|
||||
}
|
||||
}
|
||||
return v
|
||||
case map[string]interface{}:
|
||||
for k, val := range v {
|
||||
if valMap, ok := val.(map[string]interface{}); (ok && len(valMap) == 0) || val == nil {
|
||||
if valMap, ok := val.(map[string]interface{}); ok {
|
||||
// It is always map[string]interface{} when passed the map[string]interface{}
|
||||
valMap := removeEmptyInterfaces(valMap).(map[string]interface{})
|
||||
if len(valMap) == 0 {
|
||||
delete(v, k)
|
||||
}
|
||||
} else if val == nil {
|
||||
delete(v, k)
|
||||
} else {
|
||||
v[k] = removeEmptyInterfaces(val)
|
||||
processedInterface := removeEmptyInterfaces(val)
|
||||
if valSlice, ok := processedInterface.([]interface{}); ok && len(valSlice) == 0 {
|
||||
delete(v, k)
|
||||
} else {
|
||||
v[k] = processedInterface
|
||||
}
|
||||
}
|
||||
}
|
||||
return v
|
||||
default:
|
||||
return v
|
||||
}
|
||||
return obj
|
||||
}
|
||||
|
||||
// Convert JSON to YAML.
|
||||
|
||||
@@ -17,6 +17,7 @@ limitations under the License.
|
||||
package kubernetes
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
@@ -704,3 +705,27 @@ func TestFormatEnvName(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"}}},
|
||||
{Obj{"uselessdeep": Obj{"uselessdeep": Obj{}, "uselessnil": nil}}, Obj{}},
|
||||
{Obj{"uselessempty": []interface{}{nil}}, Obj{}},
|
||||
{"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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1145,25 +1145,3 @@ 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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user