Support multiple aliases for a field#339
Open
frenck wants to merge 1 commit into
Open
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #339 +/- ##
=======================================
Coverage 99.92% 99.92%
=======================================
Files 101 101
Lines 12062 12140 +78
=======================================
+ Hits 12053 12131 +78
Misses 9 9 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
A field can now declare more than one alias, via a sequence in the field `alias` option, a sequence value in the `aliases` config, or several `Alias` annotations. On deserialization the aliases are tried in order and the first key present in the input wins; serialization and JSON schema use the first (primary) alias. The per-field decode logic now builds an ordered, de-duplicated list of keys to look up, which also collapses the two near-duplicate alias branches in the field unpacker into a single path. Generated code for fields without multiple aliases is unchanged. Closes Fatal1ty#176
2400aa4 to
31b756d
Compare
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.
Support multiple aliases for a field
Closes #176.
Lets a field declare more than one alias. On deserialization the aliases are tried in order and the first key present in the input wins; serialization and JSON schema use the first (primary) alias. A single string alias keeps working exactly as before.
Three ways to declare them, matching the API discussed in the issue:
The
aliasfield option now acceptsstr | Sequence[str], thealiasesconfig acceptsdict[str, str | Sequence[str]], and a field may carry severalAliasannotations. Whenallow_deserialization_not_by_aliasis set, the field name is tried after the aliases.forbid_extra_keysaccepts every alias.Internally the per-field decode logic now builds an ordered, de-duplicated list of keys to look up, which also collapsed the two near-duplicate alias branches in the field unpacker into a single path. Generated code for fields without multiple aliases is unchanged (verified byte-for-byte against the previous output), so this is purely additive for existing users.
Design notes
A few choices worth your eye:
Alias(...)annotations on a single field. The issue only mentioned the field option and the config option, so this extends the annotation path for consistency.Tests cover all three declaration styles, deserialization fallback order,
allow_deserialization_not_by_alias,forbid_extra_keys, serialization using the primary alias, JSON schema, and the field-name-as-alias dedup case. README updated.../Frenck
Blogging my personal ramblings at frenck.dev