-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathftnull.go
More file actions
337 lines (284 loc) · 7.32 KB
/
ftnull.go
File metadata and controls
337 lines (284 loc) · 7.32 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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package ft
import (
"encoding/json"
"strconv"
"strings"
"unicode"
"github.com/guregu/null"
"github.com/pkg/errors"
)
// NString can be used to decode any JSON value to string
type NString null.String
func NStringFrom(fs string) NString {
return NString(null.StringFrom(fs))
}
// Clean removes non-graphic characters from the given string, see
// https://github.com/icza/gox/blob/master/stringsx/stringsx.go#L9 and
// https://stackoverflow.com/a/58994297/639133 However, above function also
// removes newlines, that is not desired?
func Clean(s string) string {
return strings.Map(func(r rune) rune {
if unicode.IsGraphic(r) ||
// Preserving some non-graphic characters
string(r) == "\n" || string(r) == "\t" {
return r
}
return -1
}, s)
}
// MarshalJSON method with value receiver for String
// Method must not have a pointer receiver!
// See https://stackoverflow.com/a/21394657/639133
func (fs NString) MarshalJSON() ([]byte, error) {
if !fs.Valid {
return []byte(`null`), nil
}
// 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 *NString) UnmarshalJSON(bArr []byte) (err error) {
s, i, f, b :=
string(""), uint64(0), float64(0), false
// Value is null
if string(bArr) == "null" {
*fs = NString(null.String{})
return
}
// Value is a...
// string
if err = json.Unmarshal(bArr, &s); err == nil {
*fs = NString(null.StringFrom(s))
return
}
// int
if err = json.Unmarshal(bArr, &i); err == nil {
*fs = NString(null.StringFrom(string(bArr[:])))
return
}
// float
if err = json.Unmarshal(bArr, &f); err == nil {
*fs = NString(null.StringFrom(string(bArr[:])))
return
}
// bool
if err = json.Unmarshal(bArr, &b); err == nil {
*fs = NString(null.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 NString) MarshalText() (text []byte, err error) {
if !fs.Valid {
// TODO Consider marshalling as empty value instead?
return text, errors.Errorf("invalid ft.NString")
}
return []byte(fs.String), nil
}
func (fs *NString) UnmarshalText(text []byte) error {
return fs.UnmarshalJSON(text)
}
// NInt 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 NInt null.Int
func NIntFrom(fi int64) NInt {
return NInt(null.IntFrom(fi))
}
// NIntFromString returns an Int for the given string
func NIntFromString(s string) (NInt, error) {
i, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return NInt{}, err
}
return NIntFrom(i), nil
}
// MarshalJSON method for Int
func (fi NInt) MarshalJSON() ([]byte, error) {
if !fi.Valid {
return []byte(`null`), nil
}
return []byte(strconv.FormatInt(fi.Int64, 10)), nil
}
// UnmarshalJSON method for Int
func (fi *NInt) UnmarshalJSON(bArr []byte) (err error) {
s, i, f, b :=
string(""), int64(0), float64(0), false
// Value is null
if string(bArr) == "null" {
*fi = NInt(null.Int{})
return
}
// Value is a...
// string
if err = json.Unmarshal(bArr, &s); err == nil {
if strings.TrimSpace(s) == "" {
// Empty string parses as null
*fi = NInt(null.Int{})
return
}
i, err2 := strconv.ParseInt(s, 10, 64)
if err2 != nil {
return err2
}
*fi = NInt(null.IntFrom(i))
return
}
// int
if err = json.Unmarshal(bArr, &i); err == nil {
*fi = NInt(null.IntFrom(i))
return
}
// float
if err = json.Unmarshal(bArr, &f); err == nil {
*fi = NInt(null.IntFrom(int64(f)))
return
}
// bool
if err = json.Unmarshal(bArr, &b); err == nil {
return errors.Errorf("value is a bool")
}
return
}
func (fi NInt) MarshalText() (text []byte, err error) {
if !fi.Valid {
return text, errors.Errorf("invalid ft.NInt")
}
return []byte(strconv.FormatInt(fi.Int64, 10)), nil
}
func (fi *NInt) UnmarshalText(text []byte) error {
return fi.UnmarshalJSON(text)
}
// NFloat 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 NFloat null.Float
func NFloatFrom(ff float64) NFloat {
return NFloat(null.FloatFrom(ff))
}
// MarshalJSON method for Float
func (fi NFloat) MarshalJSON() ([]byte, error) {
if !fi.Valid {
return []byte(`null`), nil
}
return []byte(strconv.FormatFloat(fi.Float64, 'f', -1, 64)), nil
}
// UnmarshalJSON method for Float
func (fi *NFloat) UnmarshalJSON(bArr []byte) (err error) {
s, i, f, b :=
string(""), int64(0), float64(0), false
// Value is null
if string(bArr) == "null" {
*fi = NFloat(null.Float{})
return
}
// Value is a...
// string
if err = json.Unmarshal(bArr, &s); err == nil {
i, err2 := strconv.ParseFloat(s, 64)
if err2 != nil {
return err2
}
*fi = NFloat(null.FloatFrom(i))
return
}
// int
if err = json.Unmarshal(bArr, &i); err == nil {
*fi = NFloat(null.FloatFrom(float64(i)))
return
}
// float
if err = json.Unmarshal(bArr, &f); err == nil {
*fi = NFloat(null.FloatFrom(f))
return
}
// bool
if err = json.Unmarshal(bArr, &b); err == nil {
return errors.Errorf("value is a bool")
}
return
}
func (ff NFloat) MarshalText() (text []byte, err error) {
if !ff.Valid {
return text, errors.Errorf("invalid ft.NFloat")
}
return []byte(strconv.FormatFloat(ff.Float64, 'f', -1, 64)), nil
}
func (ff *NFloat) UnmarshalText(text []byte) error {
return ff.UnmarshalJSON(text)
}
// NBool 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 NBool null.Bool
func NBoolFrom(fb bool) NBool {
return NBool(null.BoolFrom(fb))
}
// MarshalJSON method for Bool
func (fb NBool) MarshalJSON() ([]byte, error) {
if !fb.Valid {
return []byte(`null`), nil
}
return []byte(strconv.FormatBool(fb.Bool)), nil
}
// UnmarshalJSON method for Bool
func (fb *NBool) UnmarshalJSON(bArr []byte) (err error) {
s, i, f, b :=
string(""), int64(0), float64(0), false
// Value is null
if string(bArr) == "null" {
*fb = NBool(null.Bool{})
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 = NBool(null.BoolFrom(false))
return
}
*fb = NBool(null.BoolFrom(true))
return
}
// int
if err = json.Unmarshal(bArr, &i); err == nil {
if i == 0 {
*fb = NBool(null.BoolFrom(false))
return
}
*fb = NBool(null.BoolFrom(true))
return
}
// float
if err = json.Unmarshal(bArr, &f); err == nil {
if f == 0 {
*fb = NBool(null.BoolFrom(false))
return
}
*fb = NBool(null.BoolFrom(true))
return
}
// bool
if err = json.Unmarshal(bArr, &b); err == nil {
*fb = NBool(null.BoolFrom(b))
return
}
return
}
func (fb NBool) MarshalText() (text []byte, err error) {
if !fb.Valid {
return text, errors.Errorf("invalid ft.NBool")
}
return []byte(strconv.FormatBool(fb.Bool)), nil
}
func (fb *NBool) UnmarshalText(text []byte) error {
return fb.UnmarshalJSON(text)
}