/
int.go
105 lines (87 loc) · 1.87 KB
/
int.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
package model
import (
"fmt"
"strconv"
)
func NewInt(value int64) *Int {
return (*Int)(&value)
}
// Attributes:
// * min
// * max
// * label
type Int int64
func (self *Int) Get() int64 {
return int64(*self)
}
func (self *Int) Set(value int64) {
*self = Int(value)
}
func (self *Int) GetInt() int {
return int(*self)
}
func (self *Int) SetInt(value int) {
*self = Int(value)
}
func (self *Int) String() string {
return strconv.FormatInt(self.Get(), 10)
}
func (self *Int) SetString(str string) error {
value, err := strconv.ParseInt(str, 10, 64)
if err == nil {
self.Set(value)
}
return err
}
func (self *Int) IsEmpty() bool {
return false
}
func (self *Int) Required(metaData *MetaData) bool {
return true
}
func (self *Int) Validate(metaData *MetaData) error {
value := int64(*self)
min, ok, err := self.Min(metaData)
if err != nil {
return err
} else if ok && value < min {
return &IntBelowMin{value, min}
}
max, ok, err := self.Max(metaData)
if err != nil {
return err
} else if ok && value > max {
return &IntAboveMax{value, max}
}
return nil
}
func (self *Int) Min(metaData *MetaData) (min int64, ok bool, err error) {
str, ok := metaData.Attrib(StructTagKey, "min")
if !ok {
return 0, false, nil
}
value, err := strconv.ParseInt(str, 10, 64)
return value, err == nil, err
}
func (self *Int) Max(metaData *MetaData) (max int64, ok bool, err error) {
str, ok := metaData.Attrib(StructTagKey, "max")
if !ok {
return 0, false, nil
}
value, err := strconv.ParseInt(str, 10, 64)
return value, err == nil, err
}
type IntBelowMin struct {
Value int64
Min int64
}
func (self *IntBelowMin) Error() string {
return fmt.Sprintf("Int %d below minimum of %d", self.Value, self.Min)
}
type IntAboveMax struct {
Value int64
Max int64
}
func (self *IntAboveMax) Error() string {
return fmt.Sprintf("Int %d above maximum of %d", self.Value, self.Max)
}