Skip to content

feat: package union, what generated code decodes unions and tagged structs with - #4

Merged
MarkRosemaker merged 1 commit into
masterfrom
feat/union
Nov 12, 2026
Merged

MarkRosemaker merged 1 commit into
masterfrom
feat/union

Conversation

@MarkRosemaker

Copy link
Copy Markdown
Contributor

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.

Group Names
Errors Strict, UnknownName, Missing, UnknownValue
Reading members FirstMember, MembersFrom, Part/PartsFrom
Rewriting objects First, MemberString, Members, Select, Merge
Choosing an alternative Variant, Fits, EnumsFit, EnumPasses, ChooseVariants
Tagged structs TagNeed, TagOptional/TagRequired/TagRequiredOrNull, CheckTag, InferTag

Performance 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.go covers 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 a bytes.Buffer can't produce.
  • The module as a whole is at 90.8%, and make ready passes.
  • openapi-codegen generated with these helpers, for notion, pixellab, habitica, go-pkgsite, themoviedb, toggl and proton: all build and pass their tests.

The openapi-codegen change that uses this follows once MarkRosemaker/openapi-codegen#69 merges. It's ready on its feat/tag-enums-tested branch.


Generated by Claude Code

…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
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.

2 participants