Empty objects should not be encoded
This commit is contained in:
parent
e1905e13ce
commit
d81f0f0cbe
2 changed files with 29 additions and 4 deletions
|
|
@ -112,8 +112,6 @@ proc encodeField*(protobuf: var ProtoBuffer, fieldNum: int, value: SomeLengthDel
|
||||||
proc put(stream: OutputStreamVar, value: object) {.inline.}
|
proc put(stream: OutputStreamVar, value: object) {.inline.}
|
||||||
|
|
||||||
proc encodeField(stream: OutputStreamVar, fieldNum: int, value: object) {.inline.} =
|
proc encodeField(stream: OutputStreamVar, fieldNum: int, value: object) {.inline.} =
|
||||||
stream.append protoHeader(fieldNum, LengthDelimited)
|
|
||||||
|
|
||||||
# This is currently needed in order to get the size
|
# This is currently needed in order to get the size
|
||||||
# of the output before adding it to the stream.
|
# of the output before adding it to the stream.
|
||||||
# Maybe there is a better way to do this
|
# Maybe there is a better way to do this
|
||||||
|
|
@ -121,8 +119,10 @@ proc encodeField(stream: OutputStreamVar, fieldNum: int, value: object) {.inline
|
||||||
objStream.put(value)
|
objStream.put(value)
|
||||||
|
|
||||||
let objOutput = objStream.getOutput()
|
let objOutput = objStream.getOutput()
|
||||||
stream.put(len(objOutput).uint)
|
if objOutput.len > 0:
|
||||||
stream.put(objOutput)
|
stream.append protoHeader(fieldNum, LengthDelimited)
|
||||||
|
stream.put(len(objOutput).uint)
|
||||||
|
stream.put(objOutput)
|
||||||
|
|
||||||
proc encodeField*(protobuf: var ProtoBuffer, value: object) {.inline.} =
|
proc encodeField*(protobuf: var ProtoBuffer, value: object) {.inline.} =
|
||||||
protobuf.outstream.encodeField(protobuf.fieldNum, value)
|
protobuf.outstream.encodeField(protobuf.fieldNum, value)
|
||||||
|
|
|
||||||
|
|
@ -116,4 +116,29 @@ suite "Test Varint Encoding":
|
||||||
|
|
||||||
var output = proto.output
|
var output = proto.output
|
||||||
let decoded = output.decode(Test3)
|
let decoded = output.decode(Test3)
|
||||||
|
|
||||||
|
assert decoded == obj
|
||||||
|
|
||||||
|
test "Empty object field does not get encoded":
|
||||||
|
var proto = newProtoBuffer()
|
||||||
|
|
||||||
|
let obj = Test1()
|
||||||
|
proto.encodeField(1, obj)
|
||||||
|
|
||||||
|
var output = proto.output
|
||||||
|
assert output.len == 0
|
||||||
|
|
||||||
|
let decoded = output.decode(Test1)
|
||||||
|
assert decoded == obj
|
||||||
|
|
||||||
|
test "Empty object does not get encoded":
|
||||||
|
var proto = newProtoBuffer()
|
||||||
|
|
||||||
|
let obj = Test1()
|
||||||
|
proto.encode(obj)
|
||||||
|
|
||||||
|
var output = proto.output
|
||||||
|
assert output.len == 0
|
||||||
|
|
||||||
|
let decoded = output.decode(Test1)
|
||||||
assert decoded == obj
|
assert decoded == obj
|
||||||
Loading…
Add table
Add a link
Reference in a new issue