mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 17:12:51 +00:00
return error for duplicate keys rather than panicking
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user