OptionalParam 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 and retur
(args map[string]any, p string)
| 178 | // 1. Checks if the parameter is present in the request, if not, it returns its zero-value |
| 179 | // 2. If it is present, it checks if the parameter is of the expected type and returns it |
| 180 | func OptionalParam[T any](args map[string]any, p string) (T, error) { |
| 181 | var zero T |
| 182 | |
| 183 | // Check if the parameter is present in the request |
| 184 | if _, ok := args[p]; !ok { |
| 185 | return zero, nil |
| 186 | } |
| 187 | |
| 188 | // Check if the parameter is of the expected type |
| 189 | if _, ok := args[p].(T); !ok { |
| 190 | return zero, fmt.Errorf("parameter %s is not of type %T, is %T", p, zero, args[p]) |
| 191 | } |
| 192 | |
| 193 | return args[p].(T), nil |
| 194 | } |
| 195 | |
| 196 | // OptionalIntParam is a helper function that can be used to fetch a requested parameter from the request. |
| 197 | // It does the following checks: |
no outgoing calls
no test coverage detected