-
Notifications
You must be signed in to change notification settings - Fork 55
/
bind.go
167 lines (154 loc) · 4.15 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package zvalid
import (
"errors"
"fmt"
"reflect"
"strconv"
"strings"
)
// ValidEle ValidEle
type ValidEle struct {
target interface{}
source Engine
}
// Silent an error occurred during filtering, no error is returned
func (v Engine) Silent() Engine {
v.silent = true
return v
}
// Default if a filtering error occurs, the default value is assigned to the variable
func (v Engine) Default(value interface{}) Engine {
return pushQueue(v, func(v Engine) Engine {
v.defaultValue = value
return v
}, true)
}
// Separator specify the separator of the slice type
func (v Engine) Separator(sep string) Engine {
if v.err != nil || v.value == "" {
return v
}
v.sep = sep
return v
}
// Batch assign multiple filtered results to the specified object
func Batch(elements ...*ValidEle) error {
for k := range elements {
err := Var(elements[k].target, elements[k].source)
if err != nil {
return err
}
}
return nil
}
// BatchVar assign the filtered result to the specified variable
func BatchVar(target interface{}, source Engine) *ValidEle {
return &ValidEle{
target: target,
source: source,
}
}
// Var assign the filtered result to the specified variable
func Var(target interface{}, source Engine, name ...string) error {
source = source.valid()
if len(name) > 0 {
source.name = name[0]
}
if source.err != nil && source.defaultValue == nil {
if source.silent {
return nil
}
return source.err
}
targetValueOf := reflect.ValueOf(target)
if targetValueOf.Kind() != reflect.Ptr {
if source.silent {
return nil
}
return fmt.Errorf("parameter must pass in a pointer type: %s", source.name)
}
if !targetValueOf.Elem().CanSet() {
if source.silent {
return nil
}
return fmt.Errorf("target value of the variable cannot be changed: %s", source.name)
}
targetTypeOf := targetValueOf.Elem().Type().Kind()
if source.err == nil && source.value != "" {
source.err = setRawValue(targetTypeOf, targetValueOf, source.value, source.sep)
}
if source.err != nil && source.defaultValue != nil {
if err := setDefaultValue(targetTypeOf, targetValueOf, source.defaultValue); err != nil {
if source.silent {
return nil
}
return err
}
return source.err
} else if source.err != nil {
if source.silent {
return nil
}
return errors.New(source.name + source.err.Error())
}
return nil
}
func setRawValue(targetTypeOf reflect.Kind, targetValueOf reflect.Value, value string, sep string) error {
typeErr := errors.New("不能用" + targetTypeOf.String() + "类型赋值")
switch targetTypeOf {
case reflect.String:
targetValueOf.Elem().SetString(value)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
v, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return errors.New("必须是整数")
}
if targetValueOf.Elem().OverflowInt(v) {
return typeErr
}
targetValueOf.Elem().SetInt(v)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
v, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return errors.New("必须是无符号整数")
}
if targetValueOf.Elem().OverflowUint(v) {
return typeErr
}
targetValueOf.Elem().SetUint(v)
case reflect.Float32, reflect.Float64:
v, err := strconv.ParseFloat(value, 10)
if err != nil {
return errors.New("必须是小数")
}
if targetValueOf.Elem().OverflowFloat(v) {
return typeErr
}
targetValueOf.Elem().SetFloat(v)
case reflect.Bool:
v, err := strconv.ParseBool(value)
if err != nil {
return errors.New("必须是布尔值")
}
targetValueOf.Elem().SetBool(v)
case reflect.Slice:
sliceType := targetValueOf.Elem().Type().String()
if sliceType == "[]string" {
if sep == "" {
return errors.New("过滤规则的分隔符参数(sep)未定义")
}
targetValueOf.Elem().Set(reflect.ValueOf(strings.Split(value, sep)))
}
default:
return typeErr
}
return nil
}
func setDefaultValue(targetTypeOf reflect.Kind, targetValueOf reflect.Value, value interface{}) error {
valueTypeOf := reflect.TypeOf(value)
if valueTypeOf.Kind() != targetTypeOf {
return errors.New("值类型默认值类型不相同")
}
targetValueOf.Elem().Set(reflect.ValueOf(value))
return nil
}