-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathint.go
More file actions
172 lines (151 loc) · 3.85 KB
/
Copy pathint.go
File metadata and controls
172 lines (151 loc) · 3.85 KB
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
168
169
170
171
172
package config
import (
"fmt"
"reflect"
"strconv"
)
var intType = reflect.TypeOf(0)
var int8Type = reflect.TypeOf(int8(0))
var int16Type = reflect.TypeOf(int16(0))
var int32Type = reflect.TypeOf(int32(0))
var int64Type = reflect.TypeOf(int64(0))
type intSetter struct {
val reflect.Value
tag reflect.StructTag
}
func (is *intSetter) String() string {
if is.val.Kind() == reflect.Invalid {
return "0"
}
return strconv.FormatInt(is.val.Int(), 10)
}
func (is *intSetter) bitSize() int {
switch is.val.Type() {
case int8Type:
return 8
case int16Type:
return 16
case int32Type:
return 32
case int64Type:
return 64
}
return 0
}
func (is *intSetter) Set(val string) error {
ival, err := strconv.ParseInt(val, 0, is.bitSize())
if err != nil {
return &ConversionError{Value: val, ToType: is.val.Type()}
}
return is.set(ival)
}
func (is *intSetter) set(val int64) error {
bitSize := is.bitSize()
tag := is.tag.Get("le")
if tag == "" {
tag = is.tag.Get("max")
}
if tag != "" {
n, err := strconv.ParseInt(tag, 0, bitSize)
if err != nil {
return &ValidationError{Value: val, Message: err.Error()}
}
if !(val <= n) {
msg := fmt.Sprintf("%d is not less than or equal to %d", val, n)
return &ValidationError{Value: val, Message: msg}
}
}
tag = is.tag.Get("ge")
if tag == "" {
tag = is.tag.Get("min")
}
if tag != "" {
n, err := strconv.ParseInt(tag, 0, bitSize)
if err != nil {
return &ValidationError{Value: val, Message: err.Error()}
}
if !(val >= n) {
msg := fmt.Sprintf("%d is not greater than or equal to %d", val, n)
return &ValidationError{Value: val, Message: msg}
}
}
if tag = is.tag.Get("lt"); tag != "" {
n, err := strconv.ParseInt(tag, 0, bitSize)
if err != nil {
return &ValidationError{Value: val, Message: err.Error()}
}
if !(val < n) {
msg := fmt.Sprintf("%d is not less than %d", val, n)
return &ValidationError{Value: val, Message: msg}
}
}
if tag = is.tag.Get("gt"); tag != "" {
n, err := strconv.ParseInt(tag, 0, bitSize)
if err != nil {
return &ValidationError{Value: val, Message: err.Error()}
}
if !(val > n) {
msg := fmt.Sprintf("%d is not greater than %d", val, n)
return &ValidationError{Value: val, Message: msg}
}
}
is.val.SetInt(val)
return nil
}
func (is *intSetter) SetInt(val int64) error {
ival := reflect.New(is.val.Type()).Elem()
ival.SetInt(val)
if ival.Int() != val {
return &ConversionError{Value: val, ToType: is.val.Type()}
}
return is.set(val)
}
func (is *intSetter) SetUint(val uint64) error {
t := is.val.Type()
ival := reflect.New(t).Elem()
ival.Set(reflect.ValueOf(val).Convert(t))
if i := ival.Int(); uint64(i) != val || i < 0 {
return &ConversionError{Value: val, ToType: is.val.Type()}
}
return is.set(ival.Int())
}
func (is *intSetter) SetFloat(val float64) error {
t := is.val.Type()
ival := reflect.New(t).Elem()
ival.Set(reflect.ValueOf(val).Convert(t))
if float64(ival.Int()) != val {
return &ConversionError{Value: val, ToType: is.val.Type()}
}
return is.set(ival.Int())
}
func (is *intSetter) SetBool(val bool) error {
if val {
return is.set(1)
}
return is.set(0)
}
func (is *intSetter) Get() interface{} {
if is.val.Kind() == reflect.Invalid {
return nil
}
return is.val.Interface()
}
type intSetterCreator struct {
t reflect.Type
}
func (isc intSetterCreator) Type() reflect.Type {
return isc.t
}
func (isc intSetterCreator) Setter(val reflect.Value, tag reflect.StructTag) Setter {
if val.Type() != isc.t {
panic(fmt.Sprintf("value must be type %s", isc.t))
}
return &intSetter{val: val, tag: tag}
}
func init() {
DefaultSetterRegistry.Add(intSetterCreator{t: intType})
DefaultSetterRegistry.Add(intSetterCreator{t: int8Type})
DefaultSetterRegistry.Add(intSetterCreator{t: int16Type})
DefaultSetterRegistry.Add(intSetterCreator{t: int32Type})
DefaultSetterRegistry.Add(intSetterCreator{t: int64Type})
}