@@ -35,7 +35,7 @@ func UpdateYamlValue(yamlBytes []byte, path []string, value string) ([]byte, err
3535 }
3636
3737 // Convert the updated YAML node back to YAML bytes.
38- updatedYAMLBytes , err := yamlMarshal (body )
38+ updatedYAMLBytes , err := YamlMarshal (body )
3939 if err != nil {
4040 return nil , fmt .Errorf ("failed to convert YAML node to bytes: %w" , err )
4141 }
@@ -100,39 +100,25 @@ func lookupKey(node *yaml.Node, key string) (*yaml.Node, *yaml.Node) {
100100 return nil , nil
101101}
102102
103- // Walks a yaml document to the specified path, and then applies the transformation to that node.
103+ // Walks a yaml tree to the specified path, and then applies the transformation to that node.
104104//
105105// The transform must return true if it made changes to the node.
106106// If the requested path is not defined in the document, no changes are made to the document.
107107//
108- // If no changes are made, the original document is returned.
109- // If changes are made, a newly marshalled document is returned. (This may result in different indentation for all nodes)
110- func TransformNode (yamlBytes []byte , path []string , transform func (node * yaml.Node ) (bool , error )) ([]byte , error ) {
111- // Parse the YAML file.
112- var node yaml.Node
113- err := yaml .Unmarshal (yamlBytes , & node )
114- if err != nil {
115- return nil , fmt .Errorf ("failed to parse YAML: %w" , err )
116- }
117-
108+ // Returns true if the transform made changes to the node
109+ func TransformNode (rootNode * yaml.Node , path []string , transform func (node * yaml.Node ) (bool , error )) (bool , error ) {
118110 // Empty document: nothing to do.
119- if len (node .Content ) == 0 {
120- return yamlBytes , nil
111+ if len (rootNode .Content ) == 0 {
112+ return false , nil
121113 }
122114
123- body := node .Content [0 ]
115+ body := rootNode .Content [0 ]
124116
125117 if didTransform , err := transformNode (body , path , transform ); err != nil || ! didTransform {
126- return yamlBytes , err
118+ return false , err
127119 }
128120
129- // Convert the updated YAML node back to YAML bytes.
130- updatedYAMLBytes , err := yamlMarshal (body )
131- if err != nil {
132- return nil , fmt .Errorf ("failed to convert YAML node to bytes: %w" , err )
133- }
134-
135- return updatedYAMLBytes , nil
121+ return true , nil
136122}
137123
138124// A recursive function to walk down the tree. See TransformNode for more details.
@@ -149,34 +135,22 @@ func transformNode(node *yaml.Node, path []string, transform func(node *yaml.Nod
149135 return transformNode (valueNode , path [1 :], transform )
150136}
151137
152- // takes a yaml document in bytes , a path to a key, and a new name for the key.
138+ // Takes the root node of a yaml document, a path to a key, and a new name for the key.
153139// Will rename the key to the new name if it exists, and do nothing otherwise.
154- func RenameYamlKey (yamlBytes []byte , path []string , newKey string ) ([]byte , error ) {
155- // Parse the YAML file.
156- var node yaml.Node
157- err := yaml .Unmarshal (yamlBytes , & node )
158- if err != nil {
159- return nil , fmt .Errorf ("failed to parse YAML: %w for bytes %s" , err , string (yamlBytes ))
160- }
161-
140+ // Returns true if it found the old path and renamed the key
141+ func RenameYamlKey (rootNode * yaml.Node , path []string , newKey string ) (bool , error ) {
162142 // Empty document: nothing to do.
163- if len (node .Content ) == 0 {
164- return yamlBytes , nil
143+ if len (rootNode .Content ) == 0 {
144+ return false , nil
165145 }
166146
167- body := node .Content [0 ]
147+ body := rootNode .Content [0 ]
168148
169149 if didRename , err := renameYamlKey (body , path , newKey ); err != nil || ! didRename {
170- return yamlBytes , err
171- }
172-
173- // Convert the updated YAML node back to YAML bytes.
174- updatedYAMLBytes , err := yamlMarshal (body )
175- if err != nil {
176- return nil , fmt .Errorf ("failed to convert YAML node to bytes: %w" , err )
150+ return false , err
177151 }
178152
179- return updatedYAMLBytes , nil
153+ return true , nil
180154}
181155
182156// Recursive function to rename the YAML key.
@@ -206,34 +180,21 @@ func renameYamlKey(node *yaml.Node, path []string, newKey string) (bool, error)
206180
207181// Traverses a yaml document, calling the callback function for each node. The
208182// callback is allowed to modify the node in place, in which case it should
209- // return true. The function returns the original yaml document if none of the
210- // callbacks returned true, and the modified document otherwise.
211- func Walk (yamlBytes []byte , callback func (node * yaml.Node , path string ) bool ) ([]byte , error ) {
212- // Parse the YAML file.
213- var node yaml.Node
214- err := yaml .Unmarshal (yamlBytes , & node )
215- if err != nil {
216- return nil , fmt .Errorf ("failed to parse YAML: %w" , err )
217- }
218-
183+ // return true. The function returns true if the callback modified the document at all,
184+ // and false otherwise.
185+ func Walk (rootNode * yaml.Node , callback func (node * yaml.Node , path string ) bool ) (bool , error ) {
219186 // Empty document: nothing to do.
220- if len (node .Content ) == 0 {
221- return yamlBytes , nil
187+ if len (rootNode .Content ) == 0 {
188+ return false , nil
222189 }
223190
224- body := node .Content [0 ]
191+ body := rootNode .Content [0 ]
225192
226193 if didChange , err := walk (body , "" , callback ); err != nil || ! didChange {
227- return yamlBytes , err
194+ return false , err
228195 }
229196
230- // Convert the updated YAML node back to YAML bytes.
231- updatedYAMLBytes , err := yamlMarshal (body )
232- if err != nil {
233- return nil , fmt .Errorf ("failed to convert YAML node to bytes: %w" , err )
234- }
235-
236- return updatedYAMLBytes , nil
197+ return true , nil
237198}
238199
239200func walk (node * yaml.Node , path string , callback func (* yaml.Node , string ) bool ) (bool , error ) {
@@ -275,7 +236,7 @@ func walk(node *yaml.Node, path string, callback func(*yaml.Node, string) bool)
275236 return didChange , nil
276237}
277238
278- func yamlMarshal (node * yaml.Node ) ([]byte , error ) {
239+ func YamlMarshal (node * yaml.Node ) ([]byte , error ) {
279240 var buffer bytes.Buffer
280241 encoder := yaml .NewEncoder (& buffer )
281242 encoder .SetIndent (2 )
0 commit comments