-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrapper-country.go
108 lines (85 loc) · 1.99 KB
/
wrapper-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
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
106
107
108
package wrappers
import (
"fmt"
"github.com/biter777/countries"
)
const (
WrapperCountryName Name = "WrapperCountry"
)
type WrapperCountry Wrapper[countries.CountryCode, string]
var _ WrapperProvider = (*WrapperCountry)(nil) // Ensure that WrapperCountry implements WrapperProvider.
func (wrapper *WrapperCountry) Get() countries.CountryCode {
return wrapper.Value
}
func (wrapper *WrapperCountry) GetAny() any {
return wrapper.Get()
}
func (wrapper *WrapperCountry) Wrap(value any, discard bool) error {
switch v := value.(type) {
case nil:
wrapper.Discard()
if !discard {
return ErrorNil(WrapperCountryName)
}
case WrapperProvider:
if v.IsDiscarded() {
wrapper.Discard()
return nil
}
return wrapper.Wrap(v.UnwrapAny(), discard)
case countries.CountryCode:
if v == countries.Unknown {
wrapper.Discard()
if !discard {
return ErrorValue(WrapperCountryName, value, "DE")
}
}
wrapper.Value = v
case countries.Country:
if v.Code == countries.Unknown {
wrapper.Discard()
if !discard {
return ErrorValue(WrapperCountryName, value, "DE")
}
}
wrapper.Value = v.Code
case string:
if v == "Unknown" {
wrapper.Discard()
return nil
}
code := countries.ByName(v)
if code != countries.Unknown {
wrapper.Value = code
} else {
wrapper.Discard()
if !discard {
return ErrorValue(WrapperCountryName, value, "DE")
}
}
default:
wrapper.Discard()
if !discard {
return ErrorType(WrapperCountryName, value)
}
}
return nil
}
func (wrapper *WrapperCountry) Unwrap() string {
if wrapper.IsDiscarded() {
return countries.Unknown.String()
}
return wrapper.Value.String()
}
func (wrapper *WrapperCountry) UnwrapAny() any {
return wrapper.Unwrap()
}
func (wrapper *WrapperCountry) MarshalJSON() ([]byte, error) {
return MarshalJSON(wrapper)
}
func (wrapper *WrapperCountry) UnmarshalJSON(data []byte) error {
if wrapper == nil {
return fmt.Errorf("unmarshal into nil wrapper")
}
return UnmarshalJSON(data, wrapper)
}