-
Notifications
You must be signed in to change notification settings - Fork 34
/
validator__float.go
239 lines (202 loc) · 5.21 KB
/
validator__float.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package validator
import (
"context"
"fmt"
"reflect"
"strconv"
"github.com/machinefi/w3bstream/pkg/depends/kit/validator/errors"
"github.com/machinefi/w3bstream/pkg/depends/kit/validator/rules"
"github.com/machinefi/w3bstream/pkg/depends/x/ptrx"
"github.com/machinefi/w3bstream/pkg/depends/x/typesx"
)
type Float struct {
MaxDigits uint
DecimalDigits *uint
Minimum *float64
Maximum *float64
ExclusiveMaximum bool
ExclusiveMinimum bool
MultipleOf float64
Enums map[float64]string
}
func init() { DefaultFactory.Register(&Float{}) }
func (vf *Float) SetDefault() {
if vf != nil {
if vf.MaxDigits == 0 {
vf.MaxDigits = 7
}
if vf.DecimalDigits == nil {
vf.DecimalDigits = ptrx.Uint(2)
}
}
}
func (Float) Names() []string {
return []string{"float", "double", "float32", "float64"}
}
var (
TargetFloatValue = "float value"
TargetDecimalDigitsOfFloatValue = "decimal digits of float value"
TargetTotalDigitsOfFloatValue = "total digits of float value"
)
func (vf *Float) Validate(v interface{}) error {
rv, ok := v.(reflect.Value)
if !ok {
rv = reflect.ValueOf(v)
}
if k := rv.Type().Kind(); !typesx.IsFloatReflectKind(k) {
return errors.NewUnsupportedTypeError(rv.Type().String(), vf.String())
}
val := rv.Float()
decimal := *vf.DecimalDigits
m, d := FloatLengthOfDigit(val)
if m > vf.MaxDigits {
return &errors.OutOfRangeError{
Target: TargetTotalDigitsOfFloatValue,
Current: m,
Maximum: vf.MaxDigits,
}
}
if d > decimal {
return &errors.OutOfRangeError{
Target: TargetDecimalDigitsOfFloatValue,
Current: d,
Maximum: decimal,
}
}
if vf.Enums != nil {
if _, ok := vf.Enums[val]; !ok {
values := make([]interface{}, 0)
for _, v := range vf.Enums {
values = append(values, v)
}
return &errors.NotInEnumError{
Target: TargetFloatValue,
Current: v,
Enums: values,
}
}
return nil
}
if vf.Minimum != nil {
minimum := *vf.Minimum
if (vf.ExclusiveMinimum && val == minimum) || val < minimum {
return &errors.OutOfRangeError{
Target: TargetFloatValue,
Current: val,
Minimum: minimum,
ExclusiveMinimum: vf.ExclusiveMinimum,
}
}
}
if vf.Maximum != nil {
maximum := *vf.Maximum
if (vf.ExclusiveMaximum && val == maximum) || val > maximum {
return &errors.OutOfRangeError{
Target: TargetFloatValue,
Current: val,
Maximum: maximum,
ExclusiveMaximum: vf.ExclusiveMaximum,
}
}
}
if vf.MultipleOf != 0 {
if !IsFloatMultipleOf(val, vf.MultipleOf, decimal) {
return &errors.MultipleOfError{
Target: TargetFloatValue,
Current: val,
MultipleOf: vf.MultipleOf,
}
}
}
return nil
}
func (Float) New(ctx context.Context, r *Rule) (Validator, error) {
vf := &Float{}
switch r.Name {
case "float", "float32":
vf.MaxDigits = 7
case "double", "float64":
vf.MaxDigits = 15
}
// @float<max_digit,max_decimal>
// @float<5,2> 1.111(too many decimal) 12345.67 (too many digits)
if len(r.Params) > 0 {
digits, decimal, err := FloatRuleParam(r)
if err != nil {
return nil, err
}
vf.MaxDigits = uint(digits)
if decimal != nil {
vf.DecimalDigits = ptrx.Uint(uint(*decimal))
}
}
vf.SetDefault()
vf.ExclusiveMinimum = r.ExclusiveMin
vf.ExclusiveMaximum = r.ExclusiveMax
// @float[min,max]
// @float(min,max]
// @float[min,max)
// @float(min,max)
min, max, err := FloatRuleRange(r, vf.MaxDigits, vf.DecimalDigits)
if err != nil {
return nil, err
}
vf.Minimum, vf.Maximum = min, max
// @float{1.1,2.2,3.3} should be one of these
// @float{%2.2} should be multiple of 2.2
multiple, enums, err := FloatRuleValues(r, vf.MaxDigits, vf.DecimalDigits)
if err != nil {
return nil, err
}
vf.MultipleOf = multiple
vf.Enums = enums
return vf, vf.TypeCheck(r)
}
func (vf *Float) TypeCheck(r *Rule) error {
switch r.Type.Kind() {
case reflect.Float32:
if vf.MaxDigits > 7 {
return fmt.Errorf("max digits too large for type %s", r)
}
return nil
case reflect.Float64:
return nil
}
return errors.NewUnsupportedTypeError(r.String(), vf.String())
}
func (vf *Float) String() string {
vf.SetDefault()
decimal := *vf.DecimalDigits
rule := rules.NewRule(vf.Names()[0])
rule.Params = []rules.Node{
rules.NewLiteral([]byte(strconv.Itoa(int(vf.MaxDigits)))),
rules.NewLiteral([]byte(strconv.Itoa(int(decimal)))),
}
if vf.Minimum != nil || vf.Maximum != nil {
rule.Range = make([]*rules.Lit, 2)
if vf.Minimum != nil {
rule.Range[0] = rules.NewLiteral(
[]byte(fmt.Sprintf("%."+strconv.Itoa(int(decimal))+"f", *vf.Minimum)),
)
}
if vf.Maximum != nil {
rule.Range[1] = rules.NewLiteral(
[]byte(fmt.Sprintf("%."+strconv.Itoa(int(decimal))+"f", *vf.Maximum)),
)
}
rule.ExclusiveMin = vf.ExclusiveMinimum
rule.ExclusiveMax = vf.ExclusiveMaximum
}
if vf.MultipleOf != 0 {
rule.ValueMatrix = [][]*rules.Lit{{
rules.NewLiteral([]byte("%" + fmt.Sprintf("%."+strconv.Itoa(int(decimal))+"f", vf.MultipleOf))),
}}
} else if vf.Enums != nil {
values := make([]*rules.Lit, 0)
for _, str := range vf.Enums {
values = append(values, rules.NewLiteral([]byte(str)))
}
rule.ValueMatrix = [][]*rules.Lit{values}
}
return string(rule.Bytes())
}