Skip to content

Commit 02611da

Browse files
committed
Remove unused function UpdateYamlValue
1 parent a199ed1 commit 02611da

File tree

2 files changed

+0
-189
lines changed

2 files changed

+0
-189
lines changed

pkg/utils/yaml_utils/yaml_utils.go

Lines changed: 0 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -8,88 +8,6 @@ import (
88
"gopkg.in/yaml.v3"
99
)
1010

11-
// takes a yaml document in bytes, a path to a key, and a value to set. The value must be a scalar.
12-
func UpdateYamlValue(yamlBytes []byte, path []string, value string) ([]byte, error) {
13-
// Parse the YAML file.
14-
var node yaml.Node
15-
err := yaml.Unmarshal(yamlBytes, &node)
16-
if err != nil {
17-
return nil, fmt.Errorf("failed to parse YAML: %w", err)
18-
}
19-
20-
// Empty document: need to create the top-level map ourselves
21-
if len(node.Content) == 0 {
22-
node.Content = append(node.Content, &yaml.Node{
23-
Kind: yaml.MappingNode,
24-
})
25-
}
26-
27-
body := node.Content[0]
28-
29-
if body.Kind != yaml.MappingNode {
30-
return yamlBytes, errors.New("yaml document is not a dictionary")
31-
}
32-
33-
if didChange, err := updateYamlNode(body, path, value); err != nil || !didChange {
34-
return yamlBytes, err
35-
}
36-
37-
// Convert the updated YAML node back to YAML bytes.
38-
updatedYAMLBytes, err := YamlMarshal(body)
39-
if err != nil {
40-
return nil, fmt.Errorf("failed to convert YAML node to bytes: %w", err)
41-
}
42-
43-
return updatedYAMLBytes, nil
44-
}
45-
46-
// Recursive function to update the YAML node.
47-
func updateYamlNode(node *yaml.Node, path []string, value string) (bool, error) {
48-
if len(path) == 0 {
49-
if node.Kind != yaml.ScalarNode {
50-
return false, errors.New("yaml node is not a scalar")
51-
}
52-
if node.Value != value {
53-
node.Value = value
54-
return true, nil
55-
}
56-
return false, nil
57-
}
58-
59-
if node.Kind != yaml.MappingNode {
60-
return false, errors.New("yaml node in path is not a dictionary")
61-
}
62-
63-
key := path[0]
64-
if _, valueNode := lookupKey(node, key); valueNode != nil {
65-
return updateYamlNode(valueNode, path[1:], value)
66-
}
67-
68-
// if the key doesn't exist, we'll add it
69-
70-
// at end of path: add the new key, done
71-
if len(path) == 1 {
72-
node.Content = append(node.Content, &yaml.Node{
73-
Kind: yaml.ScalarNode,
74-
Value: key,
75-
}, &yaml.Node{
76-
Kind: yaml.ScalarNode,
77-
Value: value,
78-
})
79-
return true, nil
80-
}
81-
82-
// otherwise, create the missing intermediate node and continue
83-
newNode := &yaml.Node{
84-
Kind: yaml.MappingNode,
85-
}
86-
node.Content = append(node.Content, &yaml.Node{
87-
Kind: yaml.ScalarNode,
88-
Value: key,
89-
}, newNode)
90-
return updateYamlNode(newNode, path[1:], value)
91-
}
92-
9311
func lookupKey(node *yaml.Node, key string) (*yaml.Node, *yaml.Node) {
9412
for i := 0; i < len(node.Content)-1; i += 2 {
9513
if node.Content[i].Value == key {

pkg/utils/yaml_utils/yaml_utils_test.go

Lines changed: 0 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -8,113 +8,6 @@ import (
88
"gopkg.in/yaml.v3"
99
)
1010

11-
func TestUpdateYamlValue(t *testing.T) {
12-
tests := []struct {
13-
name string
14-
in string
15-
path []string
16-
value string
17-
expectedOut string
18-
expectedErr string
19-
}{
20-
{
21-
name: "update value",
22-
in: "foo: bar\n",
23-
path: []string{"foo"},
24-
value: "baz",
25-
expectedOut: "foo: baz\n",
26-
expectedErr: "",
27-
},
28-
{
29-
name: "add new key and value",
30-
in: "foo: bar\n",
31-
path: []string{"foo2"},
32-
value: "baz",
33-
expectedOut: "foo: bar\nfoo2: baz\n",
34-
expectedErr: "",
35-
},
36-
{
37-
name: "add new key and value when document was empty",
38-
in: "",
39-
path: []string{"foo"},
40-
value: "bar",
41-
expectedOut: "foo: bar\n",
42-
expectedErr: "",
43-
},
44-
{
45-
name: "preserve inline comment",
46-
in: "foo: bar # my comment\n",
47-
path: []string{"foo2"},
48-
value: "baz",
49-
expectedOut: "foo: bar # my comment\nfoo2: baz\n",
50-
expectedErr: "",
51-
},
52-
{
53-
name: "nested update",
54-
in: "foo:\n bar: baz\n",
55-
path: []string{"foo", "bar"},
56-
value: "qux",
57-
expectedOut: "foo:\n bar: qux\n",
58-
expectedErr: "",
59-
},
60-
{
61-
name: "nested where parents doesn't exist yet",
62-
in: "",
63-
path: []string{"foo", "bar", "baz"},
64-
value: "qux",
65-
expectedOut: "foo:\n bar:\n baz: qux\n",
66-
expectedErr: "",
67-
},
68-
{
69-
name: "don't rewrite file if value didn't change",
70-
in: "foo:\n bar: baz\n",
71-
path: []string{"foo", "bar"},
72-
value: "baz",
73-
expectedOut: "foo:\n bar: baz\n",
74-
expectedErr: "",
75-
},
76-
77-
// Error cases
78-
{
79-
name: "existing document is not a dictionary",
80-
in: "42\n",
81-
path: []string{"foo"},
82-
value: "bar",
83-
expectedOut: "42\n",
84-
expectedErr: "yaml document is not a dictionary",
85-
},
86-
{
87-
name: "trying to update a note that is not a scalar",
88-
in: "foo: [1, 2, 3]\n",
89-
path: []string{"foo"},
90-
value: "bar",
91-
expectedOut: "foo: [1, 2, 3]\n",
92-
expectedErr: "yaml node is not a scalar",
93-
},
94-
{
95-
name: "not all path elements are dictionaries",
96-
in: "foo:\n bar: [1, 2, 3]\n",
97-
path: []string{"foo", "bar", "baz"},
98-
value: "qux",
99-
expectedOut: "foo:\n bar: [1, 2, 3]\n",
100-
expectedErr: "yaml node in path is not a dictionary",
101-
},
102-
}
103-
104-
for _, test := range tests {
105-
t.Run(test.name, func(t *testing.T) {
106-
out, actualErr := UpdateYamlValue([]byte(test.in), test.path, test.value)
107-
if test.expectedErr == "" {
108-
assert.NoError(t, actualErr)
109-
} else {
110-
assert.EqualError(t, actualErr, test.expectedErr)
111-
}
112-
113-
assert.Equal(t, test.expectedOut, string(out))
114-
})
115-
}
116-
}
117-
11811
func TestRenameYamlKey(t *testing.T) {
11912
tests := []struct {
12013
name string

0 commit comments

Comments
 (0)