|
82 | 82 |
|
83 | 83 | # Overload the dictionary encoder
|
84 | 84 | function PB.encode(e::ProtoEncoder, i::Int, x::Dict{String,HValue})
|
85 |
| - PB.Codecs.encode_tag(e, i, PB.Codecs.LENGTH_DELIMITED) |
86 |
| - PB.Codecs.vbyte_encode(e.io, UInt32(PB.Codecs._encoded_size(x))) |
87 |
| - |
| 85 | + # PB.Codecs.encode_tag(e, i, PB.Codecs.LENGTH_DELIMITED) |
| 86 | + # remaining_size = PB.Codecs._encoded_size(x, i) - 2 # remove two for the field name and length |
| 87 | + # PB.Codecs.vbyte_encode(e.io, UInt32(remaining_size)) |
| 88 | + |
88 | 89 | for (k, v) in x
|
| 90 | + PB.Codecs.encode_tag(e, 1, PB.Codecs.LENGTH_DELIMITED) |
| 91 | + total_size = PB.Codecs._encoded_size(k, 1) + PB.Codecs._encoded_size(v, 2) |
| 92 | + PB.Codecs.vbyte_encode(e.io, UInt32(total_size)) # Add two for the wire type and length |
89 | 93 | PB.Codecs.encode(e, 1, k)
|
90 | 94 | PB.Codecs.encode(e, 2, v)
|
91 | 95 | end
|
92 | 96 | return nothing
|
93 | 97 | end
|
| 98 | +function PB.Codecs._encoded_size(x::Dict{String,HValue}, i::Int) |
| 99 | + # Field number and length is another 2 bytes |
| 100 | + # There are two bytes for each key value pair extra |
| 101 | + return mapreduce((xi) -> 2 + PB.Codecs._encoded_size(xi.first, 1) + PB.Codecs._encoded_size(xi.second, 2), +, x, init=0) |
| 102 | +end |
94 | 103 |
|
95 | 104 |
|
96 | 105 | """
|
|
0 commit comments