forked from ungerik/go-start
/
bool.go
49 lines (39 loc) · 816 Bytes
/
bool.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
package model
import "strconv"
func NewBool(value bool) *Bool {
return (*Bool)(&value)
}
/*
Bool model value.
Struct tag attributes:
required
*/
type Bool bool
func (self *Bool) Get() bool {
return bool(*self)
}
func (self *Bool) Set(value bool) {
*self = Bool(value)
}
func (self *Bool) String() string {
return strconv.FormatBool(self.Get())
}
func (self *Bool) SetString(str string) error {
value, err := strconv.ParseBool(str)
if err == nil {
self.Set(value)
}
return err
}
func (self *Bool) IsEmpty() bool {
return false
}
func (self *Bool) Required(metaData *MetaData) bool {
return metaData.BoolAttrib(StructTagKey, "required")
}
func (self *Bool) Validate(metaData *MetaData) error {
if self.Required(metaData) && *self == false {
return NewRequiredError(metaData)
}
return nil
}