-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchain.go
666 lines (567 loc) · 18.5 KB
/
chain.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
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
package logiface
import (
"encoding/base64"
"encoding/json"
"time"
)
const (
_ parentJSONType = iota
parentJSONTypeArray
parentJSONTypeObject
)
type (
// Chain wraps a [Parent] implementation in order to support nested
// data structures.
Chain[E Event, P comparable] refPoolItem
// BuilderArray is an alias for the fluent-style builder provided
// by the [Builder] type (when the [Event] interface is used as the type).
BuilderArray = *ArrayBuilder[Event, *Chain[Event, *Builder[Event]]]
// BuilderObject is an alias for the fluent-style builder provided
// by the [Builder] type (when the [Event] interface is used as the type).
BuilderObject = *ObjectBuilder[Event, *Chain[Event, *Builder[Event]]]
// ContextArray is an alias for the fluent-style builder provided
// by the [Context] type (when the [Event] interface is used as the type).
ContextArray = *ArrayBuilder[Event, *Chain[Event, *Context[Event]]]
// ContextObject is an alias for the fluent-style builder provided
// by the [Context] type (when the [Event] interface is used as the type).
ContextObject = *ObjectBuilder[Event, *Chain[Event, *Context[Event]]]
// Parent models one of the fluent-style builder implementations, including
// [Builder], [Context], [ArrayBuilder], and others.
Parent[E Event] interface {
Enabled() bool
Root() *Logger[E]
jsonSupport() iJSONSupport[E]
// these methods are effectively jsonSupport, but vary depending on
// both the top-level parent, and implementation details such as
// JSONSupport.CanAppendArray
//
// WARNING: The guarded methods must always return the input arr, even
// when false, in order to avoid allocs within the arrayFields methods.
// necessary for the top-level logic when initializing objects or arrays
// (guarded recursively internally, but not possible on the top level)
jsonMustUseDefault() bool
jsonNewObject(key string) any
jsonWriteObject(key string, obj any)
jsonNewArray(key string) any
jsonWriteArray(key string, arr any)
objNewObject(obj any, key string) any
objWriteObject(obj any, key string, val any) (any, bool)
objNewArray(obj any, key string) any
objWriteArray(obj any, key string, val any) (any, bool)
objField(obj any, key string, val any) any
objString(obj any, key string, val string) (any, bool)
objBool(obj any, key string, val bool) (any, bool)
objBase64Bytes(obj any, key string, b []byte, enc *base64.Encoding) (any, bool)
objDuration(obj any, key string, d time.Duration) (any, bool)
objError(obj any, err error) (any, bool)
objInt(obj any, key string, val int) (any, bool)
objFloat32(obj any, key string, val float32) (any, bool)
objTime(obj any, key string, t time.Time) (any, bool)
objFloat64(obj any, key string, val float64) (any, bool)
objInt64(obj any, key string, val int64) (any, bool)
objUint64(obj any, key string, val uint64) (any, bool)
objRawJSON(obj any, key string, val json.RawMessage) (any, bool)
arrNewObject(arr any) any
arrWriteObject(arr, val any) (any, bool)
arrNewArray(arr any) any
arrWriteArray(arr, val any) (any, bool)
arrField(arr any, val any) any
arrString(arr any, val string) (any, bool)
arrBool(arr any, val bool) (any, bool)
arrBase64Bytes(arr any, b []byte, enc *base64.Encoding) (any, bool)
arrDuration(arr any, d time.Duration) (any, bool)
arrError(arr any, err error) (any, bool)
arrInt(arr any, val int) (any, bool)
arrFloat32(arr any, val float32) (any, bool)
arrTime(arr any, t time.Time) (any, bool)
arrFloat64(arr any, val float64) (any, bool)
arrInt64(arr any, val int64) (any, bool)
arrUint64(arr any, val uint64) (any, bool)
arrRawJSON(arr any, val json.RawMessage) (any, bool)
}
chainInterface interface {
isChain() *refPoolItem
}
chainInterfaceFull[E Event] interface {
chainInterface
newChain(current Parent[E]) any
}
parentJSONType int
)
func (x *Context[E]) Array() *ArrayBuilder[E, *Chain[E, *Context[E]]] {
if x.Enabled() {
return Array[E](newChainParent[E](x))
}
return nil
}
func (x *Context[E]) ArrayFunc(key string, fn func(b *ArrayBuilder[E, *Chain[E, *Context[E]]])) *Context[E] {
if x.Enabled() {
b := ArrayWithKey[E](newChainParent[E](x), key)
if fn != nil {
fn(b)
}
b.As(key).End()
}
return x
}
func (x *Context[E]) Object() *ObjectBuilder[E, *Chain[E, *Context[E]]] {
if x.Enabled() {
return Object[E](newChainParent[E](x))
}
return nil
}
func (x *Context[E]) ObjectFunc(key string, fn func(b *ObjectBuilder[E, *Chain[E, *Context[E]]])) *Context[E] {
if x.Enabled() {
b := ObjectWithKey[E](newChainParent[E](x), key)
if fn != nil {
fn(b)
}
b.As(key).End()
}
return x
}
func (x *Builder[E]) Array() *ArrayBuilder[E, *Chain[E, *Builder[E]]] {
if x.Enabled() {
return Array[E](newChainParent[E](x))
}
return nil
}
func (x *Builder[E]) ArrayFunc(key string, fn func(b *ArrayBuilder[E, *Chain[E, *Builder[E]]])) *Builder[E] {
if x.Enabled() {
b := ArrayWithKey[E](newChainParent[E](x), key)
if fn != nil {
fn(b)
}
b.As(key).End()
}
return x
}
func (x *Builder[E]) Object() *ObjectBuilder[E, *Chain[E, *Builder[E]]] {
if x.Enabled() {
return Object[E](newChainParent[E](x))
}
return nil
}
func (x *Builder[E]) ObjectFunc(key string, fn func(b *ObjectBuilder[E, *Chain[E, *Builder[E]]])) *Builder[E] {
if x.Enabled() {
b := ObjectWithKey[E](newChainParent[E](x), key)
if fn != nil {
fn(b)
}
b.As(key).End()
}
return x
}
// Array attempts to initialize a sub-array, which will succeed only if the
// parent is [Chain], otherwise performing [Logger.DPanic] (returning nil
// if in a production configuration).
func (x *ArrayBuilder[E, P]) Array() *ArrayBuilder[E, P] {
if x.Enabled() {
if c, ok := any(x.p()).(chainInterfaceFull[E]); !ok {
x.Root().DPanic().Log(`logiface: cannot chain a sub-array from a non-chain parent`)
} else {
return Array[E](c.newChain(x).(P))
}
}
return nil
}
func (x *ArrayBuilder[E, P]) ArrayFunc(fn func(b *ArrayBuilder[E, P])) *ArrayBuilder[E, P] {
if b := x.Array(); b != nil {
if fn != nil {
fn(b)
}
endChain(b.Add())
}
return x
}
// Object attempts to initialize a sub-object, which will succeed only if the
// receiver is [Chain], otherwise performing [Logger.DPanic] (returning nil
// if in a production configuration).
func (x *ArrayBuilder[E, P]) Object() *ObjectBuilder[E, P] {
if x.Enabled() {
if c, ok := any(x.p()).(chainInterfaceFull[E]); !ok {
x.Root().DPanic().Log(`logiface: cannot chain a sub-object from a non-chain parent`)
} else {
return Object[E](c.newChain(x).(P))
}
}
return nil
}
func (x *ArrayBuilder[E, P]) ObjectFunc(fn func(b *ObjectBuilder[E, P])) *ArrayBuilder[E, P] {
if b := x.Object(); b != nil {
if fn != nil {
fn(b)
}
endChain(b.Add())
}
return x
}
// Array attempts to initialize a sub-array, which will succeed only if the
// parent is [Chain], otherwise performing [Logger.DPanic] (returning nil
// if in a production configuration).
func (x *ObjectBuilder[E, P]) Array() *ArrayBuilder[E, P] {
return x.arrayWithKey(``)
}
func (x *ObjectBuilder[E, P]) ArrayFunc(key string, fn func(b *ArrayBuilder[E, P])) *ObjectBuilder[E, P] {
if b := x.arrayWithKey(key); b != nil {
if fn != nil {
fn(b)
}
endChain(b.As(key))
}
return x
}
func (x *ObjectBuilder[E, P]) arrayWithKey(key string) *ArrayBuilder[E, P] {
if x.Enabled() {
if c, ok := any(x.p()).(chainInterfaceFull[E]); !ok {
x.Root().DPanic().Log(`logiface: cannot chain a sub-array from a non-chain parent`)
} else {
return ArrayWithKey[E](c.newChain(x).(P), key)
}
}
return nil
}
// Object attempts to initialize a sub-object, which will succeed only if the
// parent is [Chain], otherwise performing [Logger.DPanic] (returning nil
// if in a production configuration).
func (x *ObjectBuilder[E, P]) Object() *ObjectBuilder[E, P] {
return x.objectWithKey(``)
}
func (x *ObjectBuilder[E, P]) ObjectFunc(key string, fn func(b *ObjectBuilder[E, P])) *ObjectBuilder[E, P] {
if b := x.objectWithKey(key); b != nil {
if fn != nil {
fn(b)
}
endChain(b.As(key))
}
return x
}
func (x *ObjectBuilder[E, P]) objectWithKey(key string) *ObjectBuilder[E, P] {
if x.Enabled() {
if c, ok := any(x.p()).(chainInterfaceFull[E]); !ok {
x.Root().DPanic().Log(`logiface: cannot chain a sub-object from a non-chain parent`)
} else {
return ObjectWithKey[E](c.newChain(x).(P), key)
}
}
return nil
}
func (x *Chain[E, P]) Array() *ArrayBuilder[E, *Chain[E, P]] {
if x.Enabled() {
return Array[E](x)
}
return nil
}
func (x *Chain[E, P]) ArrayFunc(key string, fn func(b *ArrayBuilder[E, *Chain[E, P]])) *Chain[E, P] {
if x.Enabled() {
b := ArrayWithKey[E](x, key)
if fn != nil {
fn(b)
}
b.As(key)
}
return x
}
func (x *Chain[E, P]) Object() *ObjectBuilder[E, *Chain[E, P]] {
if x.Enabled() {
return Object[E](x)
}
return nil
}
func (x *Chain[E, P]) ObjectFunc(key string, fn func(b *ObjectBuilder[E, *Chain[E, P]])) *Chain[E, P] {
if x.Enabled() {
b := ObjectWithKey[E](x, key)
if fn != nil {
fn(b)
}
b.As(key)
}
return x
}
// CurArray returns the current array, calls [Logger.DPanic] if the current
// value is not an array, and returns nil if in a production configuration.
//
// Allows adding fields on the same level as nested object(s) and/or array(s).
func (x *Chain[E, P]) CurArray() *ArrayBuilder[E, *Chain[E, P]] {
if x.Enabled() {
if current := x.current(); current != nil {
if current, ok := current.(*ArrayBuilder[E, *Chain[E, P]]); ok {
return current
}
x.Root().DPanic().Log(`logiface: cannot access a non-array as an array`)
}
}
return nil
}
// CurObject returns the current object, calls [Logger.DPanic] if the current
// value is not an array, and returns nil if in a production configuration.
//
// Allows adding fields on the same level as nested object(s) and/or array(s).
func (x *Chain[E, P]) CurObject() *ObjectBuilder[E, *Chain[E, P]] {
if x.Enabled() {
if current := x.current(); current != nil {
if current, ok := current.(*ObjectBuilder[E, *Chain[E, P]]); ok {
return current
}
x.Root().DPanic().Log(`logiface: cannot access a non-object as an object`)
}
}
return nil
}
func (x *Chain[E, P]) As(key string) *Chain[E, P] {
if current := x.current(); current != nil {
switch current := current.(type) {
case arrayBuilderInterface:
if current, ok := current.as(key).(*Chain[E, P]); ok && current != nil && current.a == x.a {
x.setCurrent(current.current())
refPoolPut((*refPoolItem)(current))
} else {
x.setCurrent(nil)
}
case objectBuilderInterface:
if current, ok := current.as(key).(*Chain[E, P]); ok && current != nil && current.a == x.a {
x.setCurrent(current.current())
refPoolPut((*refPoolItem)(current))
} else {
x.setCurrent(nil)
}
default:
x.setCurrent(nil)
}
if x.current() == nil {
x.Root().DPanic().Log(`logiface: chain as failed: called on invalid or terminated parent`)
}
}
return x
}
func (x *Chain[E, P]) Add() *Chain[E, P] {
return x.As(``)
}
// End jumps out of chain, returning the parent, and returning the receiver to
// the pool.
func (x *Chain[E, P]) End() (p P) {
if x != nil {
if x.a != nil {
p = x.a.(P)
}
refPoolPut((*refPoolItem)(x))
}
return
}
func endChain(v any) {
refPoolPut(v.(chainInterface).isChain())
}
func (x *Chain[E, P]) Enabled() bool {
if current := x.current(); current != nil {
return current.Enabled()
}
return false
}
// Root returns the root [Logger] for this instance.
func (x *Chain[E, P]) Root() *Logger[E] {
if current := x.current(); current != nil {
return current.Root()
}
return nil
}
//lint:ignore U1000 it is or will be used
func (x *Chain[E, P]) jsonSupport() iJSONSupport[E] {
return x.current().jsonSupport()
}
//lint:ignore U1000 it is or will be used
func (x *Chain[E, P]) jsonMustUseDefault() bool {
return x.current().jsonMustUseDefault()
}
//lint:ignore U1000 it is or will be used
func (x *Chain[E, P]) jsonNewObject(key string) any {
return x.current().jsonNewObject(key)
}
//lint:ignore U1000 it is or will be used
func (x *Chain[E, P]) objNewObject(obj any, key string) any {
return x.current().objNewObject(obj, key)
}
//lint:ignore U1000 it is or will be used
func (x *Chain[E, P]) arrNewObject(arr any) any {
return x.current().arrNewObject(arr)
}
//lint:ignore U1000 it is or will be used
func (x *Chain[E, P]) jsonWriteObject(key string, obj any) {
x.current().jsonWriteObject(key, obj)
}
//lint:ignore U1000 it is or will be used
func (x *Chain[E, P]) objField(obj any, key string, val any) any {
return x.current().objField(obj, key, val)
}
//lint:ignore U1000 it is or will be used
func (x *Chain[E, P]) objWriteObject(obj any, key string, val any) (any, bool) {
return x.current().objWriteObject(obj, key, val)
}
//lint:ignore U1000 it is or will be used
func (x *Chain[E, P]) objWriteArray(obj any, key string, val any) (any, bool) {
return x.current().objWriteArray(obj, key, val)
}
//lint:ignore U1000 it is or will be used
func (x *Chain[E, P]) objString(obj any, key string, val string) (any, bool) {
return x.current().objString(obj, key, val)
}
//lint:ignore U1000 it is or will be used
func (x *Chain[E, P]) objBool(obj any, key string, val bool) (any, bool) {
return x.current().objBool(obj, key, val)
}
//lint:ignore U1000 it is or will be used
func (x *Chain[E, P]) objBase64Bytes(obj any, key string, b []byte, enc *base64.Encoding) (any, bool) {
return x.current().objBase64Bytes(obj, key, b, enc)
}
//lint:ignore U1000 it is or will be used
func (x *Chain[E, P]) objDuration(obj any, key string, d time.Duration) (any, bool) {
return x.current().objDuration(obj, key, d)
}
//lint:ignore U1000 it is or will be used
func (x *Chain[E, P]) objError(obj any, err error) (any, bool) {
return x.current().objError(obj, err)
}
//lint:ignore U1000 it is or will be used
func (x *Chain[E, P]) objInt(obj any, key string, val int) (any, bool) {
return x.current().objInt(obj, key, val)
}
//lint:ignore U1000 it is or will be used
func (x *Chain[E, P]) objFloat32(obj any, key string, val float32) (any, bool) {
return x.current().objFloat32(obj, key, val)
}
//lint:ignore U1000 it is or will be used
func (x *Chain[E, P]) objTime(obj any, key string, t time.Time) (any, bool) {
return x.current().objTime(obj, key, t)
}
//lint:ignore U1000 it is or will be used
func (x *Chain[E, P]) objFloat64(obj any, key string, val float64) (any, bool) {
return x.current().objFloat64(obj, key, val)
}
//lint:ignore U1000 it is or will be used
func (x *Chain[E, P]) objInt64(obj any, key string, val int64) (any, bool) {
return x.current().objInt64(obj, key, val)
}
//lint:ignore U1000 it is or will be used
func (x *Chain[E, P]) objUint64(obj any, key string, val uint64) (any, bool) {
return x.current().objUint64(obj, key, val)
}
//lint:ignore U1000 it is or will be used
func (x *Chain[E, P]) objRawJSON(obj any, key string, val json.RawMessage) (any, bool) {
return x.current().objRawJSON(obj, key, val)
}
//lint:ignore U1000 it is or will be used
func (x *Chain[E, P]) jsonNewArray(key string) any {
return x.current().jsonNewArray(key)
}
//lint:ignore U1000 it is or will be used
func (x *Chain[E, P]) objNewArray(obj any, key string) any {
return x.current().objNewArray(obj, key)
}
//lint:ignore U1000 it is or will be used
func (x *Chain[E, P]) arrNewArray(arr any) any {
return x.current().arrNewArray(arr)
}
//lint:ignore U1000 it is or will be used
func (x *Chain[E, P]) jsonWriteArray(key string, arr any) {
x.current().jsonWriteArray(key, arr)
}
//lint:ignore U1000 it is or will be used
func (x *Chain[E, P]) arrField(arr any, val any) any {
return x.current().arrField(arr, val)
}
//lint:ignore U1000 it is or will be used
func (x *Chain[E, P]) arrWriteArray(arr, val any) (any, bool) {
return x.current().arrWriteArray(arr, val)
}
//lint:ignore U1000 it is or will be used
func (x *Chain[E, P]) arrWriteObject(arr, val any) (any, bool) {
return x.current().arrWriteObject(arr, val)
}
//lint:ignore U1000 it is or will be used
func (x *Chain[E, P]) arrString(arr any, val string) (any, bool) {
return x.current().arrString(arr, val)
}
//lint:ignore U1000 it is or will be used
func (x *Chain[E, P]) arrBool(arr any, val bool) (any, bool) {
return x.current().arrBool(arr, val)
}
//lint:ignore U1000 it is or will be used
func (x *Chain[E, P]) arrBase64Bytes(arr any, b []byte, enc *base64.Encoding) (any, bool) {
return x.current().arrBase64Bytes(arr, b, enc)
}
//lint:ignore U1000 it is or will be used
func (x *Chain[E, P]) arrDuration(arr any, d time.Duration) (any, bool) {
return x.current().arrDuration(arr, d)
}
//lint:ignore U1000 it is or will be used
func (x *Chain[E, P]) arrError(arr any, err error) (any, bool) {
return x.current().arrError(arr, err)
}
//lint:ignore U1000 it is or will be used
func (x *Chain[E, P]) arrInt(arr any, val int) (any, bool) {
return x.current().arrInt(arr, val)
}
//lint:ignore U1000 it is or will be used
func (x *Chain[E, P]) arrFloat32(arr any, val float32) (any, bool) {
return x.current().arrFloat32(arr, val)
}
//lint:ignore U1000 it is or will be used
func (x *Chain[E, P]) arrTime(arr any, t time.Time) (any, bool) {
return x.current().arrTime(arr, t)
}
//lint:ignore U1000 it is or will be used
func (x *Chain[E, P]) arrFloat64(arr any, val float64) (any, bool) {
return x.current().arrFloat64(arr, val)
}
//lint:ignore U1000 it is or will be used
func (x *Chain[E, P]) arrInt64(arr any, val int64) (any, bool) {
return x.current().arrInt64(arr, val)
}
//lint:ignore U1000 it is or will be used
func (x *Chain[E, P]) arrUint64(arr any, val uint64) (any, bool) {
return x.current().arrUint64(arr, val)
}
//lint:ignore U1000 it is or will be used
func (x *Chain[E, P]) arrRawJSON(arr any, val json.RawMessage) (any, bool) {
return x.current().arrRawJSON(arr, val)
}
func (x *Chain[E, P]) current() (p Parent[E]) {
if x != nil {
p, _ = x.b.(Parent[E])
}
return
}
func (x *Chain[E, P]) setCurrent(p Parent[E]) {
x.b = p
}
//lint:ignore U1000 it is or will be used
func (x *Chain[E, P]) isChain() *refPoolItem {
return (*refPoolItem)(x)
}
//lint:ignore U1000 it is or will be used
func (x *Chain[E, P]) newChain(current Parent[E]) any {
return newChain[E](x.a.(P), current)
}
func newChain[E Event, P comparable](parent P, current Parent[E]) (c *Chain[E, P]) {
c = (*Chain[E, P])(refPoolGet())
c.a = parent
c.b = current
return
}
func newChainParent[E Event, P interface {
Parent[E]
comparable
}](parent P) *Chain[E, P] {
return newChain[E, P](parent, parent)
}
func getParentJSONType(p any) parentJSONType {
switch p := p.(type) {
case arrayBuilderInterface:
return parentJSONTypeArray
case objectBuilderInterface:
return parentJSONTypeObject
case chainInterface:
return getParentJSONType(p.isChain().b)
default:
return 0
}
}