-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…file Several options in one file
- Loading branch information
Showing
33 changed files
with
361 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ./... | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.