-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkarg.go
179 lines (148 loc) · 3.22 KB
/
checkarg.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
168
169
170
171
172
173
174
175
176
177
178
179
package util
import (
"fmt"
"reflect"
"time"
)
type Checker interface {
Check() bool
Error() string
}
type StringChecker struct {
Value string
}
func (c *StringChecker) Check() bool {
return c.Value != ""
}
func (c *StringChecker) Error() string {
return "StringChecker value is empty"
}
type TimeChecker struct {
Value time.Time
}
func (c *TimeChecker) Check() bool {
return !IsInvalidTime(c.Value)
}
func (c *TimeChecker) Error() string {
return "TimeChecker value is invalid"
}
type UnixMillisecondChecker struct {
Value int64
}
func (c *UnixMillisecondChecker) Check() bool {
return c.Value > 0
}
func (c *UnixMillisecondChecker) Error() string {
return "UnixMillisecondChecker value is invalid"
}
type InterfaceChecker struct {
Value interface{}
}
func (c *InterfaceChecker) Check() bool {
return c.Value != nil && !reflect.ValueOf(c.Value).IsNil()
}
func (c *InterfaceChecker) Error() string {
return "InterfaceChecker value is nil"
}
type StringArrayChecker struct {
Value []string
error string
}
func (c *StringArrayChecker) Check() bool {
if c.Value == nil {
c.error = "StringArrayChecker value is nil"
return false
}
for index, value := range c.Value {
if value == "" {
c.error = fmt.Sprintf("StringArrayChecker item %d is empty", index)
return false
}
}
return true
}
func (c *StringArrayChecker) Error() string {
return c.error
}
type IntegerChecker struct {
Value int64
Min int64
Max int64
error string
}
func (c *IntegerChecker) Check() bool {
if c.Value < c.Min {
c.error = fmt.Sprintf("IntegerChecker value %d < min %d", c.Value, c.Min)
return false
}
if c.Value > c.Max {
c.error = fmt.Sprintf("IntegerChecker value %d > max %d", c.Value, c.Max)
return false
}
return true
}
func (c *IntegerChecker) Error() string {
return c.error
}
type DecimalChecker struct {
Value float64
Min float64
Max float64
error string
}
func (c *DecimalChecker) Check() bool {
if c.Value < c.Min {
c.error = fmt.Sprintf("DecimalChecker value %f < min %f", c.Value, c.Min)
return false
}
if c.Value > c.Max {
c.error = fmt.Sprintf("DecimalChecker value %f > max %f", c.Value, c.Max)
return false
}
return true
}
func (c *DecimalChecker) Error() string {
return c.error
}
type UUIDChecker struct {
Value string
}
func (c *UUIDChecker) Check() bool {
return !IsInvalidUUID(c.Value)
}
func (c *UUIDChecker) Error() string {
return "UUIDChecker value is invalid"
}
type UUIDArrayChecker struct {
Value []string
error string
}
func (c *UUIDArrayChecker) Check() bool {
if c.Value == nil {
c.error = "UUIDArrayChecker value is nil"
return false
}
for index, value := range c.Value {
if IsInvalidUUID(value) {
c.error = fmt.Sprintf("UUIDArrayChecker item %d is invalid", index)
return false
}
}
return true
}
func (c *UUIDArrayChecker) Error() string {
return c.error
}
func getCheckCallerName() (name string) {
return GetCallerName(2)
}
func CheckArgs(fields map[string]Checker) (err error) {
for key, checker := range fields {
if checker == nil {
err = fmt.Errorf("%s check %s error: checker is nil", getCheckCallerName(), key)
} else if !checker.Check() {
err = fmt.Errorf("%s check %s error: %s", getCheckCallerName(), key, checker.Error())
}
}
return
}