aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOskari Timperi <oskari.timperi@iki.fi>2018-04-26 18:24:01 +0300
committerOskari Timperi <oskari.timperi@iki.fi>2018-04-26 18:24:01 +0300
commit629d7f0fe2ab9b20f14da11acfb6819176267e44 (patch)
tree4b65cb1a04cfa1c5b4a1861c363e9330672bb37b
parent1190e18cc36e46f34c41c77d7d8b1f4bd6bbbebf (diff)
downloadnimpb-629d7f0fe2ab9b20f14da11acfb6819176267e44.tar.gz
nimpb-629d7f0fe2ab9b20f14da11acfb6819176267e44.zip
Fixes to bytes handling
-rw-r--r--nimpb/nimpb.nim11
-rw-r--r--tests/conformance/conformance_nim.nim4
2 files changed, 10 insertions, 5 deletions
diff --git a/nimpb/nimpb.nim b/nimpb/nimpb.nim
index de648b1..f00bc87 100644
--- a/nimpb/nimpb.nim
+++ b/nimpb/nimpb.nim
@@ -347,9 +347,12 @@ proc protoWriteString*(stream: Stream, s: string, fieldNumber: int) =
writeTag(stream, fieldNumber, WireType.LengthDelimited)
protoWriteString(stream, s)
-proc protoWriteBytes*(stream: Stream, bytes: var seq[byte], fieldNumber: int) =
+proc protoWriteBytes*(stream: Stream, bytes: seq[byte], fieldNumber: int) =
+ var bytes = bytes
writeTag(stream, fieldNumber, WireType.LengthDelimited)
- writeData(stream, addr(bytes[0]), len(bytes))
+ protoWriteUInt64(stream, len(bytes).uint64)
+ if len(bytes) > 0:
+ writeData(stream, addr(bytes[0]), len(bytes))
proc safeReadStr*(stream: Stream, size: int): string =
result = newString(size)
@@ -362,7 +365,9 @@ proc protoReadString*(stream: Stream): string =
proc protoReadBytes*(stream: Stream): seq[byte] =
let size = int(protoReadUInt64(stream))
- setLen(result, size)
+ newSeq(result, size)
+ if size == 0:
+ return
if readData(stream, addr(result[0]), size) != size:
raise newException(IOError, "cannot read from stream")
diff --git a/tests/conformance/conformance_nim.nim b/tests/conformance/conformance_nim.nim
index 461bc43..25150b6 100644
--- a/tests/conformance/conformance_nim.nim
+++ b/tests/conformance/conformance_nim.nim
@@ -47,10 +47,10 @@ while true:
response.skipped = "dont know how to parse json"
else:
try:
- let parsed = newprotobuf_test_messages_proto3_TestAllTypesProto3(string(request.protobufPayload))
+ let parsed = newprotobuf_test_messages_proto3_TestAllTypesProto3(request.protobufPayload)
if request.requestedOutputFormat == conformance_WireFormat.PROTOBUF:
let ser = serialize(parsed)
- response.protobufPayload = bytes(ser)
+ response.protobufPayload = cast[seq[byte]](ser)
elif request.requestedOutputFormat == conformance_WireFormat.JSON:
response.jsonPayload = $toJson(parsed)
except IOError as exc: