Skip to content

codegen w/ go version guards - ErrorAsType (go1.26)#132

Merged
fredbi merged 4 commits into
go-openapi:masterfrom
fredbi:worktree-go-version-guards
Jun 22, 2026
Merged

codegen w/ go version guards - ErrorAsType (go1.26)#132
fredbi merged 4 commits into
go-openapi:masterfrom
fredbi:worktree-go-version-guards

Conversation

@fredbi

@fredbi fredbi commented Jun 22, 2026

Copy link
Copy Markdown
Member

Change type

Please select: 🆕 New feature or enhancement|🔧 Bug fix'|📃 Documentation update

Short description

Fixes

Full description

Checklist

  • I have signed all my commits with my name and email (see DCO. This does not require a PGP-signed commit
  • I have rebased and squashed my work, so only one commit remains
  • I have added tests to cover my changes.
  • I have properly enriched go doc comments in code.
  • I have properly documented any breaking change.

fredbi and others added 4 commits June 22, 2026 16:43
Add the infrastructure for assertions guarded by a //go:build go1.N constraint,
so the library can offer functionality that requires a newer Go toolchain while
still building on oldstable (currently go1.25). No guarded assertion exists yet;
this is the prerequisite mechanism.

Codegen:
- scanner: detect each function's source-file //go:build constraint (via
  go/build/constraint) and record it on model.Function.GoBuild.
- model: GoBuildTag, Functions.BuildVariants, AssertionPackage.BuildConstraint.
- generator: partition functions by constraint and render a parallel set of files
  suffixed _go<N> carrying the //go:build line; render() skips/removes files with
  no declarations; a shared header.gotmpl emits the guard; file-level boilerplate
  (Assertions/New, forwardArgs, mock types, shared test fixtures) is gated to the
  default partition so guarded files reference it instead of redeclaring it.
- orphan cleanup: sweepOrphanVariants removes generated *_go<N>* files whose
  variant no longer exists, but only marker-bearing, non-hand-authored files,
  logging every removal.
- safety rail: go.work carries a toolchain floor (go1.26.0) so codegen, run in
  workspace mode, always observes guarded files; TestToolchainFloorCoversGuards
  asserts the highest guard does not exceed the floor. The floor is dev-only and
  not published, so consumers on older toolchains are unaffected.

Doc site (Hugo):
- new goversion shortcode renders an explicit "minimum Go version" pill (styled in
  custom-header.html); domain pages and the quick index gain the conditional
  plumbing (Document.HasGoGuards, QuickIndexEntry.GoBuild) to show it.

Because no assertion is guarded yet, regenerated output is byte-identical to the
current generated packages and docs.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
Add ErrorAsType and its opposite NotErrorAsType, guarded behind //go:build go1.26
in internal/assertions/error_go126.go and built on go1.26 errors.AsType[E]. These
are the first assertions to use the codegen go-version guard mechanism.

Design (keeping the assertion conventions intact):
- Assertions return bool, not the upstream errors.AsType (E, bool) shape. The
  matched error is delivered through a typed target *E (the type-safe counterpart
  of ErrorAs's untyped any target).
- target is nilable: pass &got to capture the matched error (E inferred), or nil to
  only check, supplying E explicitly e.g. ErrorAsType[*json.SyntaxError](t, err, nil).
- Because E is inferable from target *E, these are ordinary inferable generics: no
  GenericSuffix special-case and no codegen changes; the //go:build go1.26 guard
  flows through the generator and into the doc site (error domain gains the
  go1.26 badge) automatically.

The nil (check-only) branch, which the example-driven generated tests cannot
exercise, is covered by a hand-written guarded test (error_go126_test.go).

Refers to stretchr#1860 (adapted: typed *E target with bool return).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
- CHANGES.md: document ErrorAsType / NotErrorAsType in the Error domain (go1.26
  generics, adapted from upstream stretchr#1860 with a typed *E target and bool return).
- TRACKING.md: move stretchr#1860 from "Monitoring" to "Implemented"; add it to the
  highlight list and the appendix table; bump summary stats (implemented 27->28,
  monitoring 2->1).
- ROADMAP.md: mark v2.6 (June 2026) done and drop the "tentative" tag; add a
  Q3 2026 section with a tentative v2.7 (Sep 2026) for generic forward methods
  (go1.27+); refresh the processed-items stat line to match TRACKING.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
@codecov

codecov Bot commented Jun 22, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 86.13861% with 28 lines in your changes missing coverage. Please review.
✅ Project coverage is 91.62%. Comparing base (d3fde91) to head (f490f00).
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
codegen/internal/generator/generator.go 77.94% 7 Missing and 8 partials ⚠️
codegen/internal/scanner/scanner.go 60.00% 2 Missing and 2 partials ⚠️
internal/assertions/error_go126.go 88.88% 2 Missing and 1 partial ⚠️
codegen/internal/generator/render.go 83.33% 1 Missing and 1 partial ⚠️
codegen/internal/model/documentation.go 71.42% 1 Missing and 1 partial ⚠️
codegen/internal/scanner/buildtags.go 88.88% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #132      +/-   ##
==========================================
- Coverage   91.70%   91.62%   -0.08%     
==========================================
  Files          97      103       +6     
  Lines       12651    12834     +183     
==========================================
+ Hits        11601    11759     +158     
- Misses        827      840      +13     
- Partials      223      235      +12     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@fredbi fredbi merged commit 4c0e1d8 into go-openapi:master Jun 22, 2026
24 checks passed
@fredbi fredbi deleted the worktree-go-version-guards branch June 22, 2026 15:11
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.

1 participant