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

@@ -11,10 +11,12 @@ import (
// CopyComments recursively copies the comments on fields in from to fields in to // CopyComments recursively copies the comments on fields in from to fields in to
func CopyComments(from, to *yaml.RNode) error { func CopyComments(from, to *yaml.RNode) error {
copy(from, to) // from node should not be modified, it should be just used as a reference
fromCopy := from.Copy()
copyFieldComments(fromCopy, to)
// walk the fields copying comments // walk the fields copying comments
_, err := walk.Walker{ _, err := walk.Walker{
Sources: []*yaml.RNode{from, to}, Sources: []*yaml.RNode{fromCopy, to},
Visitor: &copier{}, Visitor: &copier{},
VisitKeysAsScalars: true}.Walk() VisitKeysAsScalars: true}.Walk()
return err return err
@@ -25,7 +27,7 @@ func CopyComments(from, to *yaml.RNode) error {
type copier struct{} type copier struct{}
func (c *copier) VisitMap(s walk.Sources, _ *openapi.ResourceSchema) (*yaml.RNode, error) { func (c *copier) VisitMap(s walk.Sources, _ *openapi.ResourceSchema) (*yaml.RNode, error) {
copy(s.Dest(), s.Origin()) copyFieldComments(s.Dest(), s.Origin())
return s.Dest(), nil return s.Dest(), nil
} }
@@ -39,13 +41,13 @@ func (c *copier) VisitScalar(s walk.Sources, _ *openapi.ResourceSchema) (*yaml.R
to.Document().Style = yaml.DoubleQuotedStyle to.Document().Style = yaml.DoubleQuotedStyle
} }
copy(s.Dest(), to) copyFieldComments(s.Dest(), to)
return s.Dest(), nil return s.Dest(), nil
} }
func (c *copier) VisitList(s walk.Sources, _ *openapi.ResourceSchema, _ walk.ListKind) ( func (c *copier) VisitList(s walk.Sources, _ *openapi.ResourceSchema, _ walk.ListKind) (
*yaml.RNode, error) { *yaml.RNode, error) {
copy(s.Dest(), s.Origin()) copyFieldComments(s.Dest(), s.Origin())
destItems := s.Dest().Content() destItems := s.Dest().Content()
originItems := s.Origin().Content() originItems := s.Origin().Content()
@@ -64,8 +66,8 @@ func (c *copier) VisitList(s walk.Sources, _ *openapi.ResourceSchema, _ walk.Lis
return s.Dest(), nil return s.Dest(), nil
} }
// copy copies the comment from one field to another // copyFieldComments copies the comment from one field to another
func copy(from, to *yaml.RNode) { func copyFieldComments(from, to *yaml.RNode) {
if from == nil || to == nil { if from == nil || to == nil {
return return
} }

View File

@@ -351,6 +351,30 @@ apiVersion: v1
kind: ConfigMap kind: ConfigMap
data: data:
somekey: "012345678901234567890123456789012345678901234567890123456789012345678901234" # x somekey: "012345678901234567890123456789012345678901234567890123456789012345678901234" # x
`,
},
{
name: "sort fields with null value",
from: `apiVersion: v1
kind: ConfigMap
metadata:
name: workspaces.app.terraform.io
creationTimestamp: null # this field is null
namespace: staging
`,
to: `apiVersion: v1
kind: ConfigMap
metadata:
name: workspaces.app.terraform.io
creationTimestamp: null
namespace: staging
`,
expected: `apiVersion: v1
kind: ConfigMap
metadata:
name: workspaces.app.terraform.io
creationTimestamp: null # this field is null
namespace: staging
`, `,
}, },
} }
@@ -378,9 +402,18 @@ data:
t.FailNow() t.FailNow()
} }
actualFrom, err := from.String()
if !assert.NoError(t, err) {
t.FailNow()
}
if !assert.Equal(t, strings.TrimSpace(tc.expected), strings.TrimSpace(actual)) { if !assert.Equal(t, strings.TrimSpace(tc.expected), strings.TrimSpace(actual)) {
t.FailNow() t.FailNow()
} }
if !assert.Equal(t, strings.TrimSpace(tc.from), strings.TrimSpace(actualFrom)) {
t.FailNow()
}
}) })
} }
} }

View File

@@ -13,7 +13,9 @@ import (
// Field order might be altered due to round-tripping in arbitrary functions. // 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. // This functionality helps to retain the original order of fields to avoid unnecessary diffs.
func SyncOrder(from, to *yaml.RNode) error { 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()) return errors.Errorf("failed to sync field order: %q", err.Error())
} }
rearrangeHeadCommentOfSeqNode(to.YNode()) rearrangeHeadCommentOfSeqNode(to.YNode())

View File

@@ -5,6 +5,7 @@ package order
import ( import (
"bytes" "bytes"
"strings"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@@ -413,6 +414,15 @@ status:
if !assert.Equal(t, tc.expected, out.String()) { if !assert.Equal(t, tc.expected, out.String()) {
t.FailNow() t.FailNow()
} }
actualFrom, err := from.String()
if !assert.NoError(t, err) {
t.FailNow()
}
if !assert.Equal(t, strings.TrimSpace(tc.from), strings.TrimSpace(actualFrom)) {
t.FailNow()
}
}) })
} }
} }