MCPcopy Create free account
hub / github.com/apache/fory / Write

Method Write

go/fory/pointer.go:42–95  ·  view source on GitHub ↗
(ctx *WriteContext, refMode RefMode, writeType bool, hasGenerics bool, value reflect.Value)

Source from the content-addressed store, hash-verified

40}
41
42func (s *ptrToValueSerializer) Write(ctx *WriteContext, refMode RefMode, writeType bool, hasGenerics bool, value reflect.Value) {
43 switch refMode {
44 case RefModeTracking:
45 if value.IsNil() {
46 ctx.Buffer().WriteInt8(NullFlag)
47 return
48 }
49 refWritten, err := ctx.RefResolver().WriteRefOrNull(ctx.Buffer(), value)
50 if err != nil {
51 ctx.SetError(FromError(err))
52 return
53 }
54 if refWritten {
55 return
56 }
57 case RefModeNullOnly:
58 if value.IsNil() {
59 ctx.Buffer().WriteInt8(NullFlag)
60 return
61 }
62 ctx.Buffer().WriteInt8(NotNullValueFlag)
63 case RefModeNone:
64 // For RefModeNone with nullable=false, we should NOT write any null flag.
65 // The xlang protocol expects the value data directly without any header.
66 // If the pointer is nil, write a zero/default value for the underlying type.
67 // This can happen in schema evolution when a field is missing from the remote data.
68 if value.IsNil() {
69 // Create a zero value for the underlying type and write it
70 zeroValue := reflect.New(value.Type().Elem()).Elem()
71 if writeType {
72 typeInfo, err := ctx.TypeResolver().getTypeInfo(zeroValue, true)
73 if err != nil {
74 ctx.SetError(FromError(err))
75 return
76 }
77 ctx.TypeResolver().WriteTypeInfo(ctx.Buffer(), typeInfo, ctx.Err())
78 }
79 s.valueSerializer.WriteData(ctx, zeroValue)
80 return
81 }
82 // Do NOT write any flag - just continue to write the value data
83 }
84 if writeType {
85 // Always use TypeResolver to get the correct TypeID from registered TypeInfo
86 // This ensures compatible mode uses NAMED_COMPATIBLE_STRUCT instead of NAMED_STRUCT
87 typeInfo, err := ctx.TypeResolver().getTypeInfo(value.Elem(), true)
88 if err != nil {
89 ctx.SetError(FromError(err))
90 return
91 }
92 ctx.TypeResolver().WriteTypeInfo(ctx.Buffer(), typeInfo, ctx.Err())
93 }
94 s.WriteData(ctx, value)
95}
96
97func (s *ptrToValueSerializer) writeWithTypeInfo(ctx *WriteContext, refMode RefMode, value reflect.Value, typeInfo *TypeInfo) {
98 switch refMode {

Callers 1

writeWithTypeInfoMethod · 0.95

Calls 13

WriteDataMethod · 0.95
FromErrorFunction · 0.85
WriteRefOrNullMethod · 0.80
WriteDataMethod · 0.65
WriteInt8Method · 0.45
BufferMethod · 0.45
RefResolverMethod · 0.45
SetErrorMethod · 0.45
TypeMethod · 0.45
getTypeInfoMethod · 0.45
TypeResolverMethod · 0.45
WriteTypeInfoMethod · 0.45

Tested by

no test coverage detected