Skip to content

Commit

Permalink
Merge pull request #50 from kazhuravlev/gh-49-several-options-in-one-…
Browse files Browse the repository at this point in the history
…file

Several options in one file
  • Loading branch information
kazhuravlev authored Jan 20, 2024
2 parents 712f94b + 145ae57 commit 0889cef
Show file tree
Hide file tree
Showing 33 changed files with 361 additions and 55 deletions.
11 changes: 6 additions & 5 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand All @@ -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: |
Expand Down
9 changes: 8 additions & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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 ./...
6 changes: 6 additions & 0 deletions cmd/options-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func main() {
outFilename string
optionsStructName string
outPackageName string
outPrefix string
defaultsFrom string
muteWarnings bool
)
Expand Down Expand Up @@ -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) {
Expand All @@ -67,6 +72,7 @@ func main() {
outFilename,
optionsStructName,
outPackageName,
outPrefix,
*defaults,
!muteWarnings,
)
Expand Down
13 changes: 13 additions & 0 deletions examples/go-generate-2options-1pkg/README.md
Original file line number Diff line number Diff line change
@@ -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 ./...
```
25 changes: 25 additions & 0 deletions examples/go-generate-2options-1pkg/client.go
Original file line number Diff line number Diff line change
@@ -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
}
39 changes: 39 additions & 0 deletions examples/go-generate-2options-1pkg/options.go
Original file line number Diff line number Diff line change
@@ -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,
}
93 changes: 93 additions & 0 deletions examples/go-generate-2options-1pkg/options1_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

93 changes: 93 additions & 0 deletions examples/go-generate-2options-1pkg/options2_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/library/example_out_config.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/library/example_out_options.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/library/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func main() {
params.outFname,
params.structName,
"main",
"Some",
optionsgen.Defaults{From: optionsgen.DefaultsFromTag, Param: ""},
true,
); err != nil {
Expand Down
3 changes: 2 additions & 1 deletion internal/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))

Expand All @@ -78,6 +78,7 @@ func RenderOptions(
"optionsTypeParamsSpec": spec.TypeParamsSpec,
"optionsTypeParams": spec.TypeParams,

"optionsPrefix": prefix,
"optionsStructName": optionsStructName,
"optionsStructType": optionsStructType,
"optionsStructInstanceType": optionsStructInstanceType,
Expand Down
Loading

0 comments on commit 0889cef

Please sign in to comment.