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

     1  package jsonfmt
     2  
     3  import (
     4  	"unsafe"
     5  	"context"
     6  	"github.com/v2pro/plz/reflect2"
     7  )
     8  
     9  type structEncoder struct {
    10  	fields []structEncoderField
    11  }
    12  
    13  type structEncoderField struct {
    14  	structField *reflect2.UnsafeStructField
    15  	prefix  string
    16  	encoder Encoder
    17  }
    18  
    19  func (encoder *structEncoder) Encode(ctx context.Context, space []byte, ptr unsafe.Pointer) []byte {
    20  	space = append(space, '{')
    21  	for _, field := range encoder.fields {
    22  		space = append(space, field.prefix...)
    23  		fieldPtr := field.structField.UnsafeGet(ptr)
    24  		space = field.encoder.Encode(ctx, space, fieldPtr)
    25  	}
    26  	space = append(space, '}')
    27  	return space
    28  }