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

Preserve map ordering when transforming #13

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

diamondburned
Copy link

This commit changes both JSONToYAML and YAMLToJSON to preserve the map
key ordering when the document is transformed. This is done by
reimplementing the library to use YAML v3's yaml.Node API.

This vastly improves the Go test output. The benchmark is also added for
the next commit which changes the behavior of this library and may
potentially be marginally slower.
@diamondburned
Copy link
Author

Benchmark results:

goos: linux
goarch: amd64
pkg: github.com/invopop/yaml
cpu: AMD Ryzen 5 5500                               
              │ /tmp/old.bench │           /tmp/new.bench            │
              │     sec/op     │   sec/op     vs base                │
JSONToYAML-12      6.656µ ± 4%   5.811µ ± 1%  -12.69% (p=0.000 n=10)
YAMLToJSON-12      4.688µ ± 3%   4.927µ ± 1%   +5.10% (p=0.001 n=10)
geomean            5.585µ        5.351µ        -4.21%

(old = before PR; new = after PR)

This commit changes both JSONToYAML and YAMLToJSON to preserve the map
key ordering when the document is transformed. This is done by
reimplementing the library to use YAML v3's yaml.Node API.
@diamondburned
Copy link
Author

Some of this work was borrowed from @silasdavis and @skipor from PR ghodss/yaml#62.

diamondburned added a commit to diamondburned/kin-openapi that referenced this pull request Aug 12, 2024
This commit adds the `OrderedPropertyKeys` method to the
`openapi3.Schema`:

    OrderedPropertyKeys returns the keys of the properties in the order
    they were defined. This is useful for generating code that needs to
    iterate over the properties in a consistent order. If the keys could
    not be extracted for some reason, then this method automatically
    sorts the keys to be deterministic.

This is done via a temporary fork of the YAML-to-JSON transformation library.
It will not be ready until invopop/yaml#13 is merged.
diamondburned added a commit to diamondburned/kin-openapi that referenced this pull request Aug 12, 2024
This commit adds the `PropertyKeys` property to the type
`openapi3.Schema` which contains the keys of the `Properties` map in the
order that they appear in the original YAML file. This is useful to
guarantee deterministic code generation.

This is done via a temporary fork of the YAML-to-JSON transformation library.
It will not be ready until invopop/yaml#13 is merged.
diamondburned added a commit to diamondburned/kin-openapi that referenced this pull request Aug 13, 2024
This commit adds the `PropertyKeys` property to the type
`openapi3.Schema` which contains the keys of the `Properties` map in the
order that they appear in the original YAML file. This is useful to
guarantee deterministic code generation.

This is done via a temporary fork of the YAML-to-JSON transformation library.
It will not be ready until invopop/yaml#13 is merged.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant