writeSameType efficiently serializes a collection where all elements share the same type
(ctx *WriteContext, buf *ByteBuffer, keys []reflect.Value, typeInfo *TypeInfo, flag byte)
| 218 | |
| 219 | // writeSameType efficiently serializes a collection where all elements share the same type |
| 220 | func (s setSerializer) writeSameType(ctx *WriteContext, buf *ByteBuffer, keys []reflect.Value, typeInfo *TypeInfo, flag byte) { |
| 221 | if typeInfo == nil && s.elemSerializer == nil { |
| 222 | return |
| 223 | } |
| 224 | serializer := s.elemSerializer |
| 225 | if serializer == nil { |
| 226 | serializer = typeInfo.Serializer |
| 227 | } |
| 228 | trackRefs := (flag & CollectionTrackingRef) != 0 // Check if reference tracking is enabled |
| 229 | declaredGenerics := (flag & CollectionIsDeclElementType) != 0 |
| 230 | |
| 231 | for _, key := range keys { |
| 232 | key = UnwrapReflectValue(key) |
| 233 | if isNull(key) { |
| 234 | buf.WriteInt8(NullFlag) // WriteData null marker |
| 235 | continue |
| 236 | } |
| 237 | |
| 238 | if trackRefs { |
| 239 | // Handle reference tracking if enabled |
| 240 | refWritten, err := ctx.RefResolver().WriteRefOrNull(buf, key) |
| 241 | if err != nil { |
| 242 | ctx.SetError(FromError(err)) |
| 243 | return |
| 244 | } |
| 245 | if !refWritten { |
| 246 | // WriteData actual value if not a reference |
| 247 | writeSerializerData(ctx, serializer, declaredGenerics, key) |
| 248 | if ctx.HasError() { |
| 249 | return |
| 250 | } |
| 251 | } |
| 252 | } else { |
| 253 | // Directly write value without reference tracking |
| 254 | writeSerializerData(ctx, serializer, declaredGenerics, key) |
| 255 | if ctx.HasError() { |
| 256 | return |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | // writeDifferentTypes handles serialization of collections with mixed element types |
| 263 | func (s setSerializer) writeDifferentTypes(ctx *WriteContext, buf *ByteBuffer, keys []reflect.Value, flag byte) { |
no test coverage detected