-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft.go
More file actions
296 lines (245 loc) · 6.07 KB
/
ft.go
File metadata and controls
296 lines (245 loc) · 6.07 KB
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package ft
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"github.com/pkg/errors"
)
// String can be used to decode any JSON value to string
type String struct {
String string
}
func StringFrom(fs string) String {
return String{String: fs}
}
// MarshalJSON method with value receiver for String
// Method must not have a pointer receiver!
// See https://stackoverflow.com/a/21394657/639133
func (fs String) MarshalJSON() ([]byte, error) {
// Control characters like "Start of Text" \u0002 breaks MarshalJSON
// after calling Quote and wrapping the string with RawMessage.
// First call Clean to remove non-graphic characters (except newline)
return json.RawMessage(strconv.Quote(Clean(fs.String))), nil
}
// UnmarshalJSON for String
func (fs *String) UnmarshalJSON(bArr []byte) (err error) {
s, i, f, b :=
"", uint64(0), float64(0), false
// Value is null
if string(bArr) == "null" {
*fs = StringFrom("")
return
}
// Value is a...
// string
if err = json.Unmarshal(bArr, &s); err == nil {
*fs = StringFrom(s)
return
}
// int
if err = json.Unmarshal(bArr, &i); err == nil {
*fs = StringFrom(string(bArr[:]))
return
}
// float
if err = json.Unmarshal(bArr, &f); err == nil {
*fs = StringFrom(string(bArr[:]))
return
}
// bool
if err = json.Unmarshal(bArr, &b); err == nil {
*fs = StringFrom(string(bArr[:]))
return
}
return
}
// TextMarshaler and TextUnmarshaler interfaces
// must be implemented for custom types to be used as JSON map keys
// https://pkg.go.dev/encoding#TextMarshaler
func (fs String) MarshalText() (text []byte, err error) {
return []byte(fs.String), nil
}
func (fs *String) UnmarshalText(text []byte) error {
return fs.UnmarshalJSON(text)
}
// Int can be used to decode any JSON value to int64.
// Strings that are not valid representation of a number will error.
// Boolean values will error
type Int struct {
Int64 int64
}
func IntFrom(fi int64) Int {
return Int{Int64: fi}
}
// MarshalJSON method for Int
func (fi Int) MarshalJSON() ([]byte, error) {
return []byte(strconv.FormatInt(fi.Int64, 10)), nil
}
// UnmarshalJSON method for Int
func (fi *Int) UnmarshalJSON(bArr []byte) (err error) {
s, i, f, b :=
"", int64(0), float64(0), false
// Value is null
if string(bArr) == "null" {
*fi = IntFrom(0)
return
}
// Value is a...
// string
if err = json.Unmarshal(bArr, &s); err == nil {
i, err2 := strconv.ParseInt(s, 10, 64)
if err2 != nil {
// Value is null if int could not be parsed from the string
//*fi = Int(0) // This is not a good idea...
return err2
}
*fi = IntFrom(i)
return
}
// int
if err = json.Unmarshal(bArr, &i); err == nil {
*fi = IntFrom(i)
return
}
// float
if err = json.Unmarshal(bArr, &f); err == nil {
*fi = IntFrom(int64(f))
return
}
// bool
if err = json.Unmarshal(bArr, &b); err == nil {
//*fi = Int(0) // This is not a good idea...
return errors.WithStack(fmt.Errorf("value is a bool"))
}
return
}
func (fi Int) MarshalText() (text []byte, err error) {
return []byte(strconv.FormatInt(fi.Int64, 10)), nil
}
func (fi *Int) UnmarshalText(text []byte) error {
return fi.UnmarshalJSON(text)
}
// Float can be used to decode any JSON value to int64.
// Strings that are not valid representation of a number will error.
// Boolean values will error
type Float struct {
Float64 float64
}
func FloatFrom(ff float64) Float {
return Float{Float64: ff}
}
// MarshalJSON method for Float
func (ff Float) MarshalJSON() ([]byte, error) {
return []byte(strconv.FormatFloat(ff.Float64, 'f', -1, 64)), nil
}
// UnmarshalJSON method for Float
func (ff *Float) UnmarshalJSON(bArr []byte) (err error) {
s, i, f, b :=
"", int64(0), float64(0), false
// Value is null
if string(bArr) == "null" {
*ff = FloatFrom(0)
return
}
// Value is a...
// string
if err = json.Unmarshal(bArr, &s); err == nil {
i, err2 := strconv.ParseFloat(s, 64)
if err2 != nil {
// Value is null if int could not be parsed from the string
//*fi = Float(0) // This is not a good idea...
return err2
}
*ff = FloatFrom(i)
return
}
// int
if err = json.Unmarshal(bArr, &i); err == nil {
*ff = FloatFrom(float64(i))
return
}
// float
if err = json.Unmarshal(bArr, &f); err == nil {
*ff = FloatFrom(f)
return
}
// bool
if err = json.Unmarshal(bArr, &b); err == nil {
//*ff = Float(0) // This is not a good idea...
return errors.WithStack(fmt.Errorf("value is a bool"))
}
return
}
func (ff Float) MarshalText() (text []byte, err error) {
return []byte(strconv.FormatFloat(ff.Float64, 'f', -1, 64)), nil
}
func (ff *Float) UnmarshalText(text []byte) error {
return ff.UnmarshalJSON(text)
}
// Bool can be used to decode any JSON value to bool.
// Empty strings as well as "false" and "0" evaluate to false,
// all other strings are true.
// Numbers equal to 0 will evaluate to false,
// all other numbers are true.
type Bool struct {
Bool bool
}
func BoolFrom(fb bool) Bool {
return Bool{Bool: fb}
}
// MarshalJSON method for Bool
func (fb Bool) MarshalJSON() ([]byte, error) {
return []byte(strconv.FormatBool(fb.Bool)), nil
}
// UnmarshalJSON method for Bool
func (fb *Bool) UnmarshalJSON(bArr []byte) (err error) {
s, i, f, b :=
"", int64(0), float64(0), false
// Value is null
if string(bArr) == "null" {
*fb = BoolFrom(false)
return
}
// Value is a...
// string
if err = json.Unmarshal(bArr, &s); err == nil {
compare := strings.ToLower(strings.TrimSpace(s))
if compare == "false" || compare == "0" || compare == "" {
*fb = BoolFrom(false)
return
}
*fb = BoolFrom(true)
return
}
// int
if err = json.Unmarshal(bArr, &i); err == nil {
if i == 0 {
*fb = BoolFrom(false)
return
}
*fb = BoolFrom(true)
return
}
// float
if err = json.Unmarshal(bArr, &f); err == nil {
if f == 0 {
*fb = BoolFrom(false)
return
}
*fb = BoolFrom(true)
return
}
// bool
if err = json.Unmarshal(bArr, &b); err == nil {
*fb = BoolFrom(b)
return
}
return
}
func (fb Bool) MarshalText() (text []byte, err error) {
return []byte(strconv.FormatBool(fb.Bool)), nil
}
func (fb *Bool) UnmarshalText(text []byte) error {
return fb.UnmarshalJSON(text)
}