Rewrites requiredField on top of optionalField.
This commit is contained in:
parent
37b8c46b3b
commit
292bb413d2
1 changed files with 12 additions and 8 deletions
|
|
@ -140,21 +140,25 @@ proc readPackageInfo*(path: string): TPackageInfo =
|
||||||
raise newException(EInvalidValue, "Cannot open package info: " & path)
|
raise newException(EInvalidValue, "Cannot open package info: " & path)
|
||||||
validatePackageInfo(result, path)
|
validatePackageInfo(result, path)
|
||||||
|
|
||||||
proc optionalField(obj: PJsonNode, name: string): string =
|
proc optionalField(obj: PJsonNode, name: string, default = ""): string =
|
||||||
|
## Queries ``obj`` for the optional ``name`` string.
|
||||||
|
##
|
||||||
|
## Returns the value of ``name`` if it is a valid string, or aborts execution
|
||||||
|
## if the field exists but is not of string type. If ``name`` is not present,
|
||||||
|
## returns ``default``.
|
||||||
if existsKey(obj, name):
|
if existsKey(obj, name):
|
||||||
if obj[name].kind == JString:
|
if obj[name].kind == JString:
|
||||||
return obj[name].str
|
return obj[name].str
|
||||||
else:
|
else:
|
||||||
quit("Corrupted packages.json file. " & name & " field is of unexpected type.")
|
quit("Corrupted packages.json file. " & name & " field is of unexpected type.")
|
||||||
else: return ""
|
else: return default
|
||||||
|
|
||||||
proc requiredField(obj: PJsonNode, name: string): string =
|
proc requiredField(obj: PJsonNode, name: string): string =
|
||||||
if existsKey(obj, name):
|
## Queries ``obj`` for the required ``name`` string.
|
||||||
if obj[name].kind == JString:
|
##
|
||||||
return obj[name].str
|
## Aborts execution if the field does not exist or is of invalid json type.
|
||||||
else:
|
result = optionalField(obj, name, nil)
|
||||||
quit("Corrupted packages.json file. " & name & " field is of unexpected type.")
|
if result == nil:
|
||||||
else:
|
|
||||||
quit("Package in packages.json file does not contain a " & name & " field.")
|
quit("Package in packages.json file does not contain a " & name & " field.")
|
||||||
|
|
||||||
proc getPackage*(pkg: string, packagesPath: string, resPkg: var TPackage): bool =
|
proc getPackage*(pkg: string, packagesPath: string, resPkg: var TPackage): bool =
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue