From 44b39c7f5cdd93065d653a1428f9bd3c6f56618b Mon Sep 17 00:00:00 2001 From: Oskari Timperi Date: Fri, 27 Apr 2018 19:46:51 +0300 Subject: Fix Duration JSON parsing I use `include duration_pb` in the `duration.nim` file. This has the effect of not using the property setter procs when doing assignments! Instead, we directly use the member variable bypassing the setter. This means that `hasField` is not updated. Which means that when serializing a message that was parsed from JSON, we don't serialize the `Duration.seconds` and `Duration.nanos` fields because the library thinks they are not set. The fix for now is to use the dedicated `setFoo` procs for setting fields in these WKT extra functionality files which include the corresponding `*_pb.nim` file. This is not a problem when using `import`, as the fields are private to the `*_pb.nim` module, so outside code uses the property setters when doing assignments. --- nimpb/wkt/duration.nim | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/nimpb/wkt/duration.nim b/nimpb/wkt/duration.nim index ecc7e6b..46e03d4 100644 --- a/nimpb/wkt/duration.nim +++ b/nimpb/wkt/duration.nim @@ -40,13 +40,13 @@ proc parsegoogle_protobuf_Duration*(node: JsonNode): google_protobuf_Duration = let dotIndex = find(node.str, '.') if dotIndex == -1: - result.seconds = parseBiggestInt(node.str[0..^2]) + setSeconds(result, parseBiggestInt(node.str[0..^2])) if result.seconds < -315_576_000_000'i64 or result.seconds > 315_576_000_000'i64: raise newException(nimpb_json.ParseError, "duration seconds out of bounds") return - result.seconds = parseBiggestInt(node.str[0..dotIndex-1]) + setSeconds(result, parseBiggestInt(node.str[0..dotIndex-1])) if result.seconds < -315_576_000_000'i64 or result.seconds > 315_576_000_000'i64: @@ -55,14 +55,14 @@ proc parsegoogle_protobuf_Duration*(node: JsonNode): google_protobuf_Duration = let nanoStr = node.str[dotIndex+1..^2] var factor = 100_000_000 - result.nanos = 0 + setNanos(result, 0) for ch in nanoStr: - result.nanos = result.nanos + int32(factor * (ord(ch) - ord('0'))) + setNanos(result, result.nanos + int32(factor * (ord(ch) - ord('0')))) factor = factor div 10 if result.nanos > 999_999_999: raise newException(ValueError, "duration nanos out of bounds") if result.seconds < 0: - result.nanos = -result.nanos + setNanos(result, -result.nanos) -- cgit v1.2.3