Migrate to only doing marshalling twice, and compare via deep copy

This commit is contained in:
Chris McDonnell 2025-02-23 22:56:07 -05:00
parent a01ca19bb3
commit d39f883b4e
3 changed files with 137 additions and 150 deletions

View file

@ -35,7 +35,7 @@ func UpdateYamlValue(yamlBytes []byte, path []string, value string) ([]byte, err
}
// Convert the updated YAML node back to YAML bytes.
updatedYAMLBytes, err := yamlMarshal(body)
updatedYAMLBytes, err := YamlMarshal(body)
if err != nil {
return nil, fmt.Errorf("failed to convert YAML node to bytes: %w", err)
}
@ -100,147 +100,101 @@ func lookupKey(node *yaml.Node, key string) (*yaml.Node, *yaml.Node) {
return nil, nil
}
// Walks a yaml document to the specified path, and then applies the transformation to that node.
//
// The transform must return true if it made changes to the node.
// Walks a yaml document from the root node to the specified path, and then applies the transformation to that node.
// If the requested path is not defined in the document, no changes are made to the document.
//
// If no changes are made, the original document is returned.
// If changes are made, a newly marshalled document is returned. (This may result in different indentation for all nodes)
func TransformNode(yamlBytes []byte, path []string, transform func(node *yaml.Node) (bool, error)) ([]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)
}
func TransformNode(rootNode *yaml.Node, path []string, transform func(node *yaml.Node) error) error {
// Empty document: nothing to do.
if len(node.Content) == 0 {
return yamlBytes, nil
if len(rootNode.Content) == 0 {
return nil
}
body := node.Content[0]
body := rootNode.Content[0]
if didTransform, err := transformNode(body, path, transform); err != nil || !didTransform {
return yamlBytes, err
if err := transformNode(body, path, transform); err != nil {
return 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
return nil
}
// A recursive function to walk down the tree. See TransformNode for more details.
func transformNode(node *yaml.Node, path []string, transform func(node *yaml.Node) (bool, error)) (bool, error) {
func transformNode(node *yaml.Node, path []string, transform func(node *yaml.Node) error) error {
if len(path) == 0 {
return transform(node)
}
keyNode, valueNode := lookupKey(node, path[0])
if keyNode == nil {
return false, nil
return nil
}
return transformNode(valueNode, path[1:], transform)
}
// takes a yaml document in bytes, a path to a key, and a new name for the key.
// Takes the root node of a yaml document, a path to a key, and a new name for the key.
// Will rename the key to the new name if it exists, and do nothing otherwise.
func RenameYamlKey(yamlBytes []byte, path []string, newKey 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 for bytes %s", err, string(yamlBytes))
}
func RenameYamlKey(rootNode *yaml.Node, path []string, newKey string) error {
// Empty document: nothing to do.
if len(node.Content) == 0 {
return yamlBytes, nil
if len(rootNode.Content) == 0 {
return nil
}
body := node.Content[0]
body := rootNode.Content[0]
if didRename, err := renameYamlKey(body, path, newKey); err != nil || !didRename {
return yamlBytes, err
if err := renameYamlKey(body, path, newKey); err != nil {
return 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
return nil
}
// Recursive function to rename the YAML key.
func renameYamlKey(node *yaml.Node, path []string, newKey string) (bool, error) {
func renameYamlKey(node *yaml.Node, path []string, newKey string) error {
if node.Kind != yaml.MappingNode {
return false, errors.New("yaml node in path is not a dictionary")
return errors.New("yaml node in path is not a dictionary")
}
keyNode, valueNode := lookupKey(node, path[0])
if keyNode == nil {
return false, nil
return nil
}
// end of path reached: rename key
if len(path) == 1 {
// Check that new key doesn't exist yet
if newKeyNode, _ := lookupKey(node, newKey); newKeyNode != nil {
return false, fmt.Errorf("new key `%s' already exists", newKey)
return fmt.Errorf("new key `%s' already exists", newKey)
}
keyNode.Value = newKey
return true, nil
return nil
}
return renameYamlKey(valueNode, path[1:], newKey)
}
// Traverses a yaml document, calling the callback function for each node. The
// callback is allowed to modify the node in place, in which case it should
// return true. The function returns the original yaml document if none of the
// callbacks returned true, and the modified document otherwise.
func Walk(yamlBytes []byte, callback func(node *yaml.Node, path string) bool) ([]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)
}
// callback is expected to modify the node in place
func Walk(rootNode *yaml.Node, callback func(node *yaml.Node, path string)) error {
// Empty document: nothing to do.
if len(node.Content) == 0 {
return yamlBytes, nil
if len(rootNode.Content) == 0 {
return nil
}
body := node.Content[0]
body := rootNode.Content[0]
if didChange, err := walk(body, "", callback); err != nil || !didChange {
return yamlBytes, err
if err := walk(body, "", callback); err != nil {
return 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
return nil
}
func walk(node *yaml.Node, path string, callback func(*yaml.Node, string) bool) (bool, error) {
didChange := callback(node, path)
func walk(node *yaml.Node, path string, callback func(*yaml.Node, string)) error {
callback(node, path)
switch node.Kind {
case yaml.DocumentNode:
return false, errors.New("Unexpected document node in the middle of a yaml tree")
return errors.New("Unexpected document node in the middle of a yaml tree")
case yaml.MappingNode:
for i := 0; i < len(node.Content); i += 2 {
name := node.Content[i].Value
@ -251,31 +205,29 @@ func walk(node *yaml.Node, path string, callback func(*yaml.Node, string) bool)
} else {
childPath = fmt.Sprintf("%s.%s", path, name)
}
didChangeChild, err := walk(childNode, childPath, callback)
err := walk(childNode, childPath, callback)
if err != nil {
return false, err
return err
}
didChange = didChange || didChangeChild
}
case yaml.SequenceNode:
for i := 0; i < len(node.Content); i++ {
childPath := fmt.Sprintf("%s[%d]", path, i)
didChangeChild, err := walk(node.Content[i], childPath, callback)
err := walk(node.Content[i], childPath, callback)
if err != nil {
return false, err
return err
}
didChange = didChange || didChangeChild
}
case yaml.ScalarNode:
// nothing to do
case yaml.AliasNode:
return false, errors.New("Alias nodes are not supported")
return errors.New("Alias nodes are not supported")
}
return didChange, nil
return nil
}
func yamlMarshal(node *yaml.Node) ([]byte, error) {
func YamlMarshal(node *yaml.Node) ([]byte, error) {
var buffer bytes.Buffer
encoder := yaml.NewEncoder(&buffer)
encoder.SetIndent(2)