Skip to content

Commit 408043c

Browse files
yveshieldbep
authored andcommitted
Add support for json.Number
Closes #115 Closes #61
1 parent b481d74 commit 408043c

File tree

2 files changed

+195
-0
lines changed

2 files changed

+195
-0
lines changed

Diff for: cast_test.go

+89
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package cast
77

88
import (
9+
"encoding/json"
910
"errors"
1011
"fmt"
1112
"html/template"
@@ -18,6 +19,10 @@ import (
1819
)
1920

2021
func TestToUintE(t *testing.T) {
22+
var jn, nj, jne json.Number
23+
_ = json.Unmarshal([]byte("8"), &jn)
24+
_ = json.Unmarshal([]byte("-8"), &nj)
25+
_ = json.Unmarshal([]byte("8.0"), &jne)
2126
tests := []struct {
2227
input interface{}
2328
expect uint
@@ -38,6 +43,7 @@ func TestToUintE(t *testing.T) {
3843
{true, 1, false},
3944
{false, 0, false},
4045
{"8", 8, false},
46+
{jn, 8, false},
4147
{nil, 0, false},
4248
// errors
4349
{int(-8), 0, true},
@@ -48,6 +54,8 @@ func TestToUintE(t *testing.T) {
4854
{float32(-8.31), 0, true},
4955
{float64(-8.31), 0, true},
5056
{"-8", 0, true},
57+
{nj, 0, true},
58+
{jne, 0, true},
5159
{"test", 0, true},
5260
{testing.T{}, 0, true},
5361
}
@@ -71,6 +79,10 @@ func TestToUintE(t *testing.T) {
7179
}
7280

7381
func TestToUint64E(t *testing.T) {
82+
var jn, nj, jne json.Number
83+
_ = json.Unmarshal([]byte("8"), &jn)
84+
_ = json.Unmarshal([]byte("-8"), &nj)
85+
_ = json.Unmarshal([]byte("8.0"), &jne)
7486
tests := []struct {
7587
input interface{}
7688
expect uint64
@@ -91,6 +103,7 @@ func TestToUint64E(t *testing.T) {
91103
{true, 1, false},
92104
{false, 0, false},
93105
{"8", 8, false},
106+
{jn, 8, false},
94107
{nil, 0, false},
95108
// errors
96109
{int(-8), 0, true},
@@ -101,6 +114,8 @@ func TestToUint64E(t *testing.T) {
101114
{float32(-8.31), 0, true},
102115
{float64(-8.31), 0, true},
103116
{"-8", 0, true},
117+
{nj, 0, true},
118+
{jne, 0, true},
104119
{"test", 0, true},
105120
{testing.T{}, 0, true},
106121
}
@@ -124,6 +139,10 @@ func TestToUint64E(t *testing.T) {
124139
}
125140

126141
func TestToUint32E(t *testing.T) {
142+
var jn, nj, jne json.Number
143+
_ = json.Unmarshal([]byte("8"), &jn)
144+
_ = json.Unmarshal([]byte("-8"), &nj)
145+
_ = json.Unmarshal([]byte("8.0"), &jne)
127146
tests := []struct {
128147
input interface{}
129148
expect uint32
@@ -144,6 +163,7 @@ func TestToUint32E(t *testing.T) {
144163
{true, 1, false},
145164
{false, 0, false},
146165
{"8", 8, false},
166+
{jn, 8, false},
147167
{nil, 0, false},
148168
{int(-8), 0, true},
149169
{int8(-8), 0, true},
@@ -153,7 +173,9 @@ func TestToUint32E(t *testing.T) {
153173
{float32(-8.31), 0, true},
154174
{float64(-8.31), 0, true},
155175
{"-8", 0, true},
176+
{nj, 0, true},
156177
// errors
178+
{jne, 0, true},
157179
{"test", 0, true},
158180
{testing.T{}, 0, true},
159181
}
@@ -177,6 +199,10 @@ func TestToUint32E(t *testing.T) {
177199
}
178200

179201
func TestToUint16E(t *testing.T) {
202+
var jn, nj, jne json.Number
203+
_ = json.Unmarshal([]byte("8"), &jn)
204+
_ = json.Unmarshal([]byte("-8"), &nj)
205+
_ = json.Unmarshal([]byte("8.0"), &jne)
180206
tests := []struct {
181207
input interface{}
182208
expect uint16
@@ -197,6 +223,7 @@ func TestToUint16E(t *testing.T) {
197223
{true, 1, false},
198224
{false, 0, false},
199225
{"8", 8, false},
226+
{jn, 8, false},
200227
{nil, 0, false},
201228
// errors
202229
{int(-8), 0, true},
@@ -207,6 +234,8 @@ func TestToUint16E(t *testing.T) {
207234
{float32(-8.31), 0, true},
208235
{float64(-8.31), 0, true},
209236
{"-8", 0, true},
237+
{nj, 0, true},
238+
{jne, 0, true},
210239
{"test", 0, true},
211240
{testing.T{}, 0, true},
212241
}
@@ -230,6 +259,10 @@ func TestToUint16E(t *testing.T) {
230259
}
231260

232261
func TestToUint8E(t *testing.T) {
262+
var jn, nj, jne json.Number
263+
_ = json.Unmarshal([]byte("8"), &jn)
264+
_ = json.Unmarshal([]byte("-8"), &nj)
265+
_ = json.Unmarshal([]byte("8.0"), &jne)
233266
tests := []struct {
234267
input interface{}
235268
expect uint8
@@ -250,6 +283,7 @@ func TestToUint8E(t *testing.T) {
250283
{true, 1, false},
251284
{false, 0, false},
252285
{"8", 8, false},
286+
{jn, 8, false},
253287
{nil, 0, false},
254288
// errors
255289
{int(-8), 0, true},
@@ -260,6 +294,8 @@ func TestToUint8E(t *testing.T) {
260294
{float32(-8.31), 0, true},
261295
{float64(-8.31), 0, true},
262296
{"-8", 0, true},
297+
{nj, 0, true},
298+
{jne, 0, true},
263299
{"test", 0, true},
264300
{testing.T{}, 0, true},
265301
}
@@ -283,6 +319,9 @@ func TestToUint8E(t *testing.T) {
283319
}
284320

285321
func TestToIntE(t *testing.T) {
322+
var jn, nj json.Number
323+
_ = json.Unmarshal([]byte("8"), &jn)
324+
_ = json.Unmarshal([]byte("8.0"), &nj)
286325
tests := []struct {
287326
input interface{}
288327
expect int
@@ -303,9 +342,11 @@ func TestToIntE(t *testing.T) {
303342
{true, 1, false},
304343
{false, 0, false},
305344
{"8", 8, false},
345+
{jn, 8, false},
306346
{nil, 0, false},
307347
// errors
308348
{"test", 0, true},
349+
{nj, 0, true},
309350
{testing.T{}, 0, true},
310351
}
311352

@@ -328,6 +369,9 @@ func TestToIntE(t *testing.T) {
328369
}
329370

330371
func TestToInt64E(t *testing.T) {
372+
var jn, nj json.Number
373+
_ = json.Unmarshal([]byte("8"), &jn)
374+
_ = json.Unmarshal([]byte(".8"), &nj)
331375
tests := []struct {
332376
input interface{}
333377
expect int64
@@ -348,9 +392,11 @@ func TestToInt64E(t *testing.T) {
348392
{true, 1, false},
349393
{false, 0, false},
350394
{"8", 8, false},
395+
{jn, 8, false},
351396
{nil, 0, false},
352397
// errors
353398
{"test", 0, true},
399+
{nj, 0, true},
354400
{testing.T{}, 0, true},
355401
}
356402

@@ -373,6 +419,9 @@ func TestToInt64E(t *testing.T) {
373419
}
374420

375421
func TestToInt32E(t *testing.T) {
422+
var jn, nj json.Number
423+
_ = json.Unmarshal([]byte("8"), &jn)
424+
_ = json.Unmarshal([]byte("8.0"), &nj)
376425
tests := []struct {
377426
input interface{}
378427
expect int32
@@ -393,9 +442,11 @@ func TestToInt32E(t *testing.T) {
393442
{true, 1, false},
394443
{false, 0, false},
395444
{"8", 8, false},
445+
{jn, 8, false},
396446
{nil, 0, false},
397447
// errors
398448
{"test", 0, true},
449+
{nj, 0, true},
399450
{testing.T{}, 0, true},
400451
}
401452

@@ -418,6 +469,9 @@ func TestToInt32E(t *testing.T) {
418469
}
419470

420471
func TestToInt16E(t *testing.T) {
472+
var jn, nj json.Number
473+
_ = json.Unmarshal([]byte("8"), &jn)
474+
_ = json.Unmarshal([]byte("8.0"), &nj)
421475
tests := []struct {
422476
input interface{}
423477
expect int16
@@ -438,9 +492,11 @@ func TestToInt16E(t *testing.T) {
438492
{true, 1, false},
439493
{false, 0, false},
440494
{"8", 8, false},
495+
{jn, 8, false},
441496
{nil, 0, false},
442497
// errors
443498
{"test", 0, true},
499+
{nj, 0, true},
444500
{testing.T{}, 0, true},
445501
}
446502

@@ -463,6 +519,9 @@ func TestToInt16E(t *testing.T) {
463519
}
464520

465521
func TestToInt8E(t *testing.T) {
522+
var jn, nj json.Number
523+
_ = json.Unmarshal([]byte("8"), &jn)
524+
_ = json.Unmarshal([]byte("8.0"), &nj)
466525
tests := []struct {
467526
input interface{}
468527
expect int8
@@ -483,9 +542,11 @@ func TestToInt8E(t *testing.T) {
483542
{true, 1, false},
484543
{false, 0, false},
485544
{"8", 8, false},
545+
{jn, 8, false},
486546
{nil, 0, false},
487547
// errors
488548
{"test", 0, true},
549+
{nj, 0, true},
489550
{testing.T{}, 0, true},
490551
}
491552

@@ -508,6 +569,9 @@ func TestToInt8E(t *testing.T) {
508569
}
509570

510571
func TestToFloat64E(t *testing.T) {
572+
var jn, nj json.Number
573+
_ = json.Unmarshal([]byte("8"), &jn)
574+
_ = json.Unmarshal([]byte(".8"), &nj)
511575
tests := []struct {
512576
input interface{}
513577
expect float64
@@ -526,10 +590,12 @@ func TestToFloat64E(t *testing.T) {
526590
{float32(8), 8, false},
527591
{float64(8.31), 8.31, false},
528592
{"8", 8, false},
593+
{jn, 8, false},
529594
{true, 1, false},
530595
{false, 0, false},
531596
// errors
532597
{"test", 0, true},
598+
{nj, 0, true},
533599
{testing.T{}, 0, true},
534600
}
535601

@@ -552,6 +618,9 @@ func TestToFloat64E(t *testing.T) {
552618
}
553619

554620
func TestToFloat32E(t *testing.T) {
621+
var jn, nj json.Number
622+
_ = json.Unmarshal([]byte("8"), &jn)
623+
_ = json.Unmarshal([]byte(".8"), &nj)
555624
tests := []struct {
556625
input interface{}
557626
expect float32
@@ -570,10 +639,12 @@ func TestToFloat32E(t *testing.T) {
570639
{float32(8.31), 8.31, false},
571640
{float64(8.31), 8.31, false},
572641
{"8", 8, false},
642+
{jn, 8, false},
573643
{true, 1, false},
574644
{false, 0, false},
575645
// errors
576646
{"test", 0, true},
647+
{nj, 0, true},
577648
{testing.T{}, 0, true},
578649
}
579650

@@ -596,6 +667,8 @@ func TestToFloat32E(t *testing.T) {
596667
}
597668

598669
func TestToStringE(t *testing.T) {
670+
var jn json.Number
671+
_ = json.Unmarshal([]byte("8"), &jn)
599672
type Key struct {
600673
k string
601674
}
@@ -618,6 +691,7 @@ func TestToStringE(t *testing.T) {
618691
{uint64(8), "8", false},
619692
{float32(8.31), "8.31", false},
620693
{float64(8.31), "8.31", false},
694+
{jn, "8", false},
621695
{true, "true", false},
622696
{false, "false", false},
623697
{nil, "", false},
@@ -1120,12 +1194,17 @@ func TestToDurationSliceE(t *testing.T) {
11201194
}
11211195

11221196
func TestToBoolE(t *testing.T) {
1197+
var jf, jt, je json.Number
1198+
_ = json.Unmarshal([]byte("0"), &jf)
1199+
_ = json.Unmarshal([]byte("1"), &jt)
1200+
_ = json.Unmarshal([]byte("1.0"), &je)
11231201
tests := []struct {
11241202
input interface{}
11251203
expect bool
11261204
iserr bool
11271205
}{
11281206
{0, false, false},
1207+
{jf, false, false},
11291208
{nil, false, false},
11301209
{"false", false, false},
11311210
{"FALSE", false, false},
@@ -1140,11 +1219,13 @@ func TestToBoolE(t *testing.T) {
11401219
{"t", true, false},
11411220
{"T", true, false},
11421221
{1, true, false},
1222+
{jt, true, false},
11431223
{true, true, false},
11441224
{-1, true, false},
11451225

11461226
// errors
11471227
{"test", false, true},
1228+
{je, false, true},
11481229
{testing.T{}, false, true},
11491230
}
11501231

@@ -1197,6 +1278,9 @@ func TestIndirectPointers(t *testing.T) {
11971278
}
11981279

11991280
func TestToTime(t *testing.T) {
1281+
var jntime, jnetime json.Number
1282+
_ = json.Unmarshal([]byte("1234567890"), &jntime)
1283+
_ = json.Unmarshal([]byte("123.4567890"), &jnetime)
12001284
tests := []struct {
12011285
input interface{}
12021286
expect time.Time
@@ -1235,9 +1319,11 @@ func TestToTime(t *testing.T) {
12351319
{uint(1482597504), time.Date(2016, 12, 24, 16, 38, 24, 0, time.UTC), false},
12361320
{uint64(1234567890), time.Date(2009, 2, 13, 23, 31, 30, 0, time.UTC), false},
12371321
{uint32(1234567890), time.Date(2009, 2, 13, 23, 31, 30, 0, time.UTC), false},
1322+
{jntime, time.Date(2009, 2, 13, 23, 31, 30, 0, time.UTC), false},
12381323
{time.Date(2009, 2, 13, 23, 31, 30, 0, time.UTC), time.Date(2009, 2, 13, 23, 31, 30, 0, time.UTC), false},
12391324
// errors
12401325
{"2006", time.Time{}, true},
1326+
{jnetime, time.Time{}, true},
12411327
{testing.T{}, time.Time{}, true},
12421328
}
12431329

@@ -1261,6 +1347,8 @@ func TestToTime(t *testing.T) {
12611347

12621348
func TestToDurationE(t *testing.T) {
12631349
var td time.Duration = 5
1350+
var jn json.Number
1351+
_ = json.Unmarshal([]byte("5"), &jn)
12641352

12651353
tests := []struct {
12661354
input interface{}
@@ -1280,6 +1368,7 @@ func TestToDurationE(t *testing.T) {
12801368
{uint8(5), td, false},
12811369
{float64(5), td, false},
12821370
{float32(5), td, false},
1371+
{jn, td, false},
12831372
{string("5"), td, false},
12841373
{string("5ns"), td, false},
12851374
{string("5us"), time.Microsecond * td, false},

0 commit comments

Comments
 (0)