goformation: Missing struct field if value is 0

I see that some struct fields with 0 as value are not rendered in yaml or json. This is causing problems.

Eg in following code DeviceIndex is missing in final output.

...
import gcf "github.com/awslabs/goformation/cloudformation"
...
			NetworkInterfaces: []gcf.AWSEC2LaunchTemplate_NetworkInterface{
				gcf.AWSEC2LaunchTemplate_NetworkInterface{
					AssociatePublicIpAddress: true,
					DeviceIndex:              0,
					Groups: []string{
						gcf.Ref("SomeSg"),
					},
				},
			},

is rendered to in yaml:

        NetworkInterfaces:
        - AssociatePublicIpAddress: true
          Groups:
          - Ref: SomeSg

or json:

"NetworkInterfaces": [
            {
              "AssociatePublicIpAddress": true,
              "Groups": [
                {
                  "Ref": "SomeSg"
                }
              ]
            }
          ]

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 3
  • Comments: 16 (7 by maintainers)

Most upvoted comments

I agree.

I think this library needs to adopt the approach the AWS Go SDK took, with making field values pointers (so they can represent zero, or not-set/null separately).

Unfortunately this will be a breaking change, and will require the library to migrate from using primitive types directly such as:

subscription := &sns.Subscription{
    Protocol: "email",
    Endpoint: "some.email@example.com",
}

to using pointers (and helper functions) like this:

subscription := &sns.Subscription{
    Protocol: aws.String("email"),
    Endpoint: aws.String("some.email@example.com"),
}

This will be quite a bit of work to implement, and as mentioned will be a breaking change. I’m not sure i’ll be able to start working on this until next year - but would happily accept a pull request for it if somebody else has more time.

@PaulMaddox did you have a chance to look into this? This issue is present in v2.3.0 and is making it impossible to create launch templates using cloud formation.

e.g: https://github.com/awslabs/goformation/blob/v2.3.0/cloudformation/resources/aws-ec2-launchtemplate_networkinterface.go#L27

This should be handled by this merged PR: https://github.com/awslabs/goformation/pull/133/commits/ab8ea0f13025595eb19f634859215a3112d9da1a

However as there are no tests for it, it’s possible this has regressed. I will add some tests and look into what’s going wrong.