Allow for nil chaining in JSON
This commit is contained in:
parent
8b82004359
commit
6d3fbf973f
1 changed files with 16 additions and 5 deletions
|
|
@ -628,16 +628,27 @@ proc len*(n: PJsonNode): int =
|
||||||
of JObject: result = n.fields.len
|
of JObject: result = n.fields.len
|
||||||
else: discard
|
else: discard
|
||||||
|
|
||||||
proc `[]`*(node: PJsonNode, name: string): PJsonNode =
|
proc `[]`*(node: PJsonNode, name: string, default: PJsonNode = nil): PJsonNode =
|
||||||
## Gets a field from a `JObject`. Returns nil if the key is not found.
|
## Gets a field from a `JObject`. If node is nil, this returns default
|
||||||
|
## This is to allow for chaining such as the following:
|
||||||
|
## .. code-block Nimrod
|
||||||
|
## foo["field1"]["field2"]
|
||||||
|
##
|
||||||
|
## If `field1` is undefined, it will return nil, and the access on `field2` will
|
||||||
|
## pass through. Also returns default if the key is not found.
|
||||||
|
if isNil(node):
|
||||||
|
return default
|
||||||
assert(node.kind == JObject)
|
assert(node.kind == JObject)
|
||||||
for key, item in items(node.fields):
|
for key, item in items(node.fields):
|
||||||
if key == name:
|
if key == name:
|
||||||
return item
|
return item
|
||||||
return nil
|
return default
|
||||||
|
|
||||||
proc `[]`*(node: PJsonNode, index: int): PJsonNode =
|
proc `[]`*(node: PJsonNode, index: int, default: PJsonNode = nil): PJsonNode =
|
||||||
## Gets the node at `index` in an Array.
|
## Gets the node at `index` in an Array.If `node` is nil, this returns nil to
|
||||||
|
## allow for chaining. Results are undefined if the index is out of range.
|
||||||
|
if isNil(node):
|
||||||
|
return default
|
||||||
assert(node.kind == JArray)
|
assert(node.kind == JArray)
|
||||||
return node.elems[index]
|
return node.elems[index]
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue