forked from rs/rest-layer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
float.go
83 lines (74 loc) · 1.66 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
package schema
import (
"errors"
"fmt"
)
// Boundaries defines min/max for an integer.
type Boundaries struct {
Min float64
Max float64
}
// Float validates float based values.
type Float struct {
Allowed []float64
Boundaries *Boundaries
}
// ValidateQuery implements schema.FieldQueryValidator interface
func (v Float) ValidateQuery(value interface{}) (interface{}, error) {
return v.parse(value)
}
func (v Float) get(value interface{}) (float64, error) {
f, ok := value.(float64)
if !ok {
return 0, errors.New("not a float")
}
return f, nil
}
// Validate validates and normalize float based value.
func (v Float) Validate(value interface{}) (interface{}, error) {
f, err := v.get(value)
if err != nil {
return nil, err
}
if v.Boundaries != nil {
if f < v.Boundaries.Min {
return nil, fmt.Errorf("is lower than %.2f", v.Boundaries.Min)
}
if f > v.Boundaries.Max {
return nil, fmt.Errorf("is greater than %.2f", v.Boundaries.Max)
}
}
if len(v.Allowed) > 0 {
found := false
for _, allowed := range v.Allowed {
if f == allowed {
found = true
break
}
}
if !found {
// TODO: build the list of allowed values.
return nil, fmt.Errorf("not one of the allowed values")
}
}
return f, nil
}
func (v Float) parse(value interface{}) (interface{}, error) {
f, ok := value.(float64)
if !ok {
return nil, errors.New("not a float")
}
return f, nil
}
// LessFunc implements the FieldComparator interface.
func (v Float) LessFunc() LessFunc {
return v.less
}
func (v Float) less(value, other interface{}) bool {
t, err1 := v.get(value)
o, err2 := v.get(other)
if err1 != nil || err2 != nil {
return false
}
return t < o
}