github.com/v2pro/plz@v0.0.0-20221028024117-e5f9aec5b631/msgfmt/jsonfmt/encoder_slice.go (about) 1 package jsonfmt 2 3 import ( 4 "unsafe" 5 "context" 6 "github.com/v2pro/plz/reflect2" 7 ) 8 9 type sliceEncoder struct { 10 elemEncoder Encoder 11 sliceType *reflect2.UnsafeSliceType 12 } 13 14 func (encoder *sliceEncoder) Encode(ctx context.Context, space []byte, ptr unsafe.Pointer) []byte { 15 sliceType := encoder.sliceType 16 space = append(space, '[') 17 length := sliceType.UnsafeLengthOf(ptr) 18 for i := 0; i < length; i++ { 19 if i != 0 { 20 space = append(space, ',') 21 } 22 elemPtr := sliceType.UnsafeGetIndex(ptr, i) 23 space = encoder.elemEncoder.Encode(ctx, space, elemPtr) 24 } 25 space = append(space, ']') 26 return space 27 }