github.com/v2pro/plz@v0.0.0-20221028024117-e5f9aec5b631/msgfmt/jsonfmt/encoder_array.go (about)

     1  package jsonfmt
     2  
     3  import (
     4  	"unsafe"
     5  	"context"
     6  	"github.com/v2pro/plz/reflect2"
     7  )
     8  
     9  type arrayEncoder struct {
    10  	elemEncoder Encoder
    11  	arrayType   *reflect2.UnsafeArrayType
    12  }
    13  
    14  func (encoder *arrayEncoder) Encode(ctx context.Context, space []byte, ptr unsafe.Pointer) []byte {
    15  	space = append(space, '[')
    16  	arrayType := encoder.arrayType
    17  	for i := 0; i < arrayType.Len(); i++ {
    18  		if i != 0 {
    19  			space = append(space, ',')
    20  		}
    21  		elemPtr := arrayType.UnsafeGetIndex(ptr, i)
    22  		space = encoder.elemEncoder.Encode(ctx, space, elemPtr)
    23  	}
    24  	space = append(space, ']')
    25  	return space
    26  }