-
Notifications
You must be signed in to change notification settings - Fork 0
/
bind.go
111 lines (92 loc) · 2.13 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
100
101
102
103
104
105
106
107
108
109
110
111
package querybinder
import (
"errors"
"fmt"
"net/url"
"reflect"
"strconv"
"strings"
)
type Binder interface {
SetTag(tag string)
Bind(obj interface{}, url *url.URL) error
}
const (
defaultTagKey = "bind"
requiredTagValue = "required"
)
var ErrInvalidObjectType = errors.New("invalid object type")
var ErrMissingQueryParam = errors.New("missing required query param")
var ErrInvalidQueryValue = errors.New("invalid query value")
type binder struct {
tagKey string
}
// Bind unmarshal the url into given object struct based on the provided tags.
func (b *binder) Bind(obj interface{}, url *url.URL) error {
val := reflect.ValueOf(obj)
if !isStructPointer(val) {
return ErrInvalidObjectType
}
rt := val.Elem().Type()
params := url.Query()
for i := 0; i < rt.NumField(); i++ {
f := rt.Field(i)
tagStr, ok := f.Tag.Lookup(b.tagKey)
if !ok {
continue
}
tag, required := isRequiredField(tagStr)
val, ok := params[tag]
if !ok {
if required {
return fmt.Errorf("%w, '%s' is required", ErrMissingQueryParam, tag)
}
continue
}
var v interface{}
switch f.Type.Kind() {
case reflect.String:
v = val[0]
case reflect.Slice:
v = val
case reflect.Int:
i, err := strconv.Atoi(val[0])
if err != nil {
return fmt.Errorf("%w, err:%s", ErrInvalidQueryValue, err.Error())
}
v = i
}
reflect.ValueOf(obj).Elem().Field(i).Set(reflect.ValueOf(v))
}
return nil
}
func isStructPointer(v reflect.Value) bool {
// Check if the value is a pointer
if v.Kind() == reflect.Ptr {
// Check if the element the pointer points to is a struct
return v.Elem().Kind() == reflect.Struct
}
return false
}
func isRequiredField(tagStr string) (tag string, required bool) {
tagSplit := strings.Split(tagStr, ",")
switch len(tagSplit) {
case 1:
return tagSplit[0], false
case 2:
if tagSplit[1] == requiredTagValue {
return tagSplit[0], true
}
}
return tagStr, false
}
// SetTag sets a new struct parameter read tag.
func (b *binder) SetTag(tag string) {
b.tagKey = tag
}
// NewQueryBinder returns a new QueryBinder object
func NewQueryBinder() Binder {
return &binder{
tagKey: defaultTagKey,
}
}