-
Notifications
You must be signed in to change notification settings - Fork 0
/
number.go
152 lines (130 loc) · 3.27 KB
/
number.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
package buzz
import (
"fmt"
"reflect"
)
type AnyNumber interface {
~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~float32 | ~float64
}
type BuzzNumberValidateFunc[T AnyNumber] func(v T) error
type BuzzNumber[T AnyNumber] struct {
name string
validateFuncs []BuzzNumberValidateFunc[T]
refType reflect.Type
}
func Number[T AnyNumber]() *BuzzNumber[T] {
return &BuzzNumber[T]{
refType: reflect.TypeOf(*new(T)),
}
}
func (n *BuzzNumber[T]) Name() string {
return n.name
}
func (n *BuzzNumber[T]) Type() reflect.Type {
return n.refType
}
func (n *BuzzNumber[T]) Validate(v any) error {
vint, ok := v.(T)
if !ok {
return fmt.Errorf(invalidTypeMsg, n.refType, v)
}
errAggr := NewFieldErrorAggregator()
for _, valFn := range n.validateFuncs {
if err := valFn(vint); err != nil {
if errAggr.Handle(err) != nil {
return err
}
}
}
return errAggr.OrNil()
}
func (n *BuzzNumber[T]) WithName(name string) BuzzField {
n.name = name
return n
}
func (n *BuzzNumber[T]) Clone() BuzzField {
return &BuzzNumber[T]{
name: n.name,
validateFuncs: n.validateFuncs,
refType: n.refType,
}
}
func (n *BuzzNumber[T]) Gt(num T) *BuzzNumber[T] {
n.registerValidateFunc(func(v T) error {
if v > num {
return nil
}
return MakeFieldError(n.name, "Gt", fmt.Sprintf("%s must be greater than %v", n.name, num))
})
return n
}
func (n *BuzzNumber[T]) Gte(num T) *BuzzNumber[T] {
n.registerValidateFunc(func(v T) error {
if v >= num {
return nil
}
return MakeFieldError(n.name, "Gte", fmt.Sprintf("%s must be greate than or equal to %v", n.name, num))
})
return n
}
func (n *BuzzNumber[T]) Lt(num T) *BuzzNumber[T] {
n.registerValidateFunc(func(v T) error {
if v < num {
return nil
}
return MakeFieldError(n.name, "Lt", fmt.Sprintf("%s must be less than %v", n.name, num))
})
return n
}
func (n *BuzzNumber[T]) Lte(num T) *BuzzNumber[T] {
n.registerValidateFunc(func(v T) error {
if v <= num {
return nil
}
return MakeFieldError(n.name, "Lte", fmt.Sprintf("%s must be less than or equal to %v", n.name, num))
})
return n
}
func (n *BuzzNumber[T]) Positive() *BuzzNumber[T] {
n.registerValidateFunc(func(v T) error {
if v <= 0 {
return MakeFieldError(n.name, "Positive", fmt.Sprintf("%s must be positive", n.name))
}
return nil
})
return n
}
func (n *BuzzNumber[T]) Nonnegative() *BuzzNumber[T] {
n.registerValidateFunc(func(v T) error {
if v < 0 {
return MakeFieldError(n.name, "Nonnegative", fmt.Sprintf("%s must be nonnegative", n.name))
}
return nil
})
return n
}
func (n *BuzzNumber[T]) Negative() *BuzzNumber[T] {
n.registerValidateFunc(func(v T) error {
if v >= 0 {
return MakeFieldError(n.name, "Negative", fmt.Sprintf("%s must be negative", n.name))
}
return nil
})
return n
}
func (n *BuzzNumber[T]) Nonpositive() *BuzzNumber[T] {
n.registerValidateFunc(func(v T) error {
if v > 0 {
return MakeFieldError(n.name, "Nonpositive", fmt.Sprintf("%s must be nonpositive", n.name))
}
return nil
})
return n
}
func (n *BuzzNumber[T]) Custom(fn BuzzNumberValidateFunc[T]) *BuzzNumber[T] {
n.registerValidateFunc(fn)
return n
}
func (n *BuzzNumber[T]) registerValidateFunc(fn BuzzNumberValidateFunc[T]) {
n.validateFuncs = append(n.validateFuncs, fn)
}