OptionalIntParam is a helper function that can be used to fetch a requested parameter from the request. It does the following checks: 1. Checks if the parameter is present in the request, if not, it returns its zero-value 2. If it is present, it checks if the parameter is of the expected type (float
(args map[string]any, p string)
| 198 | // 1. Checks if the parameter is present in the request, if not, it returns its zero-value |
| 199 | // 2. If it is present, it checks if the parameter is of the expected type (float64 or numeric string) and returns it |
| 200 | func OptionalIntParam(args map[string]any, p string) (int, error) { |
| 201 | val, ok := args[p] |
| 202 | if !ok { |
| 203 | return 0, nil |
| 204 | } |
| 205 | |
| 206 | result, err := toInt(val) |
| 207 | if err != nil { |
| 208 | return 0, fmt.Errorf("parameter %s is not a valid number: %w", p, err) |
| 209 | } |
| 210 | |
| 211 | return result, nil |
| 212 | } |
| 213 | |
| 214 | // OptionalIntParamWithDefault is a helper function that can be used to fetch a requested parameter from the request |
| 215 | // similar to optionalIntParam, but it also takes a default value. |