-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfloat.go
159 lines (138 loc) · 3.54 KB
/
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
package config
import (
"fmt"
"reflect"
"strconv"
)
var float32Type = reflect.TypeOf(float32(0))
var float64Type = reflect.TypeOf(float64(0))
type floatSetter struct {
val reflect.Value
tag reflect.StructTag
}
func (fs *floatSetter) String() string {
if fs.val.Kind() == reflect.Invalid {
return "0"
}
return strconv.FormatFloat(fs.val.Float(), 'f', -1, fs.bitSize())
}
func (fs *floatSetter) bitSize() int {
if fs.val.Type() == float32Type {
return 32
}
return 64
}
func (fs *floatSetter) Set(val string) error {
fval, err := strconv.ParseFloat(val, fs.bitSize())
if err != nil {
return &ConversionError{Value: val, ToType: fs.val.Type()}
}
return fs.set(fval)
}
func (fs *floatSetter) set(val float64) error {
bitSize := fs.bitSize()
tag := fs.tag.Get("le")
if tag == "" {
tag = fs.tag.Get("max")
}
if tag != "" {
n, err := strconv.ParseFloat(tag, bitSize)
if err != nil {
return &ValidationError{Value: val, Message: err.Error()}
}
if !(val <= n) {
msg := fmt.Sprintf("%f is not less than or equal to %f", val, n)
return &ValidationError{Value: val, Message: msg}
}
}
tag = fs.tag.Get("ge")
if tag == "" {
tag = fs.tag.Get("min")
}
if tag != "" {
n, err := strconv.ParseFloat(tag, bitSize)
if err != nil {
return &ValidationError{Value: val, Message: err.Error()}
}
if !(val >= n) {
msg := fmt.Sprintf("%f is not greater than or equal to %f", val, n)
return &ValidationError{Value: val, Message: msg}
}
}
if tag = fs.tag.Get("lt"); tag != "" {
n, err := strconv.ParseFloat(tag, bitSize)
if err != nil {
return &ValidationError{Value: val, Message: err.Error()}
}
if !(val < n) {
msg := fmt.Sprintf("%f is not less than %f", val, n)
return &ValidationError{Value: val, Message: msg}
}
}
if tag = fs.tag.Get("gt"); tag != "" {
n, err := strconv.ParseFloat(tag, bitSize)
if err != nil {
return &ValidationError{Value: val, Message: err.Error()}
}
if !(val > n) {
msg := fmt.Sprintf("%f is not greater than %f", val, n)
return &ValidationError{Value: val, Message: msg}
}
}
fs.val.SetFloat(val)
return nil
}
func (fs *floatSetter) SetInt(val int64) error {
t := fs.val.Type()
fval := reflect.New(t).Elem()
fval.Set(reflect.ValueOf(val).Convert(t))
if int64(fval.Float()) != val {
return &ConversionError{Value: val, ToType: fs.val.Type()}
}
return fs.set(fval.Float())
}
func (fs *floatSetter) SetUint(val uint64) error {
t := fs.val.Type()
fval := reflect.New(t).Elem()
fval.Set(reflect.ValueOf(val).Convert(t))
if uint64(fval.Float()) != val {
return &ConversionError{Value: val, ToType: fs.val.Type()}
}
return fs.set(fval.Float())
}
func (fs *floatSetter) SetFloat(val float64) error {
fval := reflect.New(fs.val.Type()).Elem()
fval.SetFloat(val)
if fval.Float() != val {
return &ConversionError{Value: val, ToType: fs.val.Type()}
}
return fs.set(val)
}
func (fs *floatSetter) SetBool(val bool) error {
if val {
return fs.set(1)
}
return fs.set(0)
}
func (fs *floatSetter) Get() interface{} {
if fs.val.Kind() == reflect.Invalid {
return nil
}
return fs.val.Interface()
}
type floatSetterCreator struct {
t reflect.Type
}
func (fsc floatSetterCreator) Type() reflect.Type {
return fsc.t
}
func (fsc floatSetterCreator) Setter(val reflect.Value, tag reflect.StructTag) Setter {
if val.Type() != fsc.t {
panic(fmt.Sprintf("value must be type %s", fsc.t))
}
return &floatSetter{val: val, tag: tag}
}
func init() {
DefaultSetterRegistry.Add(floatSetterCreator{t: float32Type})
DefaultSetterRegistry.Add(floatSetterCreator{t: float64Type})
}