From d2925ec9864ecfa349ec2bc34706396814df34ce Mon Sep 17 00:00:00 2001 From: Oskari Timperi Date: Fri, 27 Apr 2018 20:01:53 +0300 Subject: Fix JSON parsing of FieldMask The camelcasing proc was not implemented so conformance suite didn't like us. --- nimpb/wkt/field_mask.nim | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/nimpb/wkt/field_mask.nim b/nimpb/wkt/field_mask.nim index 6e5fcba..72f541b 100644 --- a/nimpb/wkt/field_mask.nim +++ b/nimpb/wkt/field_mask.nim @@ -31,8 +31,16 @@ proc snakeCaseToCamelCase(s: string): string = raise newException(ValueError, "trailing underscore") proc camelCaseToSnakeCase(s: string): string = - # TODO - result = s + # Nimification from https://github.com/google/protobuf/blob/master/src/google/protobuf/util/field_mask_util.cc + result = newStringOfCap(len(s) * 2) + for ch in s: + if ch == '_': + raise newException(ValueError, "unexpected underscore") + if isUpperAscii(ch): + add(result, '_') + add(result, toLowerAscii(ch)) + else: + add(result, ch) proc toJson*(message: google_protobuf_FieldMask): JsonNode = var resultString = "" @@ -51,5 +59,8 @@ proc parsegoogle_protobuf_FieldMask*(node: JsonNode): google_protobuf_FieldMask result = newgoogle_protobuf_FieldMask() result.paths = @[] - for path in split(node.str, ","): - addPaths(result, camelCaseToSnakeCase(path)) + try: + for path in split(node.str, ","): + addPaths(result, camelCaseToSnakeCase(path)) + except ValueError as exc: + raise newException(nimpb_json.ParseError, exc.msg) -- cgit v1.2.3