diff --git a/variables/yaml_helpers.go b/variables/yaml_helpers.go index a5036407..b7d58bea 100644 --- a/variables/yaml_helpers.go +++ b/variables/yaml_helpers.go @@ -282,9 +282,7 @@ func ParseYamlString(str string) (interface{}, error) { return nil, errors.WithStackTrace(err) } - parsedValue = Convert(parsedValue) - - return parsedValue, nil + return Convert(parsedValue), nil } // Parse a list of YAML files that define variables into a map from variable name to variable value. Along the way, @@ -344,7 +342,7 @@ func ParseVars(varsList []string, varFileList []string) (map[string]interface{}, return util.MergeMaps(varsFromVarsList, varsFromVarFiles), nil } -// Convert converts i of type map[interface{}]interface{} to a map[string]interface{} so that it may be properly +// Convert updates i of type map[interface{}]interface{} to a map[string]interface{} so that it may be properly // marshalled in to JSON. // See: https://github.com/go-yaml/yaml/issues/139 func Convert(i interface{}) interface{} { diff --git a/variables/yaml_helpers_test.go b/variables/yaml_helpers_test.go index 46ce35a0..651edf69 100644 --- a/variables/yaml_helpers_test.go +++ b/variables/yaml_helpers_test.go @@ -76,3 +76,44 @@ func TestParseVariablesFromKeyValuePairs(t *testing.T) { } } } + +func TestConvert(t *testing.T) { + t.Parallel() + + testCases := []struct { + input interface{} + expectedType interface{} + }{ + { + input: "", + expectedType: "string", + }, + { + input: map[interface{}]interface{}{ + "key1": "value1", + "key2": "value2", + }, + expectedType: map[string]interface{}{}, + }, + { + input: map[string]interface{}{ + "key1": "value1", + }, + expectedType: map[string]interface{}{}, + }, + { + input: []interface{}{ + map[interface{}]interface{}{ + "key1": "value1", + }, + "", + }, + expectedType: []interface{}{}, + }, + } + + for _, testCase := range testCases { + actual := Convert(testCase.input) + assert.IsType(t, testCase.expectedType, actual) + } +}