Comments addressed

This commit is contained in:
Yuriy Glukhov 2019-06-17 18:44:17 +03:00
parent f19d66b35c
commit ac2f6b9360
2 changed files with 3 additions and 22 deletions

View File

@ -12,7 +12,6 @@ requires "nim >= 0.17.3",
"chronos", "chronos",
"httputils", "httputils",
"chronicles", "chronicles",
"byteutils",
"news" "news"
proc configForTests() = proc configForTests() =

View File

@ -1,4 +1,4 @@
import macros, json, options, strutils, typetraits, byteutils import macros, json, options, typetraits
proc expect*(actual, expected: JsonNodeKind, argName: string) = proc expect*(actual, expected: JsonNodeKind, argName: string) =
if actual != expected: raise newException(ValueError, "Parameter [" & argName & "] expected " & $expected & " but got " & $actual) if actual != expected: raise newException(ValueError, "Parameter [" & argName & "] expected " & $expected & " but got " & $actual)
@ -36,13 +36,6 @@ proc fromJson(n: JsonNode, argName: string, result: var ref int64)
proc fromJson(n: JsonNode, argName: string, result: var ref int) proc fromJson(n: JsonNode, argName: string, result: var ref int)
proc fromJson[T](n: JsonNode, argName: string, result: var Option[T]) proc fromJson[T](n: JsonNode, argName: string, result: var Option[T])
proc parseStringToInt(s: string): int =
## If s starts with '0x' parse as hexadecimal, otherwise parse as decimal.
if s.len > 2 and s[0] == '0' and s[1] in {'x', 'X'}:
result = parseHexInt(s)
else:
result = parseInt(s)
# This can't be forward declared: https://github.com/nim-lang/Nim/issues/7868 # This can't be forward declared: https://github.com/nim-lang/Nim/issues/7868
proc fromJson[T: enum](n: JsonNode, argName: string, result: var T) = proc fromJson[T: enum](n: JsonNode, argName: string, result: var T) =
n.kind.expect(JInt, argName) n.kind.expect(JInt, argName)
@ -69,9 +62,6 @@ proc fromJson(n: JsonNode, argName: string, result: var bool) =
result = n.getBool() result = n.getBool()
proc fromJson(n: JsonNode, argName: string, result: var int) = proc fromJson(n: JsonNode, argName: string, result: var int) =
if n.kind == JString:
result = n.getStr().parseStringToInt()
else:
n.kind.expect(JInt, argName) n.kind.expect(JInt, argName)
result = n.getInt() result = n.getInt()
@ -120,14 +110,6 @@ proc fromJson[T](n: JsonNode, argName: string, result: var seq[T]) =
fromJson(n[i], argName, result[i]) fromJson(n[i], argName, result[i])
proc fromJson[N, T](n: JsonNode, argName: string, result: var array[N, T]) = proc fromJson[N, T](n: JsonNode, argName: string, result: var array[N, T]) =
when T is byte:
if n.kind == JString:
let s = n.getStr
if s.len >= result.len + 2 and # (2 for 0x prefix)
s[0] == '0' and s[1] in {'x', 'X'}:
hexToByteArray(n.getStr, result)
return
n.kind.expect(JArray, argName) n.kind.expect(JArray, argName)
if n.len > result.len: raise newException(ValueError, "Parameter \"" & argName & "\" item count is too big for array") if n.len > result.len: raise newException(ValueError, "Parameter \"" & argName & "\" item count is too big for array")
for i in 0 ..< n.len: for i in 0 ..< n.len: