-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmeasure.go
104 lines (85 loc) · 1.69 KB
/
measure.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
package validate
import (
"fmt"
)
type Min struct {
Min int
}
func (m Min) IsSatisfied(obj interface{}) bool {
num, ok := obj.(int)
if ok {
return num >= m.Min
}
return false
}
func (m Min) DefaultMessage() string {
return fmt.Sprint("Minimum is", m.Min)
}
type Max struct {
Max int
}
func (m Max) IsSatisfied(obj interface{}) bool {
num, ok := obj.(int)
if ok {
return num <= m.Max
}
return false
}
func (m Max) DefaultMessage() string {
return fmt.Sprint("Maximum is", m.Max)
}
type Range struct {
Min
Max
}
func (r Range) IsSatisfied(obj interface{}) bool {
return r.Min.IsSatisfied(obj) && r.Max.IsSatisfied(obj)
}
func (r Range) DefaultMessage() string {
return fmt.Sprint("Range is", r.Min.Min, "to", r.Max.Max)
}
type MinSize struct {
Min int
}
func (m MinSize) IsSatisfied(obj interface{}) bool {
if arr, ok := obj.([]interface{}); ok {
return len(arr) >= m.Min
}
if str, ok := obj.(string); ok {
return len(str) >= m.Min
}
return false
}
func (m MinSize) DefaultMessage() string {
return fmt.Sprint("Minimum size is", m.Min)
}
type MaxSize struct {
Max int
}
func (m MaxSize) IsSatisfied(obj interface{}) bool {
if arr, ok := obj.([]interface{}); ok {
return len(arr) <= m.Max
}
if str, ok := obj.(string); ok {
return len(str) <= m.Max
}
return false
}
func (m MaxSize) DefaultMessage() string {
return fmt.Sprint("Maximum size is", m.Max)
}
type Length struct {
N int
}
func (s Length) IsSatisfied(obj interface{}) bool {
if arr, ok := obj.([]interface{}); ok {
return len(arr) == s.N
}
if str, ok := obj.(string); ok {
return len(str) == s.N
}
return false
}
func (s Length) DefaultMessage() string {
return fmt.Sprint("Required length is", s.N)
}