Add yaml struct tags to Schema for bidirectional YAML (un)marshaling#192
Open
alecrubin wants to merge 1 commit into
Open
Add yaml struct tags to Schema for bidirectional YAML (un)marshaling#192alecrubin wants to merge 1 commit into
alecrubin wants to merge 1 commit into
Conversation
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 revives #171 with the context that was missing the first time. My follow-up there only went up after the PR had already been closed, so the use case never really got a hearing. Putting it here up front.
What this does
Mirrors the existing
jsonstruct tags on everySchemafield with matchingyamltags. Purely additive: no behavioral change for JSON users, the tags are inert unless a YAML (un)marshaler is actually used.Why a JSON→YAML converter doesn't cover it
The suggestion on #171 was to use a JSON→YAML converter instead. That only solves the write path. The use case here is bidirectional:
yaml.Unmarshalthem back into*jsonschema.Schema. With no yaml tags, the decoder falls back to the lowercased field name, which matches none of the$-prefixed or camelCase keys —$schema,$defs,prefixItems,additionalProperties,patternProperties, etc. A converter can't help on decode; it needs tags on the library's own struct (the only alternative is a parallel mirror struct, which is far more friction than the tags it would replace).Propertiesis a*orderedmap.OrderedMap, and property order is structurally meaningful in JSON Schema. Round-tripping through a genericmap[string]anyconverter flattens it through an unordered map and reorders fields; marshaling straight from the struct keeps order intact.On the "duplicating tags adds friction" concern
It's ~50 tags that mechanically match the existing
jsonnames, added once, with zero ongoing cost — they're ignored entirely by anyone who never imports a YAML package. The friction is a one-time mirror; the capability it unlocks (round-tripping schemas through YAML without losing field order or$-prefixed keys) isn't otherwise reachable without forking the struct.