(parser *QueryParamParser, parse func(v string) (T, error), def T, queryParam string)
| 429 | } |
| 430 | |
| 431 | func parseSingle[T any](parser *QueryParamParser, parse func(v string) (T, error), def T, queryParam string) func(set []string) (T, error) { |
| 432 | return func(set []string) (T, error) { |
| 433 | if len(set) > 1 { |
| 434 | // Set as a parser.Error rather than return an error. |
| 435 | // Returned errors are errors from the passed in `parse` function, and |
| 436 | // imply the query param value had attempted to be parsed. |
| 437 | // By raising the error this way, we can also more easily control how it |
| 438 | // is presented to the user. A returned error is wrapped with more text. |
| 439 | parser.Errors = append(parser.Errors, codersdk.ValidationError{ |
| 440 | Field: queryParam, |
| 441 | Detail: fmt.Sprintf("Query param %q provided more than once, found %d times. Only provide 1 instance of this query param.", queryParam, len(set)), |
| 442 | }) |
| 443 | return def, nil |
| 444 | } |
| 445 | return parse(set[0]) |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | func parseQueryParamSet[T any](parser *QueryParamParser, vals url.Values, parse func(set []string) (T, error), def T, queryParam string) (T, error) { |
| 450 | parser.addParsed(queryParam) |
no test coverage detected