Repository navigation
feat: package union, what generated code decodes unions and tagged structs with - #4
Merged
Merged
Conversation
…ructs with openapi-codegen writes the same JSON helpers into every library it generates: choosing a union's alternative, reading a discriminator first, checking a tagged struct's tag. They move here, unchanged but for their exported names, with tests of their own. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019QCb1AfnATvpBQCH3PrX7Y
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.
Why
openapi-codegen writes the same JSON helpers into every library that has unions or tagged structs. They choose a union's alternative, read a discriminator first, and check a tagged struct's tag. That's about 370 statements per library, a third of them untested in go-api-libs/notion, and repeated in every repository. Like
DecodeLenient, they belong here, written and tested once.Change
New package
union. The helpers are moved unchanged, with exported names and exported struct fields.Strict,UnknownName,Missing,UnknownValueFirstMember,MembersFrom,Part/PartsFromFirst,MemberString,Members,Select,MergeVariant,Fits,EnumsFit,EnumPasses,ChooseVariantsTagNeed,TagOptional/TagRequired/TagRequiredOrNull,CheckTag,InferTagPerformance is unchanged. They were separate functions in the generated code already, and Go still inlines small functions across packages. The package doc says it serves generated code and may change as the generator needs.
Tests
union_test.gocovers each function's paths: ordering, leniency, merge conflicts, enums, oneOf vs anyOf, and tag checks when decoding and encoding. Package coverage is 90.7%; what's left is mostly encoder write errors that abytes.Buffercan't produce.make readypasses.The openapi-codegen change that uses this follows once MarkRosemaker/openapi-codegen#69 merges. It's ready on its
feat/tag-enums-testedbranch.Generated by Claude Code