feat: preserve field declaration order in JSON output#868
Draft
vivainio wants to merge 4 commits intogoogle:masterfrom
Draft
feat: preserve field declaration order in JSON output#868vivainio wants to merge 4 commits intogoogle:masterfrom
vivainio wants to merge 4 commits intogoogle:masterfrom
Conversation
Objects now emit fields in source declaration order rather than
sorted alphabetically. Extended objects (A + B) emit left fields
first, then new fields from the right. Object comprehensions
preserve insertion order.
Implementation:
- simpleObject gains a fieldOrder []string field
- restrictedObject gains retainedOrder []string
- uncachedObjectFieldsOrder traverses the hierarchy for ordered keys
- manifestJSON returns jsonOrderedObject instead of map[string]interface{}
- serializeJSON and manifestAndSerializeMulti handle jsonOrderedObject
- Native function args flattened via flattenJSONForNative
Fix typo extVat -> extVar in TestExtReset (was masked by old alpha order).
…-order Default behavior reverts to alphabetical sorting (backward compatible with upstream). Pass --preserve-field-order on the CLI or set vm.PreserveFieldOrder = true in the API to get declaration order. The field order tracking in the object model is retained regardless, so switching between modes is cheap (just sort the already-collected keys at serialize time).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This is a DRAFT pull request for information only. See also: https://github.com/vivainio/go-jsonnet-fork
Adds opt-in support for preserving field declaration order in JSON output. The default behavior (alphabetical sorting) is unchanged.
Usage
CLI:
Go API:
Implementation
The object model tracks declaration order throughout:
simpleObjectgains afieldOrder []stringfield populated during AST evaluationrestrictedObjectgainsretainedOrder []stringuncachedObjectFieldsOrdertraverses the object hierarchy to produce ordered field namesA + B), left fields come first, then new fields from the rightmanifestJSONreturns ajsonOrderedObject(keys + fields map) instead ofmap[string]interface{}preserveFieldOrderis setmap[string]interface{}viaflattenJSONForNativeAddresses the requirements raised in #222 and google/jsonnet#407.