replace nil checks with assert

This commit is contained in:
flaviut 2014-05-31 14:13:29 -04:00
commit 08a76f0466

View file

@ -628,29 +628,21 @@ 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): PJsonNode =
{.raises: [EInvalidValue].} = ## Gets a field from a `JObject`, which must not be nil.
## Gets a field from a `JObject`. If `node` is nil, raises an ## If the value at `name` does not exist, returns nil
## `EInvalidValue` exception. If the value at `name` does not assert(node != nil)
## exist, returns nil
if isNil(node):
raise newException(EInvalidValue, "node may not be nil")
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 nil
proc `[]`*(node: PJsonNode, index: int): PJsonNode proc `[]`*(node: PJsonNode, index: int): PJsonNode =
{.raises: [EInvalidIndex, EInvalidValue].} = ## Gets the node at `index` in an Array. Result is undefined if `index`
## Gets the node at `index` in an Array. `EInvalidIndex` is raised if `index`
## is out of bounds ## is out of bounds
assert(node.kind == JArray) assert(node.kind == JArray)
if isNil(node): assert(node != nil)
raise newException(EInvalidValue, "node may not be nil")
if index < 0 or index >= node.elems.len:
raise newException(EInvalidIndex,
"index " & $index & " is out of range [0, " & $node.elems.len & ")")
return node.elems[index] return node.elems[index]
proc hasKey*(node: PJsonNode, key: string): bool = proc hasKey*(node: PJsonNode, key: string): bool =