Remove unused function UpdateYamlValue

This commit is contained in:
Stefan Haller 2025-04-30 17:51:02 +02:00
parent a199ed1396
commit 02611dad7c
2 changed files with 0 additions and 189 deletions

View file

@ -8,88 +8,6 @@ import (
"gopkg.in/yaml.v3"
)
// takes a yaml document in bytes, a path to a key, and a value to set. The value must be a scalar.
func UpdateYamlValue(yamlBytes []byte, path []string, value string) ([]byte, error) {
// Parse the YAML file.
var node yaml.Node
err := yaml.Unmarshal(yamlBytes, &node)
if err != nil {
return nil, fmt.Errorf("failed to parse YAML: %w", err)
}
// Empty document: need to create the top-level map ourselves
if len(node.Content) == 0 {
node.Content = append(node.Content, &yaml.Node{
Kind: yaml.MappingNode,
})
}
body := node.Content[0]
if body.Kind != yaml.MappingNode {
return yamlBytes, errors.New("yaml document is not a dictionary")
}
if didChange, err := updateYamlNode(body, path, value); err != nil || !didChange {
return yamlBytes, err
}
// Convert the updated YAML node back to YAML bytes.
updatedYAMLBytes, err := YamlMarshal(body)
if err != nil {
return nil, fmt.Errorf("failed to convert YAML node to bytes: %w", err)
}
return updatedYAMLBytes, nil
}
// Recursive function to update the YAML node.
func updateYamlNode(node *yaml.Node, path []string, value string) (bool, error) {
if len(path) == 0 {
if node.Kind != yaml.ScalarNode {
return false, errors.New("yaml node is not a scalar")
}
if node.Value != value {
node.Value = value
return true, nil
}
return false, nil
}
if node.Kind != yaml.MappingNode {
return false, errors.New("yaml node in path is not a dictionary")
}
key := path[0]
if _, valueNode := lookupKey(node, key); valueNode != nil {
return updateYamlNode(valueNode, path[1:], value)
}
// if the key doesn't exist, we'll add it
// at end of path: add the new key, done
if len(path) == 1 {
node.Content = append(node.Content, &yaml.Node{
Kind: yaml.ScalarNode,
Value: key,
}, &yaml.Node{
Kind: yaml.ScalarNode,
Value: value,
})
return true, nil
}
// otherwise, create the missing intermediate node and continue
newNode := &yaml.Node{
Kind: yaml.MappingNode,
}
node.Content = append(node.Content, &yaml.Node{
Kind: yaml.ScalarNode,
Value: key,
}, newNode)
return updateYamlNode(newNode, path[1:], value)
}
func lookupKey(node *yaml.Node, key string) (*yaml.Node, *yaml.Node) {
for i := 0; i < len(node.Content)-1; i += 2 {
if node.Content[i].Value == key {

View file

@ -8,113 +8,6 @@ import (
"gopkg.in/yaml.v3"
)
func TestUpdateYamlValue(t *testing.T) {
tests := []struct {
name string
in string
path []string
value string
expectedOut string
expectedErr string
}{
{
name: "update value",
in: "foo: bar\n",
path: []string{"foo"},
value: "baz",
expectedOut: "foo: baz\n",
expectedErr: "",
},
{
name: "add new key and value",
in: "foo: bar\n",
path: []string{"foo2"},
value: "baz",
expectedOut: "foo: bar\nfoo2: baz\n",
expectedErr: "",
},
{
name: "add new key and value when document was empty",
in: "",
path: []string{"foo"},
value: "bar",
expectedOut: "foo: bar\n",
expectedErr: "",
},
{
name: "preserve inline comment",
in: "foo: bar # my comment\n",
path: []string{"foo2"},
value: "baz",
expectedOut: "foo: bar # my comment\nfoo2: baz\n",
expectedErr: "",
},
{
name: "nested update",
in: "foo:\n bar: baz\n",
path: []string{"foo", "bar"},
value: "qux",
expectedOut: "foo:\n bar: qux\n",
expectedErr: "",
},
{
name: "nested where parents doesn't exist yet",
in: "",
path: []string{"foo", "bar", "baz"},
value: "qux",
expectedOut: "foo:\n bar:\n baz: qux\n",
expectedErr: "",
},
{
name: "don't rewrite file if value didn't change",
in: "foo:\n bar: baz\n",
path: []string{"foo", "bar"},
value: "baz",
expectedOut: "foo:\n bar: baz\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",
},
{
name: "trying to update a note that is not a scalar",
in: "foo: [1, 2, 3]\n",
path: []string{"foo"},
value: "bar",
expectedOut: "foo: [1, 2, 3]\n",
expectedErr: "yaml node is not a scalar",
},
{
name: "not all path elements are dictionaries",
in: "foo:\n bar: [1, 2, 3]\n",
path: []string{"foo", "bar", "baz"},
value: "qux",
expectedOut: "foo:\n bar: [1, 2, 3]\n",
expectedErr: "yaml node in path is not a dictionary",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
out, actualErr := UpdateYamlValue([]byte(test.in), test.path, test.value)
if test.expectedErr == "" {
assert.NoError(t, actualErr)
} else {
assert.EqualError(t, actualErr, test.expectedErr)
}
assert.Equal(t, test.expectedOut, string(out))
})
}
}
func TestRenameYamlKey(t *testing.T) {
tests := []struct {
name string