From 221433522d15ed930f54470aeb53099759b9477e Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Thu, 8 Jun 2023 08:45:07 +0200 Subject: [PATCH] Return an error if document is not a dictionary --- pkg/utils/yaml_utils/yaml_utils.go | 5 +++++ pkg/utils/yaml_utils/yaml_utils_test.go | 10 ++++++++++ 2 files changed, 15 insertions(+) diff --git a/pkg/utils/yaml_utils/yaml_utils.go b/pkg/utils/yaml_utils/yaml_utils.go index cff63871e..d93c7f57b 100644 --- a/pkg/utils/yaml_utils/yaml_utils.go +++ b/pkg/utils/yaml_utils/yaml_utils.go @@ -1,6 +1,7 @@ package yaml_utils import ( + "errors" "fmt" "gopkg.in/yaml.v3" @@ -24,6 +25,10 @@ func UpdateYamlValue(yamlBytes []byte, path []string, value string) ([]byte, err body := node.Content[0] + if body.Kind != yaml.MappingNode { + return yamlBytes, errors.New("yaml document is not a dictionary") + } + updateYamlNode(body, path, value) // Convert the updated YAML node back to YAML bytes. diff --git a/pkg/utils/yaml_utils/yaml_utils_test.go b/pkg/utils/yaml_utils/yaml_utils_test.go index fa861b1b8..2e1b2739c 100644 --- a/pkg/utils/yaml_utils/yaml_utils_test.go +++ b/pkg/utils/yaml_utils/yaml_utils_test.go @@ -56,6 +56,16 @@ func TestUpdateYamlValue(t *testing.T) { expectedOut: "foo:\n bar: qux\n", expectedErr: "", }, + + // Error cases + { + name: "existing document is not a dictionary", + in: "42\n", + path: []string{"foo"}, + value: "bar", + expectedOut: "42\n", + expectedErr: "yaml document is not a dictionary", + }, } for _, test := range tests {