From 75d41585d051d71743960e0350d00943a5fbdc1e Mon Sep 17 00:00:00 2001 From: coffeepots Date: Thu, 3 May 2018 20:20:10 +0100 Subject: [PATCH] Add conversion for json -> byte --- eth-rpc/server/servertypes.nim | 75 ++++++++++++++++++++++++++-------- 1 file changed, 58 insertions(+), 17 deletions(-) diff --git a/eth-rpc/server/servertypes.nim b/eth-rpc/server/servertypes.nim index 93d44e9..109059d 100644 --- a/eth-rpc/server/servertypes.nim +++ b/eth-rpc/server/servertypes.nim @@ -81,7 +81,16 @@ macro multiRemove(s: string, values: varargs[string]): untyped = body.add multiReplaceCall result = newBlockStmt(body) -proc jsonGetFunc(paramType: string): NimNode = +proc jsonGetFunc(paramType: string): (NimNode, JsonNodeKind) = + case paramType + of "string": result = (ident"getStr", JString) + of "int": result = (ident"getInt", JInt) + of "float": result = (ident"getFloat", JFloat) + of "bool": result = (ident"getBool", JBool) + of "uint8", "byte": result = (ident"getInt", JInt) + else: result = (nil, JInt) + +#[proc jsonGetFunc(paramType: string): NimNode = case paramType of "string": result = ident"getStr" of "int": result = ident"getInt" @@ -89,7 +98,7 @@ proc jsonGetFunc(paramType: string): NimNode = of "bool": result = ident"getBool" of "uint8": result = ident"getInt" else: result = nil - +]# proc jsonTranslate(translation: var NimNode, paramType: string): NimNode = case paramType of "uint8": @@ -104,14 +113,15 @@ proc jsonTranslate(translation: var NimNode, paramType: string): NimNode = translation = quote do: template `result`(value: untyped): untyped = value - +#[ proc jsonCheckType(paramType: string): JsonNodeKind = case paramType of "string": result = JString of "int": result = JInt of "float": result = JFloat of "bool": result = JBool - of "uint8": result = JInt + of "byte": result = JInt +]# proc preParseTypes(typeNode: var NimNode, typeName: NimNode, errorCheck: var NimNode): bool {.compileTime.} = # handle byte @@ -125,7 +135,7 @@ proc preParseTypes(typeNode: var NimNode, typeName: NimNode, errorCheck: var Nim if preParseTypes(t, typeName, errorCheck): typeNode[i] = t -proc expect(node, jsonIdent, fieldName: NimNode, tn: JsonNodeKind) = +proc expectKind(node, jsonIdent, fieldName: NimNode, tn: JsonNodeKind) = let expectedStr = "Expected parameter `" & fieldName.repr & "` to be " & $tn & " but got " tnIdent = ident($tn) @@ -134,19 +144,38 @@ proc expect(node, jsonIdent, fieldName: NimNode, tn: JsonNodeKind) = raise newException(ValueError, `expectedStr` & $`jsonIdent`.kind) ) +proc translate(paramType: string, getField: NimNode): NimNode = + # Note that specific types add extra run time bounds checking code + case paramType + of "byte": + result = quote do: + let x = `getField` + if x > 255 or x < 0: + raise newException(ValueError, "Value out of range of byte, expected 0-255, got " & $x) + uint8(x and 0xff) + else: + result = quote do: `getField` + macro processFields(jsonIdent, fieldName, fieldType: typed): untyped = result = newStmtList() let fieldTypeStr = fieldType.repr - jFetch = jsonGetFunc(fieldTypeStr) + (jFetch, jKind) = jsonGetFunc(fieldTypeStr) var translation: NimNode if not jFetch.isNil: - result.expect(jsonIdent, fieldName, jsonCheckType(fieldTypeStr)) - let transIdent = translation.jsonTranslate(fieldTypeStr) + result.expectKind(jsonIdent, fieldName, jKind) + #let transIdent = translation.jsonTranslate(fieldTypeStr) + let + getField = quote do: `jsonIdent`.`jFetch` + res = translate(`fieldTypeStr`, `getField`) result.add(quote do: - `translation` - `fieldName` = `transIdent`(`jsonIdent`.`jFetch`) + `fieldName` = `res` ) + echo ">>", result.repr, "<<" + #result.add(quote do: + # `translation` + # `fieldName` = `transIdent`(`jsonIdent`.`jFetch`) + #) else: var fetchedType = getType(fieldType) var derivedType: NimNode @@ -155,7 +184,7 @@ macro processFields(jsonIdent, fieldName, fieldType: typed): untyped = else: derivedType = fetchedType if derivedType.kind == nnkObjectTy: - result.expect(jsonIdent, fieldName, JObject) + result.expectKind(jsonIdent, fieldName, JObject) let recs = derivedType.findChild it.kind == nnkRecList for i in 0..