diff options
| author | Oskari Timperi <oskari.timperi@iki.fi> | 2018-04-27 19:46:51 +0300 |
|---|---|---|
| committer | Oskari Timperi <oskari.timperi@iki.fi> | 2018-04-27 19:46:51 +0300 |
| commit | 44b39c7f5cdd93065d653a1428f9bd3c6f56618b (patch) | |
| tree | ad3cfcc3e954c9ea534b918ea737695d59257128 | |
| parent | 15d74f0e3a53d04e96b22a83b9a94535cc8ef931 (diff) | |
| download | nimpb-44b39c7f5cdd93065d653a1428f9bd3c6f56618b.tar.gz nimpb-44b39c7f5cdd93065d653a1428f9bd3c6f56618b.zip | |
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.
| -rw-r--r-- | nimpb/wkt/duration.nim | 10 |
1 files 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) |
