-
Notifications
You must be signed in to change notification settings - Fork 42
/
types.go
129 lines (111 loc) · 2.62 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
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
}
// JSONUint64 support string quoted number in json and marshal to string
type JSONUint64 uint64
// 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
}
// 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
}
// 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
}
// 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
}
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
}
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
}