-
Notifications
You must be signed in to change notification settings - Fork 0
/
operation_form.go
73 lines (58 loc) · 1.27 KB
/
operation_form.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package inreq
import (
"mime/multipart"
"net/http"
"reflect"
"golang.org/x/exp/maps"
)
// DecodeOperationForm is a DecodeOperation that gets values from HTTP forms.
type DecodeOperationForm struct {
}
func (d *DecodeOperationForm) Decode(ctx DecodeContext, r *http.Request, isList bool, field reflect.Value,
tag *Tag) (bool, any, error) {
var form multipart.Form
err := r.ParseForm()
if err != nil {
return false, nil, err
}
if r.MultipartForm != nil {
form = *r.MultipartForm
} else {
if r.Form != nil {
form.Value = r.Form
}
}
values, ok := form.Value[tag.Name]
if !ok {
return false, nil, nil
}
if len(values) == 0 {
return false, nil, nil
}
ctx.ValueUsed(OperationForm, tag.Name)
if isList {
return true, values, nil
}
return true, values[0], nil
}
func (d *DecodeOperationForm) Validate(ctx DecodeContext, r *http.Request) error {
if !ctx.EnsureAllFormUsed() {
return nil
}
var form multipart.Form
if r.MultipartForm != nil {
form = *r.MultipartForm
} else {
if r.Form != nil {
form.Value = r.Form
}
}
formKeys := map[string]bool{}
for key, _ := range form.Value {
formKeys[key] = true
}
if !maps.Equal(formKeys, ctx.GetUsedValues(OperationForm)) {
return ValuesNotUsedError{Operation: OperationForm}
}
return nil
}