-
Notifications
You must be signed in to change notification settings - Fork 14
/
query.go
28 lines (26 loc) · 885 Bytes
/
query.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package convert
import (
"fmt"
"net/url"
"reflect"
)
// EncodeQueryParam takes a type generated by sysl-go and encodes it into a url.Values map.
// Calling Encode() on the returned object will serialise the values into a string using the default OpenAPI3.0 serialisation format.
// e.g duplicate=foo,duplicate=bar.
func EncodeQueryParam(params url.Values, key string, value interface{}) url.Values {
switch v := reflect.ValueOf(value); v.Kind() {
case reflect.Invalid:
// Handles the case when a nil value is supplied
// Silently ignores the invalid parameter
case reflect.Slice:
for i := 0; i < v.Len(); i++ {
params.Add(key, fmt.Sprint(v.Index(i)))
}
case reflect.Int64, reflect.Float64, reflect.Bool, reflect.String:
params.Add(key, fmt.Sprint(v))
default:
// Type may be unsupported for query parameter
params.Add(key, fmt.Sprint(v))
}
return params
}