diff options
author | Stefan Haller <stefan@haller-berlin.de> | 2024-03-30 12:59:44 +0100 |
---|---|---|
committer | Stefan Haller <stefan@haller-berlin.de> | 2024-03-30 13:05:33 +0100 |
commit | 84e82ea8b69f9ef9fa768a255ac2959c381831d6 (patch) | |
tree | ce6d5dd02b7a713a73b09f1aec3e159229ff132e /pkg/utils | |
parent | 8487bc397d8e78b85141214280c63683bcce6db2 (diff) |
fixup! Introduce a yaml_utils.Walk function
Diffstat (limited to 'pkg/utils')
-rw-r--r-- | pkg/utils/yaml_utils/yaml_utils_test.go | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/pkg/utils/yaml_utils/yaml_utils_test.go b/pkg/utils/yaml_utils/yaml_utils_test.go index 0b445a7ab..78e181d85 100644 --- a/pkg/utils/yaml_utils/yaml_utils_test.go +++ b/pkg/utils/yaml_utils/yaml_utils_test.go @@ -223,10 +223,20 @@ func TestWalk_paths(t *testing.T) { expectedPaths: []string{"", "foo", "foo.x"}, }, { + name: "deeply nested", + document: "foo:\n bar:\n baz: 5", + expectedPaths: []string{"", "foo", "foo.bar", "foo.bar.baz"}, + }, + { name: "array", document: "foo:\n bar: [3, 7]", expectedPaths: []string{"", "foo", "foo.bar", "foo.bar[0]", "foo.bar[1]"}, }, + { + name: "nested arrays", + document: "foo:\n bar: [[3, 7], [8, 9]]", + expectedPaths: []string{"", "foo", "foo.bar", "foo.bar[0]", "foo.bar[0][0]", "foo.bar[0][1]", "foo.bar[1]", "foo.bar[1][0]", "foo.bar[1][1]"}, + }, } for _, test := range tests { @@ -268,6 +278,32 @@ func TestWalk_inPlaceChanges(t *testing.T) { }, expectedOut: "x: 7\ny: 3\n", }, + { + name: "change nested value", + in: "x:\n y: 5", + callback: func(node *yaml.Node, path string) bool { + if path == "x.y" { + node.Value = "7" + return true + } + return false + }, + // indentation is not preserved. See https://github.com/go-yaml/yaml/issues/899 + expectedOut: "x:\n y: 7\n", + }, + { + name: "change array value", + in: "x:\n - y: 5", + callback: func(node *yaml.Node, path string) bool { + if path == "x[0].y" { + node.Value = "7" + return true + } + return false + }, + // indentation is not preserved. See https://github.com/go-yaml/yaml/issues/899 + expectedOut: "x:\n - y: 7\n", + }, } for _, test := range tests { |