-
Notifications
You must be signed in to change notification settings - Fork 0
/
StrongParamsRequiredAndPermitted.go
82 lines (65 loc) · 2.12 KB
/
StrongParamsRequiredAndPermitted.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
74
75
76
77
78
79
80
81
82
package strongparams
import (
"github.com/pkg/errors"
"github.com/vellotis/go-strongparams/permitter"
"net/http"
"net/url"
)
type StrongParamsRequiredAndPermitted struct {
*strongParamsRequiredAndPermitted
}
type strongParamsRequiredAndPermitted struct {
*strongParamsRequired
permitRules permitter.Permittable
}
// Query instructs the mechanism to process http.Request's url.URL property url.URL/Query() method returned url.Values.
func (this *StrongParamsRequiredAndPermitted) Query(request *http.Request) ReturnTarget {
return this.Values(request.URL.Query())
}
// PostForm instructs the mechanism to process http.Request's url.PostForm property's url.Values.
func (this *StrongParamsRequiredAndPermitted) PostForm(request *http.Request) ReturnTarget {
return this.Values(request.PostForm)
}
// Values instructs the mechanism to process url.Values from `values` parameter.
func (this *StrongParamsRequiredAndPermitted) Values(values url.Values) ReturnTarget {
values = cloneUrlValues(values)
if this.error == nil {
this.error = this.validate(values)
}
return func(target interface{}) error {
if this.error != nil {
return this.error
} else if target == nil {
return errors.New("`target` argument cannot be nil")
}
return this.validateTransformAndDecode(values, target)
}
}
func (this *StrongParamsRequiredAndPermitted) validateTransformAndDecode(values url.Values, target interface{}) (err error) {
if err := this.validate(values); err != nil {
return err
}
values, err = this.transform(values)
if err != nil {
return err
}
return this.decode(values, target)
}
func (this *strongParamsRequiredAndPermitted) validate(values url.Values) error {
if err := this.strongParamsRequired.validate(values); err != nil {
return err
}
return nil
}
func (this *strongParamsRequiredAndPermitted) transform(queryValues url.Values) (_ url.Values, err error) {
queryValues, err = this.strongParamsRequired.transform(queryValues)
if err != nil {
return nil, err
}
for queryKeyPath := range queryValues {
if !this.permitRules.IsPermitted(queryKeyPath) {
queryValues.Del(queryKeyPath)
}
}
return queryValues, nil
}