PositiveInt32 function checks if the given value is 32-bit and positive. We can't use `uint32` as the value must be within the range <0,2147483647> as database expects it. Otherwise, the database query fails with `pq: OFFSET must not be negative`.
(vals url.Values, def int32, queryParam string)
| 101 | // We can't use `uint32` as the value must be within the range <0,2147483647> |
| 102 | // as database expects it. Otherwise, the database query fails with `pq: OFFSET must not be negative`. |
| 103 | func (p *QueryParamParser) PositiveInt32(vals url.Values, def int32, queryParam string) int32 { |
| 104 | v, err := parseQueryParam(p, vals, func(v string) (int32, error) { |
| 105 | intValue, err := strconv.ParseInt(v, 10, 32) |
| 106 | if err != nil { |
| 107 | return 0, err |
| 108 | } |
| 109 | if intValue < 0 { |
| 110 | return 0, xerrors.Errorf("value is negative") |
| 111 | } |
| 112 | return int32(intValue), nil |
| 113 | }, def, queryParam) |
| 114 | if err != nil { |
| 115 | p.Errors = append(p.Errors, codersdk.ValidationError{ |
| 116 | Field: queryParam, |
| 117 | Detail: fmt.Sprintf("Query param %q must be a valid 32-bit positive integer: %s", queryParam, err.Error()), |
| 118 | }) |
| 119 | } |
| 120 | return v |
| 121 | } |
| 122 | |
| 123 | // PositiveInt64 function checks if the given value is 64-bit and positive. |
| 124 | func (p *QueryParamParser) PositiveInt64(vals url.Values, def int64, queryParam string) int64 { |
no test coverage detected