WriteData serializes map data using chunk protocol
(ctx *WriteContext, value reflect.Value)
| 61 | |
| 62 | // WriteData serializes map data using chunk protocol |
| 63 | func (s mapSerializer) WriteData(ctx *WriteContext, value reflect.Value) { |
| 64 | buf := ctx.Buffer() |
| 65 | value = unwrapInterface(value) |
| 66 | length := value.Len() |
| 67 | buf.WriteVarUint32(uint32(length)) |
| 68 | if length == 0 { |
| 69 | return |
| 70 | } |
| 71 | |
| 72 | iter := value.MapRange() |
| 73 | if !iter.Next() { |
| 74 | return |
| 75 | } |
| 76 | |
| 77 | typeResolver := ctx.TypeResolver() |
| 78 | trackRef := ctx.TrackRef() |
| 79 | entryKey := unwrapInterface(iter.Key()) |
| 80 | entryVal := unwrapInterface(iter.Value()) |
| 81 | hasNext := true |
| 82 | |
| 83 | for hasNext { |
| 84 | // Phase 1: Handle null entries (single-item chunks) |
| 85 | for { |
| 86 | keyValid := entryKey.IsValid() |
| 87 | valValid := entryVal.IsValid() |
| 88 | |
| 89 | if keyValid && valValid { |
| 90 | break // Proceed to regular chunk |
| 91 | } |
| 92 | |
| 93 | if !keyValid && !valValid { |
| 94 | buf.WriteInt8(KV_NULL) |
| 95 | } else if !valValid { |
| 96 | s.writeNullValueEntry(ctx, entryKey, typeResolver, trackRef) |
| 97 | } else { |
| 98 | s.writeNullKeyEntry(ctx, entryVal, typeResolver, trackRef) |
| 99 | } |
| 100 | |
| 101 | if ctx.HasError() { |
| 102 | return |
| 103 | } |
| 104 | |
| 105 | if iter.Next() { |
| 106 | entryKey = unwrapInterface(iter.Key()) |
| 107 | entryVal = unwrapInterface(iter.Value()) |
| 108 | } else { |
| 109 | return |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | // Phase 2: Write regular chunk with same-type entries |
| 114 | hasNext = s.writeChunk(ctx, iter, &entryKey, &entryVal, typeResolver, trackRef) |
| 115 | if ctx.HasError() { |
| 116 | return |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 |
no test coverage detected