ParseValueOr parses value to generic type, when value is empty defaultValue is returned. Types that are supported: - bool - float32 - float64 - int - int8 - int16 - int32 - int64 - uint - uint8/byte - uint16 - uint32 - uint64 - string - echo.BindUnmarshaler interface - encoding.TextUnmarshaler inte
(value string, defaultValue T, opts ...any)
| 391 | // - time.Duration |
| 392 | // - time.Time use echo.TimeOpts or echo.TimeLayout to set time parsing configuration |
| 393 | func ParseValueOr[T any](value string, defaultValue T, opts ...any) (T, error) { |
| 394 | if len(value) == 0 { |
| 395 | return defaultValue, nil |
| 396 | } |
| 397 | var tmp T |
| 398 | if err := bindValue(value, &tmp, opts...); err != nil { |
| 399 | var zero T |
| 400 | return zero, fmt.Errorf("failed to parse value, err: %w", err) |
| 401 | } |
| 402 | return tmp, nil |
| 403 | } |
| 404 | |
| 405 | func bindValue(value string, dest any, opts ...any) error { |
| 406 | // NOTE: if this function is ever made public the dest should be checked for nil |
no test coverage detected
searching dependent graphs…