github.com/v2pro/plz@v0.0.0-20221028024117-e5f9aec5b631/dump/eface.go (about) 1 package dump 2 3 import ( 4 "context" 5 "unsafe" 6 "reflect" 7 "encoding/json" 8 ) 9 10 type eface struct { 11 dataType unsafe.Pointer 12 data unsafe.Pointer 13 } 14 15 var sampleType = reflect.TypeOf("") 16 17 type efaceEncoder struct { 18 } 19 20 func (encoder *efaceEncoder) Encode(ctx context.Context, space []byte, ptr unsafe.Pointer) []byte { 21 space = append(space, `{"type":"`...) 22 eface := (*eface)(ptr) 23 valType := sampleType 24 (*iface)(unsafe.Pointer(&valType)).data = eface.dataType 25 space = append(space, valType.String()...) 26 space = append(space, `","data":{"__ptr__":"`...) 27 ptrStr := ptrToStr(uintptr(eface.data)) 28 space = append(space, ptrStr...) 29 space = append(space, `"}}`...) 30 elemEncoder := dumper.EncoderOf(valType) 31 elem := elemEncoder.Encode(ctx, nil, eface.data) 32 addrMap := ctx.Value(addrMapKey).(map[string]json.RawMessage) 33 addrMap[ptrStr] = json.RawMessage(elem) 34 return space 35 }