From 0a57c608958491d32365e807f943daa3ea3e3b87 Mon Sep 17 00:00:00 2001 From: Oskari Timperi Date: Thu, 26 Apr 2018 18:25:57 +0300 Subject: Fixes to FieldMask serialization Basically just being more strict about the paths than before. --- nimpb/wkt/field_mask.nim | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/nimpb/wkt/field_mask.nim b/nimpb/wkt/field_mask.nim index fd38cfb..fb8d04f 100644 --- a/nimpb/wkt/field_mask.nim +++ b/nimpb/wkt/field_mask.nim @@ -3,5 +3,43 @@ import strutils include field_mask_pb +const + LowerCaseLetters = {'a'..'z'} + +proc snakeCaseToCamelCase(s: string): string = + # Nimification from https://github.com/google/protobuf/blob/master/src/google/protobuf/util/field_mask_util.cc + result = newStringOfCap(len(s)) + + var afterUnderscore = false + + for ch in s: + if isUpperAscii(ch): + raise newException(ValueError, "string not in snake case") + + if afterUnderscore: + if ch in LowerCaseLetters: + add(result, toUpperAscii(ch)) + afterUnderscore = false + else: + raise newException(ValueError, "lower case expected after underscore") + elif ch == '_': + afterUnderscore = true + else: + add(result, ch) + + if afterUnderscore: + raise newException(ValueError, "trailing underscore") + +proc camelCaseToSnakeCase(s: string): string = + # TODO + result = s + proc toJson*(message: google_protobuf_FieldMask): JsonNode = - %join(message.paths, ",") + var resultString = "" + + for index, path in message.paths: + add(resultString, snakeCaseToCamelCase(path)) + if index < len(message.paths)-1: + add(resultString, ",") + + result = %resultString -- cgit v1.2.3