Skip to content

Commit

Permalink
first pass at issue #7430: to
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheecour committed Apr 3, 2018
1 parent 17d0e29 commit 2fb7c16
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/pure/json.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1339,6 +1339,8 @@ else:
proc parseJson*(buffer: string): JsonNode =
return parseNativeJson(buffer).convertObject()

proc to*(s: string; dest: typedesc[JsonNode]): auto = parseJson(s)

# -- Json deserialiser macro. --

proc createJsonIndexer(jsonNode: NimNode,
Expand Down Expand Up @@ -1979,6 +1981,7 @@ when isMainModule:
let stringified = $testJson
let parsedAgain = parseJson(stringified)
doAssert(parsedAgain["b"].str == "asd")
doAssert parsedAgain == stringified.to(JsonNode)

parsedAgain["abc"] = %5
doAssert parsedAgain["abc"].num == 5
Expand Down
8 changes: 7 additions & 1 deletion lib/pure/strutils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1078,7 +1078,7 @@ proc parseHexStr*(s: string): string {.noSideEffect, procvar,
else:
result[pos div 2] = chr(val + buf shl 4)

proc parseBool*(s: string): bool =
proc to*(s: string; dest: typedesc[bool]): bool =
## Parses a value into a `bool`.
##
## If ``s`` is one of the following values: ``y, yes, true, 1, on``, then
Expand All @@ -1090,6 +1090,9 @@ proc parseBool*(s: string): bool =
of "n", "no", "false", "0", "off": result = false
else: raise newException(ValueError, "cannot interpret as a bool: " & s)

# TODO: deprecate
proc parseBool*(s: string): bool = s.to(bool)

proc parseEnum*[T: enum](s: string): T =
## Parses an enum ``T``.
##
Expand Down Expand Up @@ -2498,6 +2501,8 @@ proc removePrefix*(s: var string, prefix: string) {.
s.delete(0, prefix.len - 1)

when isMainModule:
doAssert "yes".to(bool) == true

doAssert align("abc", 4) == " abc"
doAssert align("a", 0) == "a"
doAssert align("1232", 6) == " 1232"
Expand Down Expand Up @@ -2734,3 +2739,4 @@ bar
doAssert s.endsWith('\0') == false

#echo("strutils tests passed")

0 comments on commit 2fb7c16

Please sign in to comment.