From 84e82ea8b69f9ef9fa768a255ac2959c381831d6 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Sat, 30 Mar 2024 12:59:44 +0100 Subject: [PATCH] fixup! Introduce a yaml_utils.Walk function --- pkg/utils/yaml_utils/yaml_utils_test.go | 36 +++++++++++++++++++++++++ 1 file changed, 36 insertions(+) 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 @@ -222,11 +222,21 @@ func TestWalk_paths(t *testing.T) { document: "foo:\n x: 5", 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 {