/
country.go
66 lines (53 loc) · 1.31 KB
/
country.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
package model
import (
"fmt"
"github.com/ungerik/go-start/i18n"
)
type Country string
func (self *Country) Get() string {
return string(*self)
}
func (self *Country) Set(value string) error {
if value != "" {
// if _, ok := i18n.Countries()[value]; !ok {
// return &InvalidCountryCode{str}
// }
}
*self = Country(value)
return nil
}
func (self *Country) IsEmpty() bool {
return *self == ""
}
func (self *Country) String() string {
return self.Get()
}
func (self *Country) SetString(str string) error {
return self.Set(str)
}
func (self *Country) EnglishName() string {
return i18n.EnglishCountryName(self.Get())
}
func (self *Country) FixValue(metaData *MetaData) {
}
func (self *Country) Validate(metaData *MetaData) error {
str := self.Get()
if self.Required(metaData) || str != "" {
// if _, ok := i18n.Countries()[value]; !ok {
// return NewValidationErrors(&InvalidCountryCode{str}, metaData)
// }
}
if self.Required(metaData) && self.IsEmpty() {
return NewRequiredError(metaData)
}
return nil
}
func (self *Country) Required(metaData *MetaData) bool {
return metaData.BoolAttrib(StructTagKey, "required")
}
type InvalidCountryCode struct {
CountryCode string
}
func (self *InvalidCountryCode) String() string {
return fmt.Sprintf("Ivalid country code ''", self.CountryCode)
}