/
bind.go
99 lines (84 loc) · 2.16 KB
/
bind.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package bind
import (
"encoding/json"
"errors"
"net/http"
"reflect"
"github.com/matryer/way"
"gopkg.in/go-playground/validator.v9"
)
// Binder contains the request bind an validator objects.
type Binder struct {
validator *validator.Validate
}
// New returns a new binder for request bind and validation.
func New() *Binder {
return &Binder{
validator: validator.New(),
}
}
// Validate will validate a struct using the validator.
func (b *Binder) Validate(s interface{}) error {
return b.validator.Struct(s)
}
// JSONUnmarshal will perform an unmarshal on an interface using JSON.
/*func (b *Binder) JSONUnmarshal(iface interface{}, keys []string,
values []string) (err error) {
// Check for errors.
v := reflect.ValueOf(iface)
if v.Kind() != reflect.Ptr {
return errors.New("must pass a pointer, not a value")
} else if len(keys) != len(values) {
return errors.New("keys and values array or not the same length")
}
// Load the map.
m := make(map[string]interface{})
for i := 0; i < len(keys); i++ {
m[keys[i]] = values[i]
}
// Convert to JSON.
var data []byte
data, err = json.Marshal(m)
if err != nil {
return
}
// Unmarshal to the interface from JSON.
return json.Unmarshal(data, &iface)
}*/
// FormUnmarshal will perform an unmarshal on an interface using a form.
func (b *Binder) FormUnmarshal(iface interface{}, r *http.Request) (err error) {
// Check for errors.
v := reflect.ValueOf(iface)
if v.Kind() != reflect.Ptr {
return errors.New("must pass a pointer, not a value")
}
// Parse the form.
err = r.ParseForm()
if err != nil {
return err
}
// Load the map.
m := make(map[string]interface{})
for k, v := range r.Form {
m[k] = v[0]
}
// Loop through each field to extract the URL parameter.
elem := reflect.Indirect(v.Elem())
keys := elem.Type()
for j := 0; j < elem.NumField(); j++ {
tag := keys.Field(j).Tag
tagvalue := tag.Get("json")
pathParam := way.Param(r.Context(), tagvalue)
if len(pathParam) > 0 {
m[tagvalue] = pathParam
}
}
// Convert to JSON.
var data []byte
data, err = json.Marshal(m)
if err != nil {
return
}
// Unmarshal to the interface from JSON.
return json.Unmarshal(data, &iface)
}