Skip to content

Options style - #34

Merged
fredbi merged 5 commits into
go-openapi:masterfrom
fredbi:options-style
Aug 23, 2026
Merged

Options style#34
fredbi merged 5 commits into
go-openapi:masterfrom
fredbi:options-style

Conversation

@fredbi

@fredbi fredbi commented Aug 23, 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 5 commits August 23, 2026 22:16
…testing

Every option type took a pointer and returned nothing, so the options struct
escaped to the heap and each package read a little differently from the next.
They now chain by value, as go-openapi/core does:

	type (
		Option func(options) options

		options struct{ ... }
	)

	func WithX(v T) Option {
		return func(o options) options {
			o.x = v

			return o
		}
	}

applyWithDefaults folds the chain left to right. Where a package declares more
than one option type, the helper carries it: applyModWithDefaults,
applyTidyWithDefaults. Those two keep returning an error, which comes from
validating the assembled options rather than from any single option.

A default that is not a zero value stays seeded inside the helper. Nothing
declares an empty defaultOptions to match a package that has real defaults.

WithResolvedImports and WithBuildFlags write into a map and a slice the copy
shares, which stays correct because each chain starts from its own zero value.
An option that appended to a seeded slice would not: two chains would write
into one backing array, and into the seed itself.

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

gofumpt's Extra.Set clears itself before reading a rule list, and
WithExtraRules called it once per name, so only the last one survived:
WithExtraRules("group_params", "clothe_returns") turned group_params back off.

Pass the names to Set the way gofumpt's -extra flag takes them, comma
separated, in a single call.

The tests only ever named one rule, which is why this held. The new one names
two and asserts the first survives the second.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
WithExtraRules rejects a rule gofumpt does not know, and a chain that passes
values has nowhere to put that error. It goes in an options struct of our own,
wrapping fumpt.Options beside the first failure:

	type options struct {
		fumpt fumpt.Options
		err   error
	}

withError keeps the first failure and lets the rest of the chain run, and
Configure reads it before touching the package settings, so a failing option
still leaves the previous rules in place.

fumpt.Options holds no pointer - Extra is three bools - so the value the chain
carries is a plain copy.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
Option, SourceOption and DumpOption took a pointer and returned an error, so
the chain stopped at the first failure and the options struct escaped to the
heap. They now pass values, and the failure travels in the struct:

	func (o options) withError(err error) options {
		if o.err == nil {
			o.err = err
		}

		return o
	}

WithExtensions, WithRoots, WithExtraRoots, WithCoverage, Rebased and the four
From* constructors record their first bad argument there. New, Clone and Dump
read it before using the settings, so a caller sees the same error from the
same call as before. The options after a failing one now run, and none of them
touches anything outside the options value.

The defaults stay inside makeOptions and applyDumpWithDefaults rather than
moving to a package-level value: they hold a FuncMap and a slice, and building
them afresh per call is what keeps two repositories from sharing either.

For the same reason WithExtraRoots and the From* constructors clone the slice
they extend instead of appending in place, which would write into the backing
array of whichever chain got there first.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
Option in the repository package states where a rejected argument surfaces,
and DumpOption did not. It behaves the same way: the option records the
failure and Dump returns it.

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

codecov Bot commented Aug 23, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 81.67539% with 35 lines in your changes missing coverage. Please review.
✅ Project coverage is 79.69%. Comparing base (fbd6116) to head (45259d2).
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
templates-repo/sources.go 63.63% 12 Missing ⚠️
formatting/resolve/resolve.go 41.66% 7 Missing ⚠️
gentesting/testutils.go 0.00% 7 Missing ⚠️
formatting/enable/gofumpt/enable.go 87.50% 3 Missing ⚠️
genapp/options.go 85.71% 2 Missing ⚠️
genapp/tidy_options.go 87.50% 2 Missing ⚠️
formatting/options.go 92.85% 1 Missing ⚠️
templates-repo/repository.go 66.66% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master      #34      +/-   ##
==========================================
- Coverage   79.69%   79.69%   -0.01%     
==========================================
  Files          79       79              
  Lines        4798     4861      +63     
==========================================
+ Hits         3824     3874      +50     
- Misses        973      986      +13     
  Partials        1        1              

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

@fredbi
fredbi merged commit 4b06caf into go-openapi:master Aug 23, 2026
22 checks passed
@fredbi
fredbi deleted the options-style branch August 23, 2026 21:25
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