@@ -81,7 +81,7 @@ func New(typer TypeCodec, tagNames []string, maxSliceLen uint32) codec.Codec {
81
81
return & genericCodec {
82
82
typer : typer ,
83
83
maxSliceLen : maxSliceLen ,
84
- fielder : NewStructFielder (tagNames , maxSliceLen ),
84
+ fielder : NewStructFielder (tagNames ),
85
85
}
86
86
}
87
87
@@ -208,8 +208,8 @@ func (c *genericCodec) size(
208
208
size int
209
209
constSize = true
210
210
)
211
- for _ , fieldDesc := range serializedFields {
212
- innerSize , innerConstSize , err := c .size (value .Field (fieldDesc . Index ), typeStack )
211
+ for _ , fieldIndex := range serializedFields {
212
+ innerSize , innerConstSize , err := c .size (value .Field (fieldIndex ), typeStack )
213
213
if err != nil {
214
214
return 0 , false , err
215
215
}
@@ -292,7 +292,7 @@ func (c *genericCodec) MarshalInto(value interface{}, p *wrappers.Packer) error
292
292
return errMarshalNil // can't marshal nil
293
293
}
294
294
295
- return c .marshal (reflect .ValueOf (value ), p , c . maxSliceLen , nil /*=typeStack*/ )
295
+ return c .marshal (reflect .ValueOf (value ), p , nil /*=typeStack*/ )
296
296
}
297
297
298
298
// marshal writes the byte representation of [value] to [p]
@@ -301,7 +301,6 @@ func (c *genericCodec) MarshalInto(value interface{}, p *wrappers.Packer) error
301
301
func (c * genericCodec ) marshal (
302
302
value reflect.Value ,
303
303
p * wrappers.Packer ,
304
- maxSliceLen uint32 ,
305
304
typeStack set.Set [reflect.Type ],
306
305
) error {
307
306
switch valueKind := value .Kind (); valueKind {
@@ -340,7 +339,7 @@ func (c *genericCodec) marshal(
340
339
return errMarshalNil
341
340
}
342
341
343
- return c .marshal (value .Elem (), p , c . maxSliceLen , typeStack )
342
+ return c .marshal (value .Elem (), p , typeStack )
344
343
case reflect .Interface :
345
344
if value .IsNil () {
346
345
return errMarshalNil
@@ -355,18 +354,18 @@ func (c *genericCodec) marshal(
355
354
if err := c .typer .PackPrefix (p , underlyingType ); err != nil {
356
355
return err
357
356
}
358
- if err := c .marshal (value .Elem (), p , c . maxSliceLen , typeStack ); err != nil {
357
+ if err := c .marshal (value .Elem (), p , typeStack ); err != nil {
359
358
return err
360
359
}
361
360
typeStack .Remove (underlyingType )
362
361
return p .Err
363
362
case reflect .Slice :
364
363
numElts := value .Len () // # elements in the slice/array. 0 if this slice is nil.
365
- if uint32 (numElts ) > maxSliceLen {
364
+ if uint32 (numElts ) > c . maxSliceLen {
366
365
return fmt .Errorf ("%w; slice length, %d, exceeds maximum length, %d" ,
367
366
codec .ErrMaxSliceLenExceeded ,
368
367
numElts ,
369
- maxSliceLen ,
368
+ c . maxSliceLen ,
370
369
)
371
370
}
372
371
p .PackInt (uint32 (numElts )) // pack # elements
@@ -386,7 +385,7 @@ func (c *genericCodec) marshal(
386
385
return p .Err
387
386
}
388
387
for i := 0 ; i < numElts ; i ++ { // Process each element in the slice
389
- if err := c .marshal (value .Index (i ), p , c . maxSliceLen , typeStack ); err != nil {
388
+ if err := c .marshal (value .Index (i ), p , typeStack ); err != nil {
390
389
return err
391
390
}
392
391
}
@@ -406,7 +405,7 @@ func (c *genericCodec) marshal(
406
405
)
407
406
}
408
407
for i := 0 ; i < numElts ; i ++ { // Process each element in the array
409
- if err := c .marshal (value .Index (i ), p , c . maxSliceLen , typeStack ); err != nil {
408
+ if err := c .marshal (value .Index (i ), p , typeStack ); err != nil {
410
409
return err
411
410
}
412
411
}
@@ -416,20 +415,20 @@ func (c *genericCodec) marshal(
416
415
if err != nil {
417
416
return err
418
417
}
419
- for _ , fieldDesc := range serializedFields { // Go through all fields of this struct that are serialized
420
- if err := c .marshal (value .Field (fieldDesc . Index ), p , fieldDesc . MaxSliceLen , typeStack ); err != nil { // Serialize the field and write to byte array
418
+ for _ , fieldIndex := range serializedFields { // Go through all fields of this struct that are serialized
419
+ if err := c .marshal (value .Field (fieldIndex ), p , typeStack ); err != nil { // Serialize the field and write to byte array
421
420
return err
422
421
}
423
422
}
424
423
return nil
425
424
case reflect .Map :
426
425
keys := value .MapKeys ()
427
426
numElts := len (keys )
428
- if uint32 (numElts ) > maxSliceLen {
427
+ if uint32 (numElts ) > c . maxSliceLen {
429
428
return fmt .Errorf ("%w; map length, %d, exceeds maximum length, %d" ,
430
429
codec .ErrMaxSliceLenExceeded ,
431
430
numElts ,
432
- maxSliceLen ,
431
+ c . maxSliceLen ,
433
432
)
434
433
}
435
434
p .PackInt (uint32 (numElts )) // pack # elements
@@ -448,7 +447,7 @@ func (c *genericCodec) marshal(
448
447
startOffset := p .Offset
449
448
endOffset := p .Offset
450
449
for i , key := range keys {
451
- if err := c .marshal (key , p , c . maxSliceLen , typeStack ); err != nil {
450
+ if err := c .marshal (key , p , typeStack ); err != nil {
452
451
return err
453
452
}
454
453
if p .Err != nil {
@@ -481,7 +480,7 @@ func (c *genericCodec) marshal(
481
480
}
482
481
483
482
// serialize and pack value
484
- if err := c .marshal (value .MapIndex (key .key ), p , c . maxSliceLen , typeStack ); err != nil {
483
+ if err := c .marshal (value .MapIndex (key .key ), p , typeStack ); err != nil {
485
484
return err
486
485
}
487
486
}
@@ -506,7 +505,7 @@ func (c *genericCodec) Unmarshal(bytes []byte, dest interface{}) error {
506
505
if destPtr .Kind () != reflect .Ptr {
507
506
return errNeedPointer
508
507
}
509
- if err := c .unmarshal (& p , destPtr .Elem (), c . maxSliceLen , nil /*=typeStack*/ ); err != nil {
508
+ if err := c .unmarshal (& p , destPtr .Elem (), nil /*=typeStack*/ ); err != nil {
510
509
return err
511
510
}
512
511
if p .Offset != len (bytes ) {
@@ -525,7 +524,6 @@ func (c *genericCodec) Unmarshal(bytes []byte, dest interface{}) error {
525
524
func (c * genericCodec ) unmarshal (
526
525
p * wrappers.Packer ,
527
526
value reflect.Value ,
528
- maxSliceLen uint32 ,
529
527
typeStack set.Set [reflect.Type ],
530
528
) error {
531
529
switch value .Kind () {
@@ -588,11 +586,11 @@ func (c *genericCodec) unmarshal(
588
586
if p .Err != nil {
589
587
return fmt .Errorf ("couldn't unmarshal slice: %w" , p .Err )
590
588
}
591
- if numElts32 > maxSliceLen {
589
+ if numElts32 > c . maxSliceLen {
592
590
return fmt .Errorf ("%w; array length, %d, exceeds maximum length, %d" ,
593
591
codec .ErrMaxSliceLenExceeded ,
594
592
numElts32 ,
595
- maxSliceLen ,
593
+ c . maxSliceLen ,
596
594
)
597
595
}
598
596
if numElts32 > math .MaxInt32 {
@@ -618,7 +616,7 @@ func (c *genericCodec) unmarshal(
618
616
zeroValue := reflect .Zero (innerType )
619
617
for i := 0 ; i < numElts ; i ++ {
620
618
value .Set (reflect .Append (value , zeroValue ))
621
- if err := c .unmarshal (p , value .Index (i ), c . maxSliceLen , typeStack ); err != nil {
619
+ if err := c .unmarshal (p , value .Index (i ), typeStack ); err != nil {
622
620
return err
623
621
}
624
622
}
@@ -636,7 +634,7 @@ func (c *genericCodec) unmarshal(
636
634
return nil
637
635
}
638
636
for i := 0 ; i < numElts ; i ++ {
639
- if err := c .unmarshal (p , value .Index (i ), c . maxSliceLen , typeStack ); err != nil {
637
+ if err := c .unmarshal (p , value .Index (i ), typeStack ); err != nil {
640
638
return err
641
639
}
642
640
}
@@ -659,7 +657,7 @@ func (c *genericCodec) unmarshal(
659
657
typeStack .Add (intfImplementorType )
660
658
661
659
// Unmarshal into the struct
662
- if err := c .unmarshal (p , intfImplementor , c . maxSliceLen , typeStack ); err != nil {
660
+ if err := c .unmarshal (p , intfImplementor , typeStack ); err != nil {
663
661
return err
664
662
}
665
663
@@ -673,8 +671,8 @@ func (c *genericCodec) unmarshal(
673
671
return fmt .Errorf ("couldn't unmarshal struct: %w" , err )
674
672
}
675
673
// Go through the fields and umarshal into them
676
- for _ , fieldDesc := range serializedFieldIndices {
677
- if err := c .unmarshal (p , value .Field (fieldDesc . Index ), fieldDesc . MaxSliceLen , typeStack ); err != nil {
674
+ for _ , fieldIndex := range serializedFieldIndices {
675
+ if err := c .unmarshal (p , value .Field (fieldIndex ) , typeStack ); err != nil {
678
676
return err
679
677
}
680
678
}
@@ -685,7 +683,7 @@ func (c *genericCodec) unmarshal(
685
683
// Create a new pointer to a new value of the underlying type
686
684
v := reflect .New (t )
687
685
// Fill the value
688
- if err := c .unmarshal (p , v .Elem (), c . maxSliceLen , typeStack ); err != nil {
686
+ if err := c .unmarshal (p , v .Elem (), typeStack ); err != nil {
689
687
return err
690
688
}
691
689
// Assign to the top-level struct's member
@@ -720,7 +718,7 @@ func (c *genericCodec) unmarshal(
720
718
721
719
keyStartOffset := p .Offset
722
720
723
- if err := c .unmarshal (p , mapKey , c . maxSliceLen , typeStack ); err != nil {
721
+ if err := c .unmarshal (p , mapKey , typeStack ); err != nil {
724
722
return err
725
723
}
726
724
@@ -738,7 +736,7 @@ func (c *genericCodec) unmarshal(
738
736
739
737
// Get the value
740
738
mapValue := reflect .New (mapValueType ).Elem ()
741
- if err := c .unmarshal (p , mapValue , c . maxSliceLen , typeStack ); err != nil {
739
+ if err := c .unmarshal (p , mapValue , typeStack ); err != nil {
742
740
return err
743
741
}
744
742
0 commit comments