-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.go
176 lines (143 loc) · 3.1 KB
/
util.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
package woocommerce
import (
"encoding/json"
"strconv"
"time"
)
const timeFormat = "2006-01-02T15:04:05"
// Float is a support type that marshals itself to string in JSON.
type Float float64
func (f Float) MarshalJSON() ([]byte, error) {
valStr := strconv.FormatFloat(float64(f), 'f', -1, 64)
return json.Marshal(valStr)
}
func (f *Float) UnmarshalJSON(bytes []byte) error {
// Try to unmarshal into float.
var val float64
err := json.Unmarshal(bytes, &val)
if err == nil {
*f = Float(val)
return nil
}
// Unmarshal to float failed. Try to unmarshal to string and
// convert the string to float.
var valStr string
err = json.Unmarshal(bytes, &valStr)
if err != nil {
return err
}
// Convert to float
val, err = strconv.ParseFloat(valStr, 64)
if err != nil {
return err
}
*f = Float(val)
return nil
}
// NullFloat is a support type that marshals itself to string in JSON.
type NullFloat struct {
Float Float
Valid bool
}
func (f NullFloat) MarshalJSON() ([]byte, error) {
if !f.Valid {
return []byte("null"), nil
}
return json.Marshal(f.Float)
}
func (f *NullFloat) UnmarshalJSON(bytes []byte) error {
str := string(bytes)
if str == "null" || str == "\"\"" {
f.Valid = false
return nil
}
if err := json.Unmarshal(bytes, &f.Float); err != nil {
return err
}
f.Valid = true
return nil
}
// Time is a support type that marshals itself into JSON
// without timezone.
type Time struct {
time.Time
}
func (t Time) MarshalJSON() ([]byte, error) {
val := t.Format(timeFormat)
return json.Marshal(val)
}
func (t *Time) UnmarshalJSON(bytes []byte) error {
var valStr string
err := json.Unmarshal(bytes, &valStr)
if err != nil {
return err
}
t.Time, err = time.Parse(timeFormat, valStr)
return err
}
type NullTime struct {
Time Time
Valid bool
}
func (t NullTime) MarshalJSON() ([]byte, error) {
if !t.Valid {
return []byte("null"), nil
}
return json.Marshal(t.Time)
}
func (t *NullTime) UnmarshalJSON(bytes []byte) error {
if string(bytes) == "null" {
t.Valid = false
return nil
}
if err := json.Unmarshal(bytes, &t.Time); err != nil {
return err
}
t.Valid = true
return nil
}
type Int int64
func (i Int) MarshalJSON() ([]byte, error) {
val := strconv.FormatInt(int64(i), 10)
return json.Marshal(val)
}
func (i *Int) UnmarshalJSON(bytes []byte) error {
var val int64
err := json.Unmarshal(bytes, &val)
if err == nil {
*i = Int(val)
return nil
}
// Unmarshal to int64 failed. Try to unmarshal to string and
// convert the string to int64.
var valStr string
err = json.Unmarshal(bytes, &valStr)
if err != nil {
return err
}
// Convert to int64
val, err = strconv.ParseInt(valStr, 10, 64)
if err != nil {
return err
}
*i = Int(val)
return nil
}
// String is a string that is sometimes a number (thanks woocommerce).
type String string
func (s *String) UnmarshalJSON(bytes []byte) error {
var valStr string
err := json.Unmarshal(bytes, &valStr)
if err == nil {
*s = String(valStr)
return nil
}
var valNum int
err = json.Unmarshal(bytes, &valNum)
if err == nil {
*s = String(strconv.Itoa(valNum))
return nil
} else {
return err
}
}