Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix format bug with empty string and null #305

Merged
merged 2 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cft/diff/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ func New(a, b cft.Template) Diff {

func compareValues(old, new interface{}) Diff {
if reflect.TypeOf(old) != reflect.TypeOf(new) {

// In YAML there is no difference between "" and null
if old == "" && new == nil {
return value{new, Unchanged}
}

return value{new, Changed}
}

Expand Down
2 changes: 0 additions & 2 deletions cft/format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ func CheckMultilineBegin(s string) bool {
func String(t cft.Template, opt Options) string {
node := t.Node

// config.Debugf("%v", rnode.ToSJson(node))

buf := strings.Builder{}
enc := yaml.NewEncoder(&buf)
enc.SetIndent(2)
Expand Down
43 changes: 43 additions & 0 deletions cft/format/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -717,3 +717,46 @@ Parameters:
}

}

func TestFnGetAZs(t *testing.T) {
input := `
Resources:
PrivateSubnet1Subnet:
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone: !Select
- 0
- !GetAZs ""
`

// Parse the template
source, err := parse.String(string(input))
if err != nil {
t.Fatal(err)
}

output := format.String(source, format.Options{
JSON: false,
Unsorted: true,
})

// Verify the output is valid
if err = parse.Verify(source, output); err != nil {
t.Fatal(err)
}

expected := `
Resources:
PrivateSubnet1Subnet:
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone: !Select
- 0
- !GetAZs
`

if strings.TrimSpace(output) != strings.TrimSpace(expected) {
t.Fatalf("Got:\n%s\n\nExpected:\n%s", output, expected)
}

}
2 changes: 2 additions & 0 deletions cft/format/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func formatNode(n *yaml.Node) *yaml.Node {
if n.Kind == yaml.MappingNode {
// Does it have just one key/value pair?
if len(n.Content) == 2 {

// Is the key relevant?
for tag, funcName := range cft.Tags {
if n.Content[0].Value == funcName {
Expand Down Expand Up @@ -85,6 +86,7 @@ func formatNode(n *yaml.Node) *yaml.Node {
n.Style = newNode.Content[0].Style
}
}

}
}

Expand Down
13 changes: 13 additions & 0 deletions test/templates/getazs-null.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Parameters:
VpcId:
Type: String

Resources:

PrivateSubnet1Subnet:
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone: !Select
- 0
- !GetAZs ""
VpcId: !Ref VpcId
Loading