ParseEnum is a function that can be passed into ParseCustom that handles enum validation.
(term string)
| 342 | // ParseEnum is a function that can be passed into ParseCustom that handles enum |
| 343 | // validation. |
| 344 | func ParseEnum[T ValidEnum](term string) (T, error) { |
| 345 | enum := T(term) |
| 346 | if enum.Valid() { |
| 347 | return enum, nil |
| 348 | } |
| 349 | var empty T |
| 350 | return empty, xerrors.Errorf("%q is not a valid value", term) |
| 351 | } |
| 352 | |
| 353 | // ParseCustom has to be a function, not a method on QueryParamParser because generics |
| 354 | // cannot be used on struct methods. |