aws-cloudformation / rain

A development workflow tool for working with AWS CloudFormation.
Apache License 2.0
750 stars 69 forks source link

Missing Unsorted Flag For Package Command #362

Closed null93 closed 2 months ago

null93 commented 2 months ago

It seems like the pkg command is missing the --unsorted flag. Here is an example of what I mean. Consider test.yaml to have this content:

Metadata:
  Foo: 1
  Bar: 2

When running rain pkg test.yaml it transforms it to this:

Metadata:
  Bar: 2

  Foo: 1

Ideally there would be an option to not sort object keys (similar to the fmt command).

My particular use case is that I store metadata in templates where the position of the keys really matter.

I took a quick look at the source code and couldn't pin point exactly where this would be specified. I can try to submit a PR for this with a little guidance.

ericzbeard commented 2 months ago

It might be in pkg.go where we Decode and Encode the template to resolve anchors. If we're sorting unexpectedly I'd think that's where it's happening. But I would expect a random order for mappings.

null93 commented 2 months ago

I can confirm that it happens after this line: https://github.com/aws-cloudformation/rain/blob/9038611cb314e7ce8d2f8464611d54f91319d893/cft/pkg/pkg.go#L137

null93 commented 2 months ago

Looks like if you change this line, then the order is preserved: https://github.com/aws-cloudformation/rain/blob/9038611cb314e7ce8d2f8464611d54f91319d893/cft/pkg/pkg.go#L126

Here is the very complex diff lol

--- var decoded interface{}
+++ var decoded yaml.Node
ericzbeard commented 2 months ago

Ha ha nice. Honestly I would not have thought of that.

ericzbeard commented 2 months ago

Will you PR this or do you want me to?

null93 commented 2 months ago

I can do it np