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

Method WriteData

go/fory/struct.go:147–415  ·  view source on GitHub ↗
(ctx *WriteContext, value reflect.Value)

Source from the content-addressed store, hash-verified

145}
146
147func (s *structSerializer) WriteData(ctx *WriteContext, value reflect.Value) {
148 // Early error check - skip all intermediate checks for normal path performance
149 if ctx.HasError() {
150 return
151 }
152
153 // Lazy initialization
154 if !s.initialized {
155 if err := s.initialize(ctx.TypeResolver()); err != nil {
156 ctx.SetError(FromError(err))
157 return
158 }
159 }
160
161 buf := ctx.Buffer()
162
163 // Dereference pointer if needed
164 if value.Kind() == reflect.Ptr {
165 if value.IsNil() {
166 ctx.SetError(SerializationError("cannot write nil pointer"))
167 return
168 }
169 value = value.Elem()
170 }
171
172 // In compatible mode with meta share, struct hash is not written
173 if !ctx.Compatible() {
174 buf.WriteInt32(s.structHash)
175 }
176
177 // Ensure value is addressable for unsafe access
178 if !value.CanAddr() {
179 reuseCache := s.tempValue != nil
180 if ctx.RefResolver().refTracking && len(ctx.RefResolver().writtenObjects) > 0 {
181 reuseCache = false
182 }
183 if reuseCache {
184 tempValue := s.tempValue
185 s.tempValue = nil
186 defer func() {
187 tempValue.SetZero()
188 s.tempValue = tempValue
189 }()
190 addrValue := *tempValue
191 addrValue.Set(value)
192 value = addrValue
193 } else {
194 tmp := reflect.New(value.Type()).Elem()
195 tmp.Set(value)
196 value = tmp
197 }
198 }
199 ptr := unsafe.Pointer(value.UnsafeAddr())
200
201 // ==========================================================================
202 // Phase 1: Fixed-size primitives (bool, int8, int16, float32, float64)
203 // - Reserve once, inline unsafe writes with endian handling, update index once
204 // - field.WriteOffset computed at init time

Callers 2

WriteMethod · 0.95
writeWithTypeInfoMethod · 0.95

Calls 15

initializeMethod · 0.95
writeRemainingFieldMethod · 0.95
FromErrorFunction · 0.85
SerializationErrorFunction · 0.85
KindMethod · 0.80
WriterIndexMethod · 0.80
GetDataMethod · 0.80
SetWriterIndexMethod · 0.80
UnsafePutVarInt32Method · 0.80
UnsafePutVarInt64Method · 0.80
UnsafePutVarUint32Method · 0.80
UnsafePutVarUint64Method · 0.80

Tested by

no test coverage detected