-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathencode_json_test.go
More file actions
799 lines (782 loc) · 17.6 KB
/
Copy pathencode_json_test.go
File metadata and controls
799 lines (782 loc) · 17.6 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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
package zlog
import (
"fmt"
"io/fs"
"log/slog"
"math/big"
"net"
"strconv"
"testing"
"time"
"github.com/icefed/zlog/buffer"
)
type fakeJSONMarshaler struct {
json []byte
err error
}
func (f fakeJSONMarshaler) MarshalJSON() ([]byte, error) {
return f.json, f.err
}
func TestJSONEncoder(t *testing.T) {
h := NewJSONHandler(&Config{
HandlerOptions: slog.HandlerOptions{
AddSource: true,
Level: slog.LevelDebug,
},
TimeDurationAsInt: true,
IgnoreEmptyGroup: true,
})
buf := buffer.New()
defer buf.Free()
tests := []struct {
name string
groups []string
key string
value any
want string
replaceAttr func(_ []string, a slog.Attr) slog.Attr
}{
{
name: "time",
key: slog.TimeKey,
value: testTime,
want: `"time":"2023-08-16T01:02:03.666Z"`,
}, {
name: "level with group",
groups: []string{"g"},
key: slog.LevelKey,
value: slog.LevelInfo,
want: `"g":{"level":"INFO"}`,
}, {
name: "msg",
key: slog.MessageKey,
value: "test msg",
want: `"msg":"test msg"`,
}, {
name: "error with group",
groups: []string{"g", "g2"},
key: "error",
value: fs.ErrNotExist,
want: `"g":{"g2":{"error":"file does not exist"}}`,
}, {
name: "source",
key: slog.SourceKey,
value: &slog.Source{
File: "test.go",
Line: 300,
},
want: `"source":"test.go:300"`,
}, {
name: "stacktrace",
key: "stacktrace",
value: &stacktrace{getPC()},
want: `"stacktrace":"` + wantPCFunction + "\\n\\t" + wantPCFile + ":" + strconv.Itoa(wantPCLine) + `"`,
}, {
name: "ip",
key: "ip",
value: net.ParseIP("127.0.0.1"),
want: `"ip":"127.0.0.1"`,
}, {
name: "formatted",
key: "formatted",
value: []byte(`"env":"prod","app":"web"`),
want: `"env":"prod","app":"web"`,
}, {
name: "group",
key: "group",
value: slog.GroupValue(slog.Attr{Key: "env", Value: slog.StringValue("prod")}, slog.Attr{Key: "app", Value: slog.StringValue("web")}),
want: `"group":{"env":"prod","app":"web"}`,
},
{name: "bool", key: "bool", value: true, want: `"bool":true`},
{name: "duration", key: "duration", value: time.Second * 10, want: `"duration":10000000000`},
{name: "float64", key: "float64", value: float64(0.32), want: `"float64":0.32`},
{name: "int64", key: "int64", value: int64(32), want: `"int64":32`},
{name: "uint64", key: "uint64", value: uint64(111), want: `"uint64":111`},
{name: "string", key: "string", value: "stringvalue", want: `"string":"stringvalue"`},
{
name: "escapedstring",
key: "escapedstring",
value: "😀z\nz\rz\tz\"z\\z\u2028z\x00z\xff",
want: `"escapedstring":"😀z\nz\rz\tz\"z\\z\u2028z\u0000z\ufffd"`,
}, {
name: "MarshalJSON error",
key: "marshaljsonerror",
value: &fakeJSONMarshaler{err: fmt.Errorf("MarshalJSON error")},
want: `"marshaljsonerror":"!ERROR:MarshalJSON error"`,
}, {
name: "MarshalJSON get invalid",
key: "marshaljsongetinvalid",
value: &fakeJSONMarshaler{json: []byte(`"invali"d"`)},
want: `"marshaljsongetinvalid":"!ERROR:invalid MarshalJSON output:\"invali\"d\""`,
}, {
name: "MarshalText error",
key: "marshaltexterror",
value: &fakeTextMarshaler{err: fmt.Errorf("MarshalText error")},
want: `"marshaltexterror":"!ERROR:MarshalText error"`,
}, {
name: "json encode error",
key: "jsonencodeerror",
value: make(chan int),
want: `"jsonencodeerror":"!ERROR:json: unsupported type: chan int"`,
}, {
name: "ignore level",
key: slog.LevelKey,
value: slog.LevelDebug,
want: "",
replaceAttr: func(_ []string, a slog.Attr) slog.Attr {
if a.Key == slog.LevelKey && a.Value.Any().(slog.Level) == slog.LevelDebug {
return slog.Attr{}
}
return a
},
}, {
name: "error+1",
key: slog.LevelKey,
value: slog.LevelError,
want: `"level":"ERROR+1"`,
replaceAttr: func(_ []string, a slog.Attr) slog.Attr {
if a.Key == slog.LevelKey && a.Value.Any().(slog.Level) == slog.LevelError {
a.Value = slog.AnyValue(slog.LevelError + 1)
return a
}
return a
},
}, {
name: "replace time",
key: "timestring",
value: "2023-01-01T01:02:03.666Z",
want: `"timestring":"2023-08-16T01:02:03.666666666Z"`,
replaceAttr: func(_ []string, a slog.Attr) slog.Attr {
if a.Key == "timestring" {
a.Value = slog.TimeValue(testTime)
}
return a
},
}, {
name: "replace time to string",
key: "timestring2",
value: testTime,
want: `"timestring2":"2023-08-16T01:02:03.666666666Z"`,
replaceAttr: func(_ []string, a slog.Attr) slog.Attr {
if a.Key == "timestring2" {
a.Value = slog.StringValue("2023-08-16T01:02:03.666666666Z")
}
return a
},
}, {
name: "testtime",
key: "testtime",
value: testTime,
want: `"testtime":"2023-08-16T01:02:03.666Z"`,
}, {
name: "deletetime",
key: "deletetime",
value: testTime,
want: ``,
replaceAttr: func(_ []string, a slog.Attr) slog.Attr {
if a.Key == "deletetime" {
a.Key = ""
}
return a
},
}, {
name: "source",
key: slog.SourceKey,
value: &slog.Source{
File: "test.go",
Line: 300,
},
want: `"source":"test.go:300"`,
}, {
name: "replacepc",
key: "replacepc",
value: getPC(),
want: `"replacepc":"replacedpc"`,
replaceAttr: func(_ []string, a slog.Attr) slog.Attr {
if a.Key == "replacepc" {
a.Value = slog.StringValue("replacedpc")
}
return a
},
}, {
name: "stacktrace",
key: "stacktrace",
value: &stacktrace{getPC()},
want: `"stacktrace":"` + wantPCFunction + "\\n\\t" + wantPCFile + ":" + strconv.Itoa(wantPCLine) + `"`,
}, {
name: "group",
key: "group",
value: slog.GroupValue(slog.Attr{Key: "env", Value: slog.StringValue("prod")}, slog.Attr{Key: "app", Value: slog.StringValue("web")}),
want: `"group":{"env":"prod","app":"web"}`,
}, {
name: "ignore ip",
key: "ipaddr",
value: &net.IPAddr{IP: net.IPv4(127, 0, 0, 1)},
replaceAttr: func(_ []string, a slog.Attr) slog.Attr {
if a.Key == "ipaddr" {
a.Key = ""
}
return a
},
}, {
name: "replace in group",
groups: []string{"s1", "s2"},
key: "string",
value: "stringvalue",
want: `"s1":{"s2":{"string":"replacedstring"}}`,
replaceAttr: func(_ []string, a slog.Attr) slog.Attr {
if a.Key == "string" {
a.Value = slog.StringValue("replacedstring")
}
return a
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if test.replaceAttr != nil {
h = h.WithOptions(WithReplaceAttr(test.replaceAttr))
}
enc := newJSONEncoder(h, buf)
for _, group := range test.groups {
enc.OpenGroup(group)
}
switch v := test.value.(type) {
case time.Time:
enc.AppendTime(test.key, v)
case slog.Level:
enc.AppendLevel(test.key, v)
case string:
enc.AppendMessage(test.key, v)
case uintptr:
enc.AppendSourceFromPC(test.key, v)
case *stacktrace:
enc.AppendStacktrace(test.key, v)
case []byte:
enc.AppendFormatted(v)
default:
enc.AppendAttr(slog.Attr{
Key: test.key,
Value: slog.AnyValue(v),
})
}
enc.CloseGroups()
if string(buf.Bytes()) != test.want {
t.Errorf("got %v, want %v", string(buf.Bytes()), test.want)
}
buf.Reset()
})
}
}
func TestJSONEncoderAddAny(t *testing.T) {
h := NewJSONHandler(&Config{
HandlerOptions: slog.HandlerOptions{
Level: slog.LevelDebug,
},
TimeDurationAsInt: true,
IgnoreEmptyGroup: true,
})
buf := buffer.New()
defer buf.Free()
vTimePtr := &testTime
dur := time.Second * 10
vDuration := &dur
str := "stringvalue"
vStringPtr := &str
b := true
vBoolPtr := &b
i := 32
vIntPtr := &i
i8 := int8(32)
vInt8Ptr := &i8
i16 := int16(32)
vInt16Ptr := &i16
i32 := int32(32)
vInt32Ptr := &i32
i64 := int64(32)
vInt64Ptr := &i64
ui := uint(32)
vUintPtr := &ui
ui8 := uint8(32)
vUint8Ptr := &ui8
ui16 := uint16(32)
vUint16Ptr := &ui16
ui32 := uint32(32)
vUint32Ptr := &ui32
ui64 := uint64(32)
vUint64Ptr := &ui64
f32 := float32(0.32)
vFloat32Ptr := &f32
f64 := float64(0.32)
vFloat64Ptr := &f64
var nilErrArr []error
var nilByteArr []byte
var nilStrArr []string
var nilBoolArr []bool
var nilIntArr []int
var nilInt8Arr []int8
var nilInt16Arr []int16
var nilInt32Arr []int32
var nilInt64Arr []int64
var nilUintArr []uint
var nilUint16Arr []uint16
var nilUint32Arr []uint32
var nilUint64Arr []uint64
var nilFloat32Arr []float32
var nilFloat64Arr []float64
var nilDurationArr []time.Duration
var nilTimeArr []time.Time
tests := []struct {
name string
value any
want string
}{
{
name: "nil source",
value: (*slog.Source)(nil),
want: `null`,
}, {
name: "nil stacktrace",
value: (*stacktrace)(nil),
want: `null`,
}, {
name: "time ptr",
value: vTimePtr,
want: `"2023-08-16T01:02:03.666666666Z"`,
}, {
name: "nil time",
value: (*time.Time)(nil),
want: `null`,
}, {
name: "time duration",
value: vDuration,
want: `10000000000`,
}, {
name: "nil time duration",
value: (*time.Duration)(nil),
want: `null`,
}, {
name: "string ptr",
value: vStringPtr,
want: `"stringvalue"`,
}, {
name: "nil string",
value: (*string)(nil),
want: `null`,
}, {
name: "bool ptr",
value: vBoolPtr,
want: `true`,
}, {
name: "nil bool",
value: (*bool)(nil),
want: `null`,
}, {
name: "int ptr",
value: vIntPtr,
want: `32`,
}, {
name: "nil int",
value: (*int)(nil),
want: `null`,
}, {
name: "int8 ptr",
value: vInt8Ptr,
want: `32`,
}, {
name: "nil int8",
value: (*int8)(nil),
want: `null`,
}, {
name: "int16 ptr",
value: vInt16Ptr,
want: `32`,
}, {
name: "nil int16",
value: (*int16)(nil),
want: `null`,
}, {
name: "int32 ptr",
value: vInt32Ptr,
want: `32`,
}, {
name: "nil int32",
value: (*int32)(nil),
want: `null`,
}, {
name: "int64 ptr",
value: vInt64Ptr,
want: `32`,
}, {
name: "nil int64",
value: (*int64)(nil),
want: `null`,
}, {
name: "uint ptr",
value: vUintPtr,
want: `32`,
}, {
name: "nil uint",
value: (*uint)(nil),
want: `null`,
}, {
name: "uint8 ptr",
value: vUint8Ptr,
want: `32`,
}, {
name: "nil uint8",
value: (*uint8)(nil),
want: `null`,
}, {
name: "uint16 ptr",
value: vUint16Ptr,
want: `32`,
}, {
name: "nil uint16",
value: (*uint16)(nil),
want: `null`,
}, {
name: "uint32 ptr",
value: vUint32Ptr,
want: `32`,
}, {
name: "nil uint32",
value: (*uint32)(nil),
want: `null`,
}, {
name: "uint64 ptr",
value: vUint64Ptr,
want: `32`,
}, {
name: "nil uint64",
value: (*uint64)(nil),
want: `null`,
}, {
name: "float32 ptr",
value: vFloat32Ptr,
want: `0.32`,
}, {
name: "nil float32",
value: (*float32)(nil),
want: `null`,
}, {
name: "float64 ptr",
value: vFloat64Ptr,
want: `0.32`,
}, {
name: "nil float64",
value: (*float64)(nil),
want: `null`,
}, {
name: "error array",
value: []error{fs.ErrInvalid, fs.ErrPermission, nil},
want: `["invalid argument","permission denied",null]`,
}, {
name: "nil error array",
value: nilErrArr,
want: `null`,
}, {
name: "rune array",
value: []byte(`testbytes😀`),
want: `"testbytes😀"`,
}, {
name: "nil byte array",
value: nilByteArr,
want: `""`,
}, {
name: "byte array",
value: []byte{0, 1, 2, 3, '4'},
want: `"AAECAzQ="`,
}, {
name: "string array",
value: []string{"a", "b", "c"},
want: `["a","b","c"]`,
}, {
name: "nil string array",
value: nilStrArr,
want: `null`,
}, {
name: "bool array",
value: []bool{true, false, true},
want: `[true,false,true]`,
}, {
name: "nil bool array",
value: nilBoolArr,
want: `null`,
}, {
name: "int array",
value: []int{1, 2, 3, -1},
want: `[1,2,3,-1]`,
}, {
name: "nil int array",
value: nilIntArr,
want: `null`,
}, {
name: "int8 array",
value: []int8{1, 2, 3},
want: `[1,2,3]`,
}, {
name: "nil int8 array",
value: nilInt8Arr,
want: `null`,
}, {
name: "int16 array",
value: []int16{1, 2, 3},
want: `[1,2,3]`,
}, {
name: "nil int16 array",
value: nilInt16Arr,
want: `null`,
}, {
name: "int32 array",
value: []int32{1, 2, 3},
want: `[1,2,3]`,
}, {
name: "nil int32 array",
value: nilInt32Arr,
want: `null`,
}, {
name: "int64 array",
value: []int64{1, 2, 3},
want: `[1,2,3]`,
}, {
name: "nil int64 array",
value: nilInt64Arr,
want: `null`,
}, {
name: "uint array",
value: []uint{1, 2, 3},
want: `[1,2,3]`,
}, {
name: "nil uint array",
value: nilUintArr,
want: `null`,
}, {
name: "uint8 array",
value: []uint8{1, 2, 3},
want: `"AQID"`,
}, {
name: "nil uint8 array",
value: []uint16{1, 2, 3},
want: `[1,2,3]`,
}, {
name: "nil uint16 array",
value: nilUint16Arr,
want: `null`,
}, {
name: "uint32 array",
value: []uint32{1, 2, 3},
want: `[1,2,3]`,
}, {
name: "nil uint32 array",
value: nilUint32Arr,
want: `null`,
}, {
name: "uint64 array",
value: []uint64{1, 2, 3},
want: `[1,2,3]`,
}, {
name: "nil uint64 array",
value: nilUint64Arr,
want: `null`,
}, {
name: "float32 array",
value: []float32{0.1, 0.2, 0.3},
want: `[0.1,0.2,0.3]`,
}, {
name: "nil float32 array",
value: nilFloat32Arr,
want: `null`,
}, {
name: "float64 array",
value: []float64{0.1, 0.2, 0.3},
want: `[0.1,0.2,0.3]`,
}, {
name: "nil float64 array",
value: nilFloat64Arr,
want: `null`,
}, {
name: "duration array",
value: []time.Duration{time.Second, time.Minute, time.Hour},
want: `[1000000000,60000000000,3600000000000]`,
}, {
name: "nil duration array",
value: nilDurationArr,
want: `null`,
}, {
name: "time array",
value: []time.Time{testTime, testTime.Add(time.Hour), testTime.Add(time.Hour * 2)},
want: `["2023-08-16T01:02:03.666666666Z","2023-08-16T02:02:03.666666666Z","2023-08-16T03:02:03.666666666Z"]`,
}, {
name: "nil time array",
value: nilTimeArr,
want: `null`,
}, {
name: "bigint",
value: big.NewInt(32),
want: `32`,
}, {
name: "nil bigint",
value: (*big.Int)(nil),
want: `null`,
}, {
name: "net.IP",
value: net.ParseIP("192.168.1.1"),
want: `"192.168.1.1"`,
}, {
name: "nil net.IP",
value: (*net.IP)(nil),
want: `null`,
}, {
name: "net.AddrError",
value: &net.AddrError{Err: "invalid argument", Addr: "127.0.0.1"},
want: `"address 127.0.0.1: invalid argument"`,
}, {
name: "nil net.AddrError",
value: (*net.AddrError)(nil),
want: `null`,
}, {
name: "net.NS",
value: &net.NS{Host: "localhost"},
want: `{"Host":"localhost"}`,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
enc := newJSONEncoder(h, buf)
enc.addAny(test.value)
if string(buf.Bytes()) != test.want {
t.Errorf("test %T, got %v, want %v", test.value, string(buf.Bytes()), test.want)
}
buf.Reset()
})
}
}
func TestJSONEncoderTimeDurationAsInt(t *testing.T) {
h := NewJSONHandler(&Config{
HandlerOptions: slog.HandlerOptions{
Level: slog.LevelDebug,
},
IgnoreEmptyGroup: true,
})
buf := buffer.New()
defer buf.Free()
tests := []struct {
name string
key string
value any
want string
}{
{
name: "durseconds",
key: "durseconds",
value: time.Second * 30,
want: `"durseconds":"30s"`,
}, {
name: "durminutes",
key: "durminutes",
value: time.Minute * 30,
want: `"durminutes":"30m0s"`,
}, {
name: "durm_s",
key: "durm_s",
value: time.Minute*30 + time.Second*10,
want: `"durm_s":"30m10s"`,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
enc := newJSONEncoder(h, buf)
enc.AppendAttr(slog.Attr{
Key: test.key,
Value: slog.AnyValue(test.value),
})
if string(buf.Bytes()) != test.want {
t.Errorf("got %v, want %v", string(buf.Bytes()), test.want)
}
buf.Reset()
})
}
}
func TestJSONEncoderSpaceIndent(t *testing.T) {
h := NewJSONHandler(&Config{
HandlerOptions: slog.HandlerOptions{
Level: slog.LevelDebug,
},
IgnoreEmptyGroup: true,
SpaceIndent: true,
})
buf := buffer.New()
defer buf.Free()
tests := []struct {
name string
attrs []any
want string
}{
{
name: "bool",
attrs: []any{
slog.Attr{
Key: "key",
Value: slog.BoolValue(true),
},
},
want: `"key": true`,
}, {
name: "int array",
attrs: []any{
slog.Attr{
Key: "key",
Value: slog.AnyValue([]int{1, 2, 3, 4, 5}),
},
slog.Attr{
Key: "dur",
Value: slog.DurationValue(time.Hour),
},
},
want: `"key": [1, 2, 3, 4, 5], "dur": "1h0m0s"`,
}, {
name: "groups attr",
attrs: []any{
slog.Attr{
Key: "key",
Value: slog.AnyValue("value"),
},
slog.Attr{
Key: "gempty",
Value: slog.GroupValue(),
},
},
want: `"key": "value"`,
}, {
name: "empty groups",
attrs: []any{
"g",
slog.Attr{
Key: "key",
Value: slog.AnyValue("value"),
},
"g2",
},
want: `"g": {"key": "value"}`,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
enc := newJSONEncoder(h, buf)
for _, attr := range tc.attrs {
switch v := attr.(type) {
case string:
enc.OpenGroup(v)
case slog.Attr:
enc.AppendAttr(v)
}
}
enc.CloseGroups()
if string(buf.Bytes()) != tc.want {
t.Errorf("got %v, want %v", string(buf.Bytes()), tc.want)
}
buf.Reset()
})
}
}