-
Notifications
You must be signed in to change notification settings - Fork 10
/
float.go
175 lines (149 loc) · 3.01 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package meta
import (
"database/sql/driver"
"encoding/json"
"reflect"
"strconv"
"strings"
)
//
// Float64
//
type Float64 struct {
Val float64
Nullity
Presence
Path string
}
type FloatOptions struct {
Required bool
DiscardBlank bool
Null bool
MinPresent bool
Min float64
MaxPresent bool
Max float64
In []float64
}
func NewFloat64(f float64) Float64 {
return Float64{f, Nullity{false}, Presence{true}, ""}
}
func (i *Float64) ParseOptions(tag reflect.StructTag) interface{} {
opts := &FloatOptions{
DiscardBlank: true,
}
if tag.Get("meta_required") == "true" {
opts.Required = true
}
if tag.Get("meta_discard_blank") == "false" {
opts.DiscardBlank = false
}
if tag.Get("meta_null") == "true" {
opts.Null = true
}
if nstr := tag.Get("meta_min"); nstr != "" {
n, err := strconv.ParseFloat(nstr, 64)
if err != nil {
panic(err.Error())
}
opts.MinPresent = true
opts.Min = n
}
if nstr := tag.Get("meta_max"); nstr != "" {
n, err := strconv.ParseFloat(nstr, 64)
if err != nil {
panic(err.Error())
}
opts.MaxPresent = true
opts.Max = n
}
if in := tag.Get("meta_in"); in != "" {
for _, s := range strings.Split(in, ",") {
n, err := strconv.ParseFloat(strings.TrimSpace(s), 64)
if err != nil {
panic(err.Error())
}
opts.In = append(opts.In, n)
}
}
return opts
}
func (f *Float64) JSONValue(path string, i interface{}, options interface{}) Errorable {
f.Path = path
if i == nil {
return f.FormValue("", options)
}
switch value := i.(type) {
case float64:
return f.validateValue(value, options)
case json.Number:
return f.FormValue(string(value), options)
case string:
return f.FormValue(string(value), options)
}
return ErrFloat
}
func (i *Float64) FormValue(value string, options interface{}) Errorable {
opts := options.(*FloatOptions)
if value == "" {
if opts.Null {
i.Null = true
i.Present = true
return nil
}
if opts.Required {
return ErrBlank
}
if !opts.DiscardBlank {
i.Present = true
return ErrBlank
}
return nil
}
if n, err := strconv.ParseFloat(value, 64); err == nil {
return i.validateValue(n, options)
} else {
numError := err.(*strconv.NumError)
if numError.Err == strconv.ErrRange {
return ErrFloatRange
} else {
return ErrFloat
}
}
return nil
}
func (i *Float64) validateValue(value float64, options interface{}) Errorable {
opts := options.(*FloatOptions)
if opts.MinPresent && value < opts.Min {
return ErrMin
}
if opts.MaxPresent && value > opts.Max {
return ErrMax
}
if len(opts.In) > 0 {
found := false
for _, i := range opts.In {
if i == value {
found = true
}
}
if !found {
return ErrIn
}
}
i.Val = value
i.Present = true
return nil
}
func (i Float64) Value() (driver.Value, error) {
if i.Present && !i.Null {
return float64(i.Val), nil
}
return nil, nil
}
func (i Float64) MarshalJSON() ([]byte, error) {
if i.Present && !i.Null {
return MetaJson.Marshal(i.Val)
}
return nullString, nil
}