-
Notifications
You must be signed in to change notification settings - Fork 42
/
types.go
265 lines (219 loc) · 5.08 KB
/
types.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package model
import "strconv"
type AdVersion int
const (
AdVersion_DEFAULT AdVersion = 1
AdVersion_2 AdVersion = 2
)
// Uint64 support string quoted number in json
type Uint64 uint64
// UnmarshalJSON implement json Unmarshal interface
func (u64 *Uint64) UnmarshalJSON(b []byte) (err error) {
if b[0] == '"' && b[len(b)-1] == '"' {
b = b[1 : len(b)-1]
}
i, _ := strconv.ParseUint(string(b), 10, 64)
*u64 = Uint64(i)
return
}
func (u64 Uint64) Value() uint64 {
return uint64(u64)
}
func (u64 Uint64) String() string {
return strconv.FormatUint(uint64(u64), 10)
}
// JSONUint64 support string quoted number in json and marshal to string
type JSONUint64 uint64
func JSONUint64FromUint64(v uint64) JSONUint64 {
return JSONUint64(v)
}
// UnmarshalJSON implement json Unmarshal interface
func (u64 *JSONUint64) UnmarshalJSON(b []byte) (err error) {
if b[0] == '"' && b[len(b)-1] == '"' {
b = b[1 : len(b)-1]
}
i, _ := strconv.ParseUint(string(b), 10, 64)
*u64 = JSONUint64(i)
return
}
func (u64 JSONUint64) MarshalJSON() ([]byte, error) {
return []byte(`"` + strconv.FormatUint(uint64(u64), 10) + `"`), nil
}
func (u64 JSONUint64) Value() uint64 {
return uint64(u64)
}
// Int64 support string quoted number in json
type Int64 int64
// UnmarshalJSON implement json Unmarshal interface
func (i64 *Int64) UnmarshalJSON(b []byte) (err error) {
if b[0] == '"' && b[len(b)-1] == '"' {
b = b[1 : len(b)-1]
}
i, _ := strconv.ParseInt(string(b), 10, 64)
*i64 = Int64(i)
return
}
func (i64 Int64) Value() int64 {
return int64(i64)
}
func (i64 Int64) String() string {
return strconv.FormatInt(int64(i64), 10)
}
// JSONInt64 support string quoted number in json and marshal to string
type JSONInt64 int64
func JSONInt64FromInt64(v int64) JSONInt64 {
return JSONInt64(v)
}
// UnmarshalJSON implement json Unmarshal interface
func (i64 *JSONInt64) UnmarshalJSON(b []byte) (err error) {
if b[0] == '"' && b[len(b)-1] == '"' {
b = b[1 : len(b)-1]
}
i, _ := strconv.ParseInt(string(b), 10, 64)
*i64 = JSONInt64(i)
return
}
func (i64 JSONInt64) MarshalJSON() ([]byte, error) {
return []byte(`"` + strconv.FormatInt(int64(i64), 10) + `"`), nil
}
func (i64 JSONInt64) Value() int64 {
return int64(i64)
}
// Int support string quoted number in json
type Int int
// UnmarshalJSON implement json Unmarshal interface
func (i *Int) UnmarshalJSON(b []byte) (err error) {
if b[0] == '"' && b[len(b)-1] == '"' {
b = b[1 : len(b)-1]
}
v, _ := strconv.Atoi(string(b))
*i = Int(v)
return
}
func (i Int) Value() int {
return int(i)
}
func (i Int) String() string {
return strconv.Itoa(int(i))
}
// JSONInt support string quoted number in json and marshal to string
type JSONInt int
func JSONIntFromInt(v int) JSONInt {
return JSONInt(v)
}
// UnmarshalJSON implement json Unmarshal interface
func (i *JSONInt) UnmarshalJSON(b []byte) (err error) {
if b[0] == '"' && b[len(b)-1] == '"' {
b = b[1 : len(b)-1]
}
v, _ := strconv.Atoi(string(b))
*i = JSONInt(v)
return
}
func (i JSONInt) MarshalJSON() ([]byte, error) {
return []byte(`"` + strconv.FormatInt(int64(i), 10) + `"`), nil
}
func (i JSONInt) Value() int {
return int(i)
}
// Float64 support string quoted number in json
type Float64 float64
// UnmarshalJSON implement json Unmarshal interface
func (f64 *Float64) UnmarshalJSON(b []byte) (err error) {
if b[0] == '"' && b[len(b)-1] == '"' {
b = b[1 : len(b)-1]
}
i, _ := strconv.ParseFloat(string(b), 64)
*f64 = Float64(i)
return
}
func (f64 Float64) Value() float64 {
return float64(f64)
}
func (f64 Float64) String(prec int) string {
return strconv.FormatFloat(float64(64), 'f', prec, 64)
}
// Bool support number/string in json
type Bool bool
// UnmarshalJSON implement json Unmarshal interface
func (bl *Bool) UnmarshalJSON(b []byte) (err error) {
if b[0] == '"' && b[len(b)-1] == '"' {
b = b[1 : len(b)-1]
}
var ret bool
str := string(b)
if str == "true" {
ret = true
} else if str == "false" {
ret = false
} else if i, err := strconv.ParseInt(str, 10, 64); err != nil {
return err
} else if i == 0 {
ret = false
} else {
ret = true
}
*bl = Bool(ret)
return
}
func (b Bool) Value() bool {
return bool(b)
}
func (b Bool) String() string {
if b {
return "true"
}
return "false"
}
type OnOffInt int
func (ooi *OnOffInt) UnmarshalJSON(b []byte) (err error) {
if b[0] == '"' && b[len(b)-1] == '"' {
b = b[1 : len(b)-1]
}
str := string(b)
var i int
if str == "ON" {
i = 1
} else if str == "OFF" {
i = 0
} else {
i, _ = strconv.Atoi(str)
}
*ooi = OnOffInt(i)
return nil
}
func (i OnOffInt) Value() int {
return int(i)
}
func (i OnOffInt) String() string {
if i == 1 {
return "ON"
}
return "OFF"
}
type ReverseOnOffInt int
func (ooi *ReverseOnOffInt) UnmarshalJSON(b []byte) (err error) {
if b[0] == '"' && b[len(b)-1] == '"' {
b = b[1 : len(b)-1]
}
str := string(b)
var i int
if str == "ON" {
i = 0
} else if str == "OFF" {
i = 1
} else {
i, _ = strconv.Atoi(str)
}
*ooi = ReverseOnOffInt(i)
return nil
}
func (i ReverseOnOffInt) Value() int {
return int(i)
}
func (i ReverseOnOffInt) String() string {
if i == 1 {
return "OFF"
}
return "ON"
}