-
-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
644 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
# Setting: enum | ||
|
||
### Definition | ||
|
||
The Go language doesn't explicit define enums. In the context of Goverter an | ||
enum type is defined as a named type with an | ||
[underlying type](https://go.dev/ref/spec#Underlying_types) of (`float`, | ||
`string`, or `integer`) _and_ with at least one constant defined in the same | ||
package. | ||
|
||
These examples would qualify as enums: | ||
|
||
::: code-group | ||
<<< @../../example/enum/default/input/enum.go [iota.go] | ||
<<< @../../example/enum/default/output/enum.go [string.go] | ||
::: | ||
|
||
## enum:default ACTION | ||
|
||
`enum:default ACTION` can be defined as [method | ||
comment](./define-settings.md#method). No default value. | ||
|
||
Define what happens on an invalid or unexpected enum value. | ||
|
||
`enum:default @error` returns an error in the default case of the switch | ||
statement. | ||
|
||
::: details Example (click me) | ||
::: code-group | ||
<<< @../../example/enum/default/error/input.go | ||
<<< @../../example/enum/default/input/enum.go [input/enum.go] | ||
<<< @../../example/enum/default/output/enum.go [output/enum.go] | ||
<<< @../../example/enum/default/error/generated/generated.go [generated/generated.go] | ||
::: | ||
|
||
`enum:default @ignore` does nothing in the default case of the switch | ||
statement. | ||
|
||
::: details Example (click me) | ||
::: code-group | ||
<<< @../../example/enum/default/ignore/input.go | ||
<<< @../../example/enum/default/input/enum.go [input/enum.go] | ||
<<< @../../example/enum/default/output/enum.go [output/enum.go] | ||
<<< @../../example/enum/default/ignore/generated/generated.go [generated/generated.go] | ||
::: | ||
|
||
`enum:default @panic` panics in the default case of the switch statement. | ||
|
||
::: details Example (click me) | ||
::: code-group | ||
<<< @../../example/enum/default/panic/input.go | ||
<<< @../../example/enum/default/input/enum.go [input/enum.go] | ||
<<< @../../example/enum/default/output/enum.go [output/enum.go] | ||
<<< @../../example/enum/default/panic/generated/generated.go [generated/generated.go] | ||
::: | ||
|
||
## enum:detect | ||
|
||
`enum:detect [yes|no]` can be defined as [CLI | ||
argument](./define-settings.md#cli) or [converter | ||
comment](./define-settings.md#converter). | ||
|
||
`enum:detect` enables enum detection as per [Definition](#definition) and | ||
enables the enum conversion generation when the `source` and `target` types are | ||
enum types. | ||
|
||
::: details Example (click me) | ||
::: code-group | ||
<<< @../../example/enum/default/panic/input.go | ||
<<< @../../example/enum/default/input/enum.go [input/enum.go] | ||
<<< @../../example/enum/default/output/enum.go [output/enum.go] | ||
<<< @../../example/enum/default/panic/generated/generated.go [generated/generated.go] | ||
::: | ||
|
||
## enum:exclude | ||
|
||
`enum:exclude [PACKAGE:]NAME` can be defined as [CLI | ||
argument](./define-settings.md#cli) or [converter | ||
comment](./define-settings.md#converter). | ||
|
||
You can exclude falsely detected enums with exclude. This is useful when a type | ||
[qualifies as enum](#definition) but isn't one. If `PACKAGE` is unset, goverter | ||
will use the package of the converter interface. | ||
|
||
Both `PACKAGE` and `NAME` can be regular expressions. | ||
|
||
::: details Example (click me) | ||
::: code-group | ||
<<< @../../example/enum/exclude/input.go | ||
<<< @../../example/enum/exclude/generated/generated.go [generated/generated.go] | ||
::: | ||
|
||
## enum:map SOURCE TARGET | ||
|
||
`enum:map SOURCE TARGET` can be defined as [method | ||
comment](./define-settings.md#method). | ||
|
||
`enum:map` can be used to map an enum key of the source enum type to the target | ||
type. In this example we have two spellings of Gray|Grey. | ||
|
||
::: details Example (click me) | ||
::: code-group | ||
<<< @../../example/enum/map/input.go | ||
<<< @../../example/enum/map/input/enum.go [input/enum.go] | ||
<<< @../../example/enum/map/output/enum.go [output/enum.go] | ||
<<< @../../example/enum/map/generated/generated.go [generated/generated.go] | ||
::: | ||
|
||
You can also use any of the `@actions` from | ||
[`enum:default`](#enumdefault-action). E.g. | ||
|
||
::: details Example (click me) | ||
::: code-group | ||
<<< @../../example/enum/map-panic/input.go | ||
<<< @../../example/enum/map-panic/input/enum.go [input/enum.go] | ||
<<< @../../example/enum/map-panic/output/enum.go [output/enum.go] | ||
<<< @../../example/enum/map-panic/generated/generated.go [generated/generated.go] | ||
::: | ||
|
||
## enum:transform ID [CONFIG] | ||
|
||
|
||
`enum:transform ID [CONFIG]` can be defined as [method | ||
comment](./define-settings.md#method). | ||
|
||
`enum:transform` allows you to transform multiple enum keys to the target keys. | ||
There are 5 builtin transformers and you can define transformers yourself. | ||
|
||
### enum:transform (trim|add)-prefix PREFIX | ||
|
||
With `trim-prefix` you can trim the prefix of the source enum keys and with | ||
`add-prefix` you can add a prefix to the source enum keys. | ||
|
||
::: details Example (click me) | ||
::: code-group | ||
<<< @../../example/enum/transform-prefix/input.go | ||
<<< @../../example/enum/transform-prefix/generated/generated.go [generated/generated.go] | ||
::: | ||
|
||
### enum:transform (trim|add)-suffix SUFFIX | ||
|
||
With `trim-suffix` you can trim the suffix of the source enum keys and with | ||
`add-suffix` you can add a suffix to the source enum keys. | ||
|
||
::: details Example (click me) | ||
::: code-group | ||
<<< @../../example/enum/transform-suffix/input.go | ||
<<< @../../example/enum/transform-suffix/generated/generated.go [generated/generated.go] | ||
::: | ||
|
||
|
||
### enum:transform regex SEARCH REPLACE | ||
|
||
With `regex` you can do a regex replace of the enum keys. | ||
|
||
::: details Example (click me) | ||
::: code-group | ||
<<< @../../example/enum/transform-regex/input.go | ||
<<< @../../example/enum/transform-regex/generated/generated.go [generated/generated.go] | ||
:: |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package example | ||
|
||
import ( | ||
"github.com/jmattheis/goverter/example/enum/default/input" | ||
"github.com/jmattheis/goverter/example/enum/default/output" | ||
) | ||
|
||
// goverter:converter | ||
// goverter:enum:detect | ||
// goverter:enum:default @error | ||
type Converter interface { | ||
Convert(input.Color) (output.Color, error) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package example | ||
|
||
import ( | ||
"github.com/jmattheis/goverter/example/enum/default/input" | ||
"github.com/jmattheis/goverter/example/enum/default/output" | ||
) | ||
|
||
// goverter:converter | ||
// goverter:enum:detect | ||
// goverter:enum:default @ignore | ||
type Converter interface { | ||
Convert(input.Color) output.Color | ||
} |
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,8 @@ | ||
package input | ||
|
||
type Color int | ||
const ( | ||
Green Color = iota | ||
Blue | ||
Red | ||
) |
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,8 @@ | ||
package output | ||
|
||
type Color string | ||
const ( | ||
Green Color = "green" | ||
Blue Color = "blue" | ||
Red Color = "red" | ||
) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package example | ||
|
||
import ( | ||
"github.com/jmattheis/goverter/example/enum/default/input" | ||
"github.com/jmattheis/goverter/example/enum/default/output" | ||
) | ||
|
||
// goverter:converter | ||
// goverter:enum:detect | ||
// goverter:enum:default @panic | ||
type Converter interface { | ||
Convert(input.Color) output.Color | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package example | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
// goverter:converter | ||
// goverter:enum:detect | ||
// goverter:enum:default @panic | ||
// goverter:enum:exclude MyDuration | ||
// goverter:enum:exclude time:Duration | ||
type Converter interface { | ||
Convert(MyDuration) time.Duration | ||
} | ||
|
||
type MyDuration int64 | ||
|
||
const ( | ||
Nanoseconds MyDuration = 1 | ||
Microseconds MyDuration = 1000 * Nanoseconds | ||
) |
Oops, something went wrong.