Copy reference nodes before copying comments and syncing order

This commit is contained in:
Phani Teja Marupaka
2021-09-07 16:58:06 -07:00
parent 99e404cb61
commit 17f18604e4
4 changed files with 55 additions and 8 deletions

View File

@@ -13,7 +13,9 @@ import (
// Field order might be altered due to round-tripping in arbitrary functions.
// This functionality helps to retain the original order of fields to avoid unnecessary diffs.
func SyncOrder(from, to *yaml.RNode) error {
if err := syncOrder(from, to); err != nil {
// from node should not be modified, it should be just used as a reference
fromCopy := from.Copy()
if err := syncOrder(fromCopy, to); err != nil {
return errors.Errorf("failed to sync field order: %q", err.Error())
}
rearrangeHeadCommentOfSeqNode(to.YNode())