-
Notifications
You must be signed in to change notification settings - Fork 6
/
request.go
35 lines (31 loc) · 945 Bytes
/
request.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
29
30
31
32
33
34
35
package dynjson
import (
"net/http"
"net/url"
"strings"
)
// Option defines a FieldsFromRequest option.
type Option int
const (
// OptionMultipleFields expects multiple select query parameters.
OptionMultipleFields Option = iota
// OptionCommaList expects a single select parameter with comma separated values.
OptionCommaList
)
// FieldsFromRequest returns the list of fields requested from a http.Request.
//
// Without opt or with OptionMultipleFields, the expected format is:
// http://api.example.com/endpoint?select=foo&select=bar
//
// With OptionCommaList, the expected format is:
// http://api.example.com/endpoint?select=foo,bar
func FieldsFromRequest(r *http.Request, opt ...Option) []string {
vals, err := url.ParseQuery(r.URL.RawQuery)
if err != nil {
return nil
}
if len(opt) == 1 && opt[0] == OptionCommaList && len(vals["select"]) > 0 {
return strings.Split(vals["select"][0], ",")
}
return vals["select"]
}