return error for duplicate keys rather than panicking

This commit is contained in:
Natasha Sarkar
2021-03-03 12:03:36 -08:00
parent 93dd571df9
commit 722b0131f0
15 changed files with 86 additions and 31 deletions

View File

@@ -816,17 +816,18 @@ func FromMap(m map[string]interface{}) (*RNode, error) {
return Parse(string(c))
}
func (rn *RNode) Map() map[string]interface{} {
func (rn *RNode) Map() (map[string]interface{}, error) {
if rn == nil || rn.value == nil {
return make(map[string]interface{})
return make(map[string]interface{}), nil
}
var result map[string]interface{}
if err := rn.value.Decode(&result); err != nil {
// Should not be able to create an RNode that cannot be decoded;
// this is an unrecoverable error.
log.Fatalf("failed to decode ynode: %v", err)
str, _ := rn.String()
return nil, fmt.Errorf("received error %w for the following resource:\n%s", err, str)
}
return result
return result, nil
}
// ConvertJSONToYamlNode parses input json string and returns equivalent yaml node

View File

@@ -386,7 +386,9 @@ func TestRNodeGetValidatedMetadata(t *testing.T) {
}
func TestRNodeMapEmpty(t *testing.T) {
assert.Equal(t, 0, len(NewRNode(nil).Map()))
newRNodeMap, err := NewRNode(nil).Map()
assert.NoError(t, err)
assert.Equal(t, 0, len(newRNodeMap))
}
func TestRNodeMap(t *testing.T) {
@@ -411,7 +413,8 @@ func TestRNodeMap(t *testing.T) {
},
}
actual := wn.Map()
actual, err := wn.Map()
assert.NoError(t, err)
if diff := cmp.Diff(expected, actual); diff != "" {
t.Fatalf("actual map does not deep equal expected map:\n%v", diff)
}