You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+28-1Lines changed: 28 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -37,6 +37,8 @@ Flags:
37
37
transform each item name by removing a prefix or comma separated list of prefixes. Default: ""
38
38
-type string
39
39
comma-separated list of type names; must be set
40
+
-typederrors
41
+
if true, errors from errs/ will be errors.Join()-ed for errors.Is(...) to simplify invalid value handling. Default: false
40
42
-values
41
43
if true, alternative string values method will be generated. Default: false
42
44
-yaml
@@ -70,6 +72,9 @@ When Enumer is applied to a type, it will generate:
70
72
the enum conform to the `gopkg.in/yaml.v2.Marshaler` and `gopkg.in/yaml.v2.Unmarshaler` interfaces.
71
73
- When the flag `sql` is provided, the methods for implementing the `Scanner` and `Valuer` interfaces.
72
74
Useful when storing the enum in a database.
75
+
- When the flag `typederrors` is provided, the string conversion functions will return errors wrapped with
76
+
`errors.Join()` containing a typed error from the `errs` package. This allows you to use `errors.Is()` to
77
+
check for specific enum validation failures.
73
78
74
79
75
80
For example, if we have an enum type called `Pill`,
@@ -200,7 +205,7 @@ For a module-aware repo with `enumer` in the `go.mod` file, generation can be ca
200
205
//go:generate go run github.com/dmarkham/enumer -type=YOURTYPE
201
206
```
202
207
203
-
There are four boolean flags: `json`, `text`, `yaml`and `sql`. You can use any combination of them (i.e. `enumer -type=Pill -json -text`),
208
+
There are five boolean flags: `json`, `text`, `yaml`, `sql`, and `typederrors`. You can use any combination of them (i.e. `enumer -type=Pill -json -text -typederrors`),
204
209
205
210
For enum string representation transformation the `transform` and `trimprefix` flags
206
211
were added (i.e. `enumer -type=MyType -json -transform=snake`).
@@ -215,6 +220,28 @@ If a prefix is provided via the `addprefix` flag, it will be added to the start
215
220
216
221
The boolean flag `values` will additionally create an alternative string values method `Values() []string` to fullfill the `EnumValues` interface of [ent](https://entgo.io/docs/schema-fields/#enum-fields).
217
222
223
+
## Typed Error Handling
224
+
225
+
When using the `typederrors` flag, you can handle enum validation errors specifically using `errors.Is()`:
226
+
227
+
```go
228
+
import (
229
+
"errors"
230
+
"github.com/dmarkham/enumer/errs"
231
+
)
232
+
233
+
// This will return a typed error that can be checked
0 commit comments