Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

don't modify input when converting to orderedmap #870

Merged
merged 3 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions pkg/orderedmap/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,21 @@ func (c Conversion) fromUnorderedMaps(object interface{}) interface{} {
panic("Expected map[interface{}]interface{} instead of *unordered.Map in fromUnorderedMaps")

case []interface{}:
result := make([]interface{}, len(typedObj))
for i, item := range typedObj {
typedObj[i] = c.fromUnorderedMaps(item)
result[i] = c.fromUnorderedMaps(item)
}
return typedObj
return result

// some sources (e.g. toml library) yield specific type of slices.
// slices in Go are not covariant, so each flavor of slice must have its own case, here.
// process these exactly the same way we do for generic slices (prior case)
case []map[string]interface{}:
resultArray := make([]interface{}, len(typedObj))
result := make([]interface{}, len(typedObj))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed this because all the other case blocks use result as the new variable name

for i, item := range typedObj {
resultArray[i] = c.fromUnorderedMaps(item)
result[i] = c.fromUnorderedMaps(item)
}
return resultArray
return result
default:
return typedObj
}
Expand Down
25 changes: 25 additions & 0 deletions pkg/orderedmap/convert_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2020 VMware, Inc.
// SPDX-License-Identifier: Apache-2.0
package orderedmap_test

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: could you please add header.

import (
"reflect"
"testing"

"carvel.dev/ytt/pkg/orderedmap"
)

func TestFromUnorderedMaps(t *testing.T) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test fails without the changes to convert.go

inputA := map[string]interface{}{
"key": []interface{}{map[string]interface{}{"nestedKey": "nestedValue"}},
}
inputB := map[string]interface{}{
"key": []interface{}{map[string]interface{}{"nestedKey": "nestedValue"}},
}

orderedmap.Conversion{Object: inputA}.FromUnorderedMaps()

if !reflect.DeepEqual(inputA, inputB) {
t.Errorf("Nested object was modified. Got: %v, Expected: %v", inputA, inputB)
}
}
Loading