diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 42b7b4c..78893bf 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -8,10 +8,10 @@ jobs: lint: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 - - uses: golangci/golangci-lint-action@v2 + - uses: actions/checkout@v4 + - uses: golangci/golangci-lint-action@v3 with: - version: v1.53.3 + version: v1.54.2 build: name: Build on golang ${{ matrix.go_version }} and ${{ matrix.os }} @@ -21,17 +21,18 @@ jobs: go_version: - "1.19" - "1.20" + - "1.21" os: [ ubuntu-latest, macOS-latest ] steps: - name: Set up Go ${{ matrix.go_version }} - uses: actions/setup-go@v1 + uses: actions/setup-go@v5 with: go-version: ${{ matrix.go_version }} id: go - name: Check out code into the Go module directory - uses: actions/checkout@v1 + uses: actions/checkout@v4 - name: Get dependencies run: | diff --git a/Taskfile.yml b/Taskfile.yml index 257b8f2..535759d 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -11,7 +11,7 @@ vars: TOOL_BIN_DIR: ./bin/tools - TOOL_LINT_SRC: github.com/golangci/golangci-lint/cmd/golangci-lint@v1.53.3 + TOOL_LINT_SRC: github.com/golangci/golangci-lint/cmd/golangci-lint@v1.54.2 TOOL_LINT: ./{{ .TOOL_BIN_DIR }}/golangci-lint tasks: @@ -81,6 +81,7 @@ tasks: - echo "- Update examples" - task: examples:update:go-generate - task: examples:update:go-generate-generics + - task: examples:update:go-generate-2options-1pkg - task: examples:update:library examples:update:library: @@ -100,3 +101,9 @@ tasks: cmds: - task: "install" - go generate ./... + + examples:update:go-generate-2options-1pkg: + dir: examples/go-generate-2options-1pkg + cmds: + - task: "install" + - go generate ./... diff --git a/cmd/options-gen/main.go b/cmd/options-gen/main.go index c241eed..ad77dde 100644 --- a/cmd/options-gen/main.go +++ b/cmd/options-gen/main.go @@ -17,6 +17,7 @@ func main() { outFilename string optionsStructName string outPackageName string + outPrefix string defaultsFrom string muteWarnings bool ) @@ -44,6 +45,10 @@ func main() { flag.BoolVar(&muteWarnings, "mute-warnings", false, "mute all warnings") + flag.StringVar(&outPrefix, + "out-prefix", "", + "prefix for generated structs and functions. It is like namespace that can be used in case "+ + "when you have a several options structs in one package") flag.Parse() if isEmpty(inFilename, outFilename, outPackageName, optionsStructName, defaultsFrom) { @@ -67,6 +72,7 @@ func main() { outFilename, optionsStructName, outPackageName, + outPrefix, *defaults, !muteWarnings, ) diff --git a/examples/go-generate-2options-1pkg/README.md b/examples/go-generate-2options-1pkg/README.md new file mode 100644 index 0000000..446f097 --- /dev/null +++ b/examples/go-generate-2options-1pkg/README.md @@ -0,0 +1,13 @@ +## CLI example + +Code you write is - `client.go` and `options.go`. To +generate `options_generated.go` you can just `go generate` like that: + +```bash +go install github.com/kazhuravlev/options-gen/cmd/options-gen@latest + +git clone git@github.com:kazhuravlev/options-gen.git +cd options-gen/examples/go-generate-2options-1pkg + +go generate ./... +``` diff --git a/examples/go-generate-2options-1pkg/client.go b/examples/go-generate-2options-1pkg/client.go new file mode 100644 index 0000000..401b180 --- /dev/null +++ b/examples/go-generate-2options-1pkg/client.go @@ -0,0 +1,25 @@ +package gogenerate + +import ( + "fmt" +) + +type Client struct { + opts1 Options1 + opts2 Options2 +} + +func New(opts1 Options1, opts2 Options2) (*Client, error) { + if err := opts1.Validate(); err != nil { + return nil, fmt.Errorf("bad configuration opts1: %w", err) + } + + if err := opts2.Validate(); err != nil { + return nil, fmt.Errorf("bad configuration opts2: %w", err) + } + + return &Client{ + opts1: opts1, + opts2: opts2, + }, nil +} diff --git a/examples/go-generate-2options-1pkg/options.go b/examples/go-generate-2options-1pkg/options.go new file mode 100644 index 0000000..82d3a01 --- /dev/null +++ b/examples/go-generate-2options-1pkg/options.go @@ -0,0 +1,39 @@ +package gogenerate + +//go:generate options-gen -from-struct=Options1 -out-prefix=KKK -out-filename=options1_generated.go -defaults-from=var=defaultOptions1 +type Options1 struct { + // Options1.field0 + field0 int `validate:"min:3"` + // Options1.field1 + field1 int `validate:"min:3"` + // Options1.field2 + field2 int `validate:"min:3"` + // Options1.field3 + field3 int `validate:"min:3"` +} + +var defaultOptions1 = Options1{ + field0: 0, + field1: 1, + field2: 2, + field3: 3, +} + +//go:generate options-gen -from-struct=Options2 -out-prefix=NNN -out-filename=options2_generated.go -defaults-from=var=defaultOptions2 +type Options2 struct { + // Options2.field1 + field1 int `validate:"min:3"` + // Options2.field2 + field2 int `validate:"min:3"` + // Options2.field3 + field3 int `validate:"min:3"` + // Options2.field4 + field4 int `validate:"min:3"` +} + +var defaultOptions2 = Options2{ + field1: 1, + field2: 2, + field3: 3, + field4: 4, +} diff --git a/examples/go-generate-2options-1pkg/options1_generated.go b/examples/go-generate-2options-1pkg/options1_generated.go new file mode 100644 index 0000000..38c929a --- /dev/null +++ b/examples/go-generate-2options-1pkg/options1_generated.go @@ -0,0 +1,93 @@ +// Code generated by options-gen. DO NOT EDIT. +package gogenerate + +import ( + fmt461e464ebed9 "fmt" + + errors461e464ebed9 "github.com/kazhuravlev/options-gen/pkg/errors" + validator461e464ebed9 "github.com/kazhuravlev/options-gen/pkg/validator" +) + +type OptOptions1Setter func(o *Options1) + +func NewOptions1( + options ...OptOptions1Setter, +) Options1 { + o := Options1{} + + // Setting defaults from variable + o.field0 = defaultOptions1.field0 + o.field1 = defaultOptions1.field1 + o.field2 = defaultOptions1.field2 + o.field3 = defaultOptions1.field3 + + for _, opt := range options { + opt(&o) + } + return o +} + +// Options1.field0 +func WithKKKField0(opt int) OptOptions1Setter { + return func(o *Options1) { + o.field0 = opt + } +} + +// Options1.field1 +func WithKKKField1(opt int) OptOptions1Setter { + return func(o *Options1) { + o.field1 = opt + } +} + +// Options1.field2 +func WithKKKField2(opt int) OptOptions1Setter { + return func(o *Options1) { + o.field2 = opt + } +} + +// Options1.field3 +func WithKKKField3(opt int) OptOptions1Setter { + return func(o *Options1) { + o.field3 = opt + } +} + +func (o *Options1) Validate() error { + errs := new(errors461e464ebed9.ValidationErrors) + errs.Add(errors461e464ebed9.NewValidationError("field0", _validate_Options1_field0(o))) + errs.Add(errors461e464ebed9.NewValidationError("field1", _validate_Options1_field1(o))) + errs.Add(errors461e464ebed9.NewValidationError("field2", _validate_Options1_field2(o))) + errs.Add(errors461e464ebed9.NewValidationError("field3", _validate_Options1_field3(o))) + return errs.AsError() +} + +func _validate_Options1_field0(o *Options1) error { + if err := validator461e464ebed9.GetValidatorFor(o).Var(o.field0, "min:3"); err != nil { + return fmt461e464ebed9.Errorf("field `field0` did not pass the test: %w", err) + } + return nil +} + +func _validate_Options1_field1(o *Options1) error { + if err := validator461e464ebed9.GetValidatorFor(o).Var(o.field1, "min:3"); err != nil { + return fmt461e464ebed9.Errorf("field `field1` did not pass the test: %w", err) + } + return nil +} + +func _validate_Options1_field2(o *Options1) error { + if err := validator461e464ebed9.GetValidatorFor(o).Var(o.field2, "min:3"); err != nil { + return fmt461e464ebed9.Errorf("field `field2` did not pass the test: %w", err) + } + return nil +} + +func _validate_Options1_field3(o *Options1) error { + if err := validator461e464ebed9.GetValidatorFor(o).Var(o.field3, "min:3"); err != nil { + return fmt461e464ebed9.Errorf("field `field3` did not pass the test: %w", err) + } + return nil +} diff --git a/examples/go-generate-2options-1pkg/options2_generated.go b/examples/go-generate-2options-1pkg/options2_generated.go new file mode 100644 index 0000000..3f398f1 --- /dev/null +++ b/examples/go-generate-2options-1pkg/options2_generated.go @@ -0,0 +1,93 @@ +// Code generated by options-gen. DO NOT EDIT. +package gogenerate + +import ( + fmt461e464ebed9 "fmt" + + errors461e464ebed9 "github.com/kazhuravlev/options-gen/pkg/errors" + validator461e464ebed9 "github.com/kazhuravlev/options-gen/pkg/validator" +) + +type OptOptions2Setter func(o *Options2) + +func NewOptions2( + options ...OptOptions2Setter, +) Options2 { + o := Options2{} + + // Setting defaults from variable + o.field1 = defaultOptions2.field1 + o.field2 = defaultOptions2.field2 + o.field3 = defaultOptions2.field3 + o.field4 = defaultOptions2.field4 + + for _, opt := range options { + opt(&o) + } + return o +} + +// Options2.field1 +func WithNNNField1(opt int) OptOptions2Setter { + return func(o *Options2) { + o.field1 = opt + } +} + +// Options2.field2 +func WithNNNField2(opt int) OptOptions2Setter { + return func(o *Options2) { + o.field2 = opt + } +} + +// Options2.field3 +func WithNNNField3(opt int) OptOptions2Setter { + return func(o *Options2) { + o.field3 = opt + } +} + +// Options2.field4 +func WithNNNField4(opt int) OptOptions2Setter { + return func(o *Options2) { + o.field4 = opt + } +} + +func (o *Options2) Validate() error { + errs := new(errors461e464ebed9.ValidationErrors) + errs.Add(errors461e464ebed9.NewValidationError("field1", _validate_Options2_field1(o))) + errs.Add(errors461e464ebed9.NewValidationError("field2", _validate_Options2_field2(o))) + errs.Add(errors461e464ebed9.NewValidationError("field3", _validate_Options2_field3(o))) + errs.Add(errors461e464ebed9.NewValidationError("field4", _validate_Options2_field4(o))) + return errs.AsError() +} + +func _validate_Options2_field1(o *Options2) error { + if err := validator461e464ebed9.GetValidatorFor(o).Var(o.field1, "min:3"); err != nil { + return fmt461e464ebed9.Errorf("field `field1` did not pass the test: %w", err) + } + return nil +} + +func _validate_Options2_field2(o *Options2) error { + if err := validator461e464ebed9.GetValidatorFor(o).Var(o.field2, "min:3"); err != nil { + return fmt461e464ebed9.Errorf("field `field2` did not pass the test: %w", err) + } + return nil +} + +func _validate_Options2_field3(o *Options2) error { + if err := validator461e464ebed9.GetValidatorFor(o).Var(o.field3, "min:3"); err != nil { + return fmt461e464ebed9.Errorf("field `field3` did not pass the test: %w", err) + } + return nil +} + +func _validate_Options2_field4(o *Options2) error { + if err := validator461e464ebed9.GetValidatorFor(o).Var(o.field4, "min:3"); err != nil { + return fmt461e464ebed9.Errorf("field `field4` did not pass the test: %w", err) + } + return nil +} diff --git a/examples/library/example_out_config.go b/examples/library/example_out_config.go index 14ed38f..ff43c7b 100644 --- a/examples/library/example_out_config.go +++ b/examples/library/example_out_config.go @@ -16,7 +16,7 @@ func NewConfig( return o } -func WithName(opt string) OptConfigSetter { +func WithSomeName(opt string) OptConfigSetter { return func(o *Config) { o.name = opt } diff --git a/examples/library/example_out_options.go b/examples/library/example_out_options.go index 3607bad..7c80b74 100644 --- a/examples/library/example_out_options.go +++ b/examples/library/example_out_options.go @@ -29,7 +29,7 @@ func NewOptions( return o } -func WithPort(opt int) OptOptionsSetter { +func WithSomePort(opt int) OptOptionsSetter { return func(o *Options) { o.port = opt } diff --git a/examples/library/main.go b/examples/library/main.go index e300ab5..d493785 100644 --- a/examples/library/main.go +++ b/examples/library/main.go @@ -27,6 +27,7 @@ func main() { params.outFname, params.structName, "main", + "Some", optionsgen.Defaults{From: optionsgen.DefaultsFromTag, Param: ""}, true, ); err != nil { diff --git a/internal/generator/generator.go b/internal/generator/generator.go index 80d1470..34d66a1 100644 --- a/internal/generator/generator.go +++ b/internal/generator/generator.go @@ -57,7 +57,7 @@ func RenderOptions( packageName, optionsStructName string, fileImports []string, spec *OptionSpec, - tagName, varName, funcName string, + tagName, varName, funcName, prefix string, ) ([]byte, error) { tmpl := template.Must(template.ParseFS(templates, "templates/options.go.tpl")) @@ -78,6 +78,7 @@ func RenderOptions( "optionsTypeParamsSpec": spec.TypeParamsSpec, "optionsTypeParams": spec.TypeParams, + "optionsPrefix": prefix, "optionsStructName": optionsStructName, "optionsStructType": optionsStructType, "optionsStructInstanceType": optionsStructInstanceType, diff --git a/internal/generator/generator_test.go b/internal/generator/generator_test.go index fe33d40..413030d 100644 --- a/internal/generator/generator_test.go +++ b/internal/generator/generator_test.go @@ -70,12 +70,14 @@ func TestGetOptionSpec(t *testing.T) { //nolint:funlen }, { Name: "BoolTrue", + Docstring: "", Field: "boolTrue", Type: "bool", TagOption: generator.TagOption{IsRequired: false, GoValidator: "", Default: "true"}, }, { Name: "BoolFalse", + Docstring: "", Field: "boolFalse", Type: "bool", TagOption: generator.TagOption{IsRequired: false, GoValidator: "", Default: "false"}, diff --git a/internal/generator/templates/options.go.tpl b/internal/generator/templates/options.go.tpl index 048550d..b428ccc 100644 --- a/internal/generator/templates/options.go.tpl +++ b/internal/generator/templates/options.go.tpl @@ -64,7 +64,7 @@ func New{{ .optionsStructType }}( {{- if ne .Docstring "" -}} {{ .Docstring }} {{- end }} - func With{{ .Name }}{{ $.optionsTypeParamsSpec }}(opt {{ .Type }}) Opt{{ $.optionsStructName }}Setter{{ $.optionsTypeParams }} { + func With{{$.optionsPrefix}}{{ .Name }}{{ $.optionsTypeParamsSpec }}(opt {{ .Type }}) Opt{{ $.optionsStructName }}Setter{{ $.optionsTypeParams }} { return func(o *{{ $.optionsStructInstanceType }}) { o.{{ .Field }} = opt } diff --git a/options-gen/cmd.go b/options-gen/cmd.go index 199a0bf..9b151c9 100644 --- a/options-gen/cmd.go +++ b/options-gen/cmd.go @@ -4,6 +4,7 @@ import ( "fmt" "log" "os" + "strings" "github.com/kazhuravlev/options-gen/internal/generator" ) @@ -23,10 +24,16 @@ type Defaults struct { Param string `json:"param"` } -func Run(inFilename, outFilename, structName, packageName string, defaults Defaults, showWarnings bool) error { +func Run( + inFilename, outFilename, structName, packageName, outPrefix string, + defaults Defaults, + showWarnings bool, +) error { // парсим исходный файл так, что бы получить не только структуру, но и токены, связанные с defaults. // то есть defaults это модификатор парсинга, который заставит парсер вытащить доп инфу + outPrefix = strings.TrimSpace(outPrefix) + var tagName, varName, funcName string switch defaults.From { case DefaultsFromNone: @@ -57,7 +64,12 @@ func Run(inFilename, outFilename, structName, packageName string, defaults Defau return fmt.Errorf("cannot get imports: %w", err) } - res, err := generator.RenderOptions(packageName, structName, imports, optionSpec, tagName, varName, funcName) + res, err := generator.RenderOptions( + packageName, structName, imports, + optionSpec, + tagName, varName, funcName, + outPrefix, + ) if err != nil { return fmt.Errorf("cannot renderOptions template: %w", err) } diff --git a/options-gen/cmd_test.go b/options-gen/cmd_test.go index 2b5b519..086e6e4 100644 --- a/options-gen/cmd_test.go +++ b/options-gen/cmd_test.go @@ -46,6 +46,7 @@ func TestRun(t *testing.T) { outFilename, "Options", "testcase", + params.OutPrefix, params.Defaults, true, ) @@ -65,6 +66,7 @@ func TestRun(t *testing.T) { filepath.Join(dir, "options_generated.go"), "Options", "testcase", + "XXX", optionsgen.Defaults{From: optionsgen.DefaultsFromTag, Param: ""}, true, ) @@ -73,11 +75,13 @@ func TestRun(t *testing.T) { } type Params struct { - Defaults optionsgen.Defaults `json:"defaults"` + OutPrefix string `json:"out_prefix"` //nolint:tagliatelle + Defaults optionsgen.Defaults `json:"defaults"` } func readParams(filename string) Params { params := Params{ + OutPrefix: "", Defaults: optionsgen.Defaults{ From: optionsgen.DefaultsFromTag, Param: "", diff --git a/options-gen/default_test.go b/options-gen/default_test.go index d981cbd..fe611a5 100644 --- a/options-gen/default_test.go +++ b/options-gen/default_test.go @@ -18,31 +18,31 @@ func TestDefaultValues(t *testing.T) { wantError: false, }, { - opts: testcase.NewOptions(testcase.WithPingPeriod(0)), + opts: testcase.NewOptions(testcase.WithSomePingPeriod(0)), wantError: true, }, { - opts: testcase.NewOptions(testcase.WithPingPeriod(time.Hour)), + opts: testcase.NewOptions(testcase.WithSomePingPeriod(time.Hour)), wantError: true, }, { - opts: testcase.NewOptions(testcase.WithName("")), + opts: testcase.NewOptions(testcase.WithSomeName("")), wantError: true, }, { - opts: testcase.NewOptions(testcase.WithMaxAttempts(0)), + opts: testcase.NewOptions(testcase.WithSomeMaxAttempts(0)), wantError: true, }, { - opts: testcase.NewOptions(testcase.WithMaxAttempts(-1)), + opts: testcase.NewOptions(testcase.WithSomeMaxAttempts(-1)), wantError: true, }, { - opts: testcase.NewOptions(testcase.WithMaxAttempts(11)), + opts: testcase.NewOptions(testcase.WithSomeMaxAttempts(11)), wantError: true, }, { - opts: testcase.NewOptions(testcase.WithEps(0.)), + opts: testcase.NewOptions(testcase.WithSomeEps(0.)), wantError: true, }, } diff --git a/options-gen/generics_test.go b/options-gen/generics_test.go index 44a51ca..100d796 100644 --- a/options-gen/generics_test.go +++ b/options-gen/generics_test.go @@ -16,7 +16,7 @@ func TestGenericsOptions(t *testing.T) { "string key", nil, "", - testcase.WithAnyOpt[string, interface{ Timeout() bool }](net.Error(nil)), + testcase.WithSomeAnyOpt[string, interface{ Timeout() bool }](net.Error(nil)), ) assert.Error(t, opts.Validate()) }) @@ -27,8 +27,8 @@ func TestGenericsOptions(t *testing.T) { 42, new(handlerMock), 24, - testcase.WithAnyOpt[int, float32](24.24), - testcase.WithOptHandler[int, float32](new(handlerMock)), + testcase.WithSomeAnyOpt[int, float32](24.24), + testcase.WithSomeOptHandler[int, float32](new(handlerMock)), ) assert.NoError(t, opts.Validate()) }) diff --git a/options-gen/testdata/case-05-generics-01/.params.json b/options-gen/testdata/case-05-generics-01/.params.json new file mode 100644 index 0000000..6336194 --- /dev/null +++ b/options-gen/testdata/case-05-generics-01/.params.json @@ -0,0 +1,3 @@ +{ + "out_prefix": "Some" +} \ No newline at end of file diff --git a/options-gen/testdata/case-05-generics-01/options_generated.go b/options-gen/testdata/case-05-generics-01/options_generated.go index 72cfc77..f3324ee 100644 --- a/options-gen/testdata/case-05-generics-01/options_generated.go +++ b/options-gen/testdata/case-05-generics-01/options_generated.go @@ -28,7 +28,7 @@ func NewOptions[T string]( return o } -func WithOptKey[T string](opt T) OptOptionsSetter[T] { +func WithSomeOptKey[T string](opt T) OptOptionsSetter[T] { return func(o *Options[T]) { o.optKey = opt } diff --git a/options-gen/testdata/case-05-generics-01/options_generated.go.expected b/options-gen/testdata/case-05-generics-01/options_generated.go.expected index 72cfc77..f3324ee 100644 --- a/options-gen/testdata/case-05-generics-01/options_generated.go.expected +++ b/options-gen/testdata/case-05-generics-01/options_generated.go.expected @@ -28,7 +28,7 @@ func NewOptions[T string]( return o } -func WithOptKey[T string](opt T) OptOptionsSetter[T] { +func WithSomeOptKey[T string](opt T) OptOptionsSetter[T] { return func(o *Options[T]) { o.optKey = opt } diff --git a/options-gen/testdata/case-05-generics-02/.params.json b/options-gen/testdata/case-05-generics-02/.params.json new file mode 100644 index 0000000..6336194 --- /dev/null +++ b/options-gen/testdata/case-05-generics-02/.params.json @@ -0,0 +1,3 @@ +{ + "out_prefix": "Some" +} \ No newline at end of file diff --git a/options-gen/testdata/case-05-generics-02/options_generated.go b/options-gen/testdata/case-05-generics-02/options_generated.go index 917c469..0fb463c 100644 --- a/options-gen/testdata/case-05-generics-02/options_generated.go +++ b/options-gen/testdata/case-05-generics-02/options_generated.go @@ -33,19 +33,19 @@ func NewOptions[KeyT int | string, TT any]( return o } -func WithOptHandler[KeyT int | string, TT any](opt http.Handler) OptOptionsSetter[KeyT, TT] { +func WithSomeOptHandler[KeyT int | string, TT any](opt http.Handler) OptOptionsSetter[KeyT, TT] { return func(o *Options[KeyT, TT]) { o.optHandler = opt } } -func WithOptKey[KeyT int | string, TT any](opt KeyT) OptOptionsSetter[KeyT, TT] { +func WithSomeOptKey[KeyT int | string, TT any](opt KeyT) OptOptionsSetter[KeyT, TT] { return func(o *Options[KeyT, TT]) { o.optKey = opt } } -func WithAnyOpt[KeyT int | string, TT any](opt TT) OptOptionsSetter[KeyT, TT] { +func WithSomeAnyOpt[KeyT int | string, TT any](opt TT) OptOptionsSetter[KeyT, TT] { return func(o *Options[KeyT, TT]) { o.anyOpt = opt } diff --git a/options-gen/testdata/case-05-generics-02/options_generated.go.expected b/options-gen/testdata/case-05-generics-02/options_generated.go.expected index 917c469..0fb463c 100644 --- a/options-gen/testdata/case-05-generics-02/options_generated.go.expected +++ b/options-gen/testdata/case-05-generics-02/options_generated.go.expected @@ -33,19 +33,19 @@ func NewOptions[KeyT int | string, TT any]( return o } -func WithOptHandler[KeyT int | string, TT any](opt http.Handler) OptOptionsSetter[KeyT, TT] { +func WithSomeOptHandler[KeyT int | string, TT any](opt http.Handler) OptOptionsSetter[KeyT, TT] { return func(o *Options[KeyT, TT]) { o.optHandler = opt } } -func WithOptKey[KeyT int | string, TT any](opt KeyT) OptOptionsSetter[KeyT, TT] { +func WithSomeOptKey[KeyT int | string, TT any](opt KeyT) OptOptionsSetter[KeyT, TT] { return func(o *Options[KeyT, TT]) { o.optKey = opt } } -func WithAnyOpt[KeyT int | string, TT any](opt TT) OptOptionsSetter[KeyT, TT] { +func WithSomeAnyOpt[KeyT int | string, TT any](opt TT) OptOptionsSetter[KeyT, TT] { return func(o *Options[KeyT, TT]) { o.anyOpt = opt } diff --git a/options-gen/testdata/case-12-defaults-tag-02/.params.json b/options-gen/testdata/case-12-defaults-tag-02/.params.json index fb09f1d..5c39d5f 100644 --- a/options-gen/testdata/case-12-defaults-tag-02/.params.json +++ b/options-gen/testdata/case-12-defaults-tag-02/.params.json @@ -1,4 +1,5 @@ { + "out_prefix": "Some", "defaults": { "from": "tag", "param": "my-default-tag" diff --git a/options-gen/testdata/case-12-defaults-tag-02/options_generated.go b/options-gen/testdata/case-12-defaults-tag-02/options_generated.go index 0613504..1361e46 100644 --- a/options-gen/testdata/case-12-defaults-tag-02/options_generated.go +++ b/options-gen/testdata/case-12-defaults-tag-02/options_generated.go @@ -28,25 +28,25 @@ func NewOptions( return o } -func WithPingPeriod(opt time.Duration) OptOptionsSetter { +func WithSomePingPeriod(opt time.Duration) OptOptionsSetter { return func(o *Options) { o.pingPeriod = opt } } -func WithName(opt string) OptOptionsSetter { +func WithSomeName(opt string) OptOptionsSetter { return func(o *Options) { o.name = opt } } -func WithMaxAttempts(opt int) OptOptionsSetter { +func WithSomeMaxAttempts(opt int) OptOptionsSetter { return func(o *Options) { o.maxAttempts = opt } } -func WithEps(opt float32) OptOptionsSetter { +func WithSomeEps(opt float32) OptOptionsSetter { return func(o *Options) { o.eps = opt } diff --git a/options-gen/testdata/case-12-defaults-tag-02/options_generated.go.expected b/options-gen/testdata/case-12-defaults-tag-02/options_generated.go.expected index 0613504..1361e46 100644 --- a/options-gen/testdata/case-12-defaults-tag-02/options_generated.go.expected +++ b/options-gen/testdata/case-12-defaults-tag-02/options_generated.go.expected @@ -28,25 +28,25 @@ func NewOptions( return o } -func WithPingPeriod(opt time.Duration) OptOptionsSetter { +func WithSomePingPeriod(opt time.Duration) OptOptionsSetter { return func(o *Options) { o.pingPeriod = opt } } -func WithName(opt string) OptOptionsSetter { +func WithSomeName(opt string) OptOptionsSetter { return func(o *Options) { o.name = opt } } -func WithMaxAttempts(opt int) OptOptionsSetter { +func WithSomeMaxAttempts(opt int) OptOptionsSetter { return func(o *Options) { o.maxAttempts = opt } } -func WithEps(opt float32) OptOptionsSetter { +func WithSomeEps(opt float32) OptOptionsSetter { return func(o *Options) { o.eps = opt } diff --git a/options-gen/testdata/case-13-defaults-var/.params.json b/options-gen/testdata/case-13-defaults-var/.params.json index 3f90fd0..d151ab8 100644 --- a/options-gen/testdata/case-13-defaults-var/.params.json +++ b/options-gen/testdata/case-13-defaults-var/.params.json @@ -1,4 +1,5 @@ { + "out_prefix": "XXX", "defaults": { "from": "var", "param": "defaultOptions" diff --git a/options-gen/testdata/case-13-defaults-var/options_generated.go b/options-gen/testdata/case-13-defaults-var/options_generated.go index 6f91cd7..c8f1f6f 100644 --- a/options-gen/testdata/case-13-defaults-var/options_generated.go +++ b/options-gen/testdata/case-13-defaults-var/options_generated.go @@ -29,25 +29,25 @@ func NewOptions( return o } -func WithName(opt string) OptOptionsSetter { +func WithXXXName(opt string) OptOptionsSetter { return func(o *Options) { o.name = opt } } -func WithTimeout(opt time.Duration) OptOptionsSetter { +func WithXXXTimeout(opt time.Duration) OptOptionsSetter { return func(o *Options) { o.timeout = opt } } -func WithMaxAttempts(opt int) OptOptionsSetter { +func WithXXXMaxAttempts(opt int) OptOptionsSetter { return func(o *Options) { o.maxAttempts = opt } } -func WithHttpClient(opt *http.Client) OptOptionsSetter { +func WithXXXHttpClient(opt *http.Client) OptOptionsSetter { return func(o *Options) { o.httpClient = opt } diff --git a/options-gen/testdata/case-13-defaults-var/options_generated.go.expected b/options-gen/testdata/case-13-defaults-var/options_generated.go.expected index 6f91cd7..c8f1f6f 100644 --- a/options-gen/testdata/case-13-defaults-var/options_generated.go.expected +++ b/options-gen/testdata/case-13-defaults-var/options_generated.go.expected @@ -29,25 +29,25 @@ func NewOptions( return o } -func WithName(opt string) OptOptionsSetter { +func WithXXXName(opt string) OptOptionsSetter { return func(o *Options) { o.name = opt } } -func WithTimeout(opt time.Duration) OptOptionsSetter { +func WithXXXTimeout(opt time.Duration) OptOptionsSetter { return func(o *Options) { o.timeout = opt } } -func WithMaxAttempts(opt int) OptOptionsSetter { +func WithXXXMaxAttempts(opt int) OptOptionsSetter { return func(o *Options) { o.maxAttempts = opt } } -func WithHttpClient(opt *http.Client) OptOptionsSetter { +func WithXXXHttpClient(opt *http.Client) OptOptionsSetter { return func(o *Options) { o.httpClient = opt } diff --git a/options-gen/testdata/case-14-defaults-func/.params.json b/options-gen/testdata/case-14-defaults-func/.params.json index 1cf98d7..bc5684a 100644 --- a/options-gen/testdata/case-14-defaults-func/.params.json +++ b/options-gen/testdata/case-14-defaults-func/.params.json @@ -1,4 +1,5 @@ { + "out_prefix": "Some", "defaults": { "from": "func", "param": "getDefaults" diff --git a/options-gen/testdata/case-14-defaults-func/options_generated.go b/options-gen/testdata/case-14-defaults-func/options_generated.go index f52c3db..e1fbcf0 100644 --- a/options-gen/testdata/case-14-defaults-func/options_generated.go +++ b/options-gen/testdata/case-14-defaults-func/options_generated.go @@ -30,25 +30,25 @@ func NewOptions( return o } -func WithName(opt string) OptOptionsSetter { +func WithSomeName(opt string) OptOptionsSetter { return func(o *Options) { o.name = opt } } -func WithTimeout(opt time.Duration) OptOptionsSetter { +func WithSomeTimeout(opt time.Duration) OptOptionsSetter { return func(o *Options) { o.timeout = opt } } -func WithMaxAttempts(opt int) OptOptionsSetter { +func WithSomeMaxAttempts(opt int) OptOptionsSetter { return func(o *Options) { o.maxAttempts = opt } } -func WithHttpClient(opt *http.Client) OptOptionsSetter { +func WithSomeHttpClient(opt *http.Client) OptOptionsSetter { return func(o *Options) { o.httpClient = opt } diff --git a/options-gen/testdata/case-14-defaults-func/options_generated.go.expected b/options-gen/testdata/case-14-defaults-func/options_generated.go.expected index f52c3db..e1fbcf0 100644 --- a/options-gen/testdata/case-14-defaults-func/options_generated.go.expected +++ b/options-gen/testdata/case-14-defaults-func/options_generated.go.expected @@ -30,25 +30,25 @@ func NewOptions( return o } -func WithName(opt string) OptOptionsSetter { +func WithSomeName(opt string) OptOptionsSetter { return func(o *Options) { o.name = opt } } -func WithTimeout(opt time.Duration) OptOptionsSetter { +func WithSomeTimeout(opt time.Duration) OptOptionsSetter { return func(o *Options) { o.timeout = opt } } -func WithMaxAttempts(opt int) OptOptionsSetter { +func WithSomeMaxAttempts(opt int) OptOptionsSetter { return func(o *Options) { o.maxAttempts = opt } } -func WithHttpClient(opt *http.Client) OptOptionsSetter { +func WithSomeHttpClient(opt *http.Client) OptOptionsSetter { return func(o *Options) { o.httpClient = opt }