-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperation_query.go
58 lines (46 loc) · 1.24 KB
/
operation_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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package inreq
import (
"net/http"
"reflect"
"strings"
"golang.org/x/exp/maps"
)
// DecodeOperationQuery is a DecodeOperation that gets values from HTTP query parameters.
type DecodeOperationQuery struct {
}
func (d *DecodeOperationQuery) Decode(ctx DecodeContext, r *http.Request, isList bool, field reflect.Value,
tag *Tag) (bool, any, error) {
if !r.URL.Query().Has(tag.Name) {
return false, nil, nil
}
if isList {
explode, err := tag.Options.BoolValue("explode", false)
if err != nil {
return false, nil, err
}
var value []string
if explode {
value = strings.Split(r.URL.Query().Get(tag.Name),
tag.Options.Value("explodesep", ctx.SliceSplitSeparator()))
} else {
value = r.URL.Query()[tag.Name]
}
ctx.ValueUsed(OperationQuery, tag.Name)
return true, value, nil
}
ctx.ValueUsed(OperationQuery, tag.Name)
return true, r.URL.Query().Get(tag.Name), nil
}
func (d *DecodeOperationQuery) Validate(ctx DecodeContext, r *http.Request) error {
if !ctx.EnsureAllQueryUsed() {
return nil
}
queryKeys := map[string]bool{}
for key, _ := range r.URL.Query() {
queryKeys[key] = true
}
if !maps.Equal(queryKeys, ctx.GetUsedValues(OperationQuery)) {
return ValuesNotUsedError{Operation: OperationQuery}
}
return nil
}