mirror of https://github.com/status-im/NimYAML.git
fixes #101, minor improvements
* improved error reporting on wrong tags * removed a debug `echo` call * fixed problems with sparse objects and Option (#101)
This commit is contained in:
parent
624cbe59b3
commit
854d33378e
|
@ -264,6 +264,19 @@ suite "Serialization":
|
||||||
let input = [none(int32), some(42'i32), none(int32)]
|
let input = [none(int32), some(42'i32), none(int32)]
|
||||||
let output = blockOnlyDumper().dump(input)
|
let output = blockOnlyDumper().dump(input)
|
||||||
assertStringEqual "- !!null ~\n- 42\n- !!null ~\n", output
|
assertStringEqual "- !!null ~\n- 42\n- !!null ~\n", output
|
||||||
|
|
||||||
|
dualTest "Load Option of seq":
|
||||||
|
let input = "- !!null\n- [a, b]"
|
||||||
|
var result: array[0..1, Option[seq[string]]]
|
||||||
|
load(input, result)
|
||||||
|
assert not result[0].isSome
|
||||||
|
assert result[1].get()[0] == "a"
|
||||||
|
assert result[1].get()[1] == "b"
|
||||||
|
|
||||||
|
test "Dump Option of seq":
|
||||||
|
let input = [none(seq[string]), some(@["a", "b"])]
|
||||||
|
let output = Dumper().dump(input)
|
||||||
|
assertStringEqual "- !!null ~\n- [a, b]\n", output
|
||||||
|
|
||||||
dualTest "Load Table[int, string]":
|
dualTest "Load Table[int, string]":
|
||||||
let input = "23: dreiundzwanzig\n42: zweiundvierzig"
|
let input = "23: dreiundzwanzig\n42: zweiundvierzig"
|
||||||
|
|
|
@ -38,6 +38,7 @@ proc load*[K](input: Stream | string, target: var K)
|
||||||
if e.parent of IOError: raise (ref IOError)(e.parent)
|
if e.parent of IOError: raise (ref IOError)(e.parent)
|
||||||
if e.parent of OSError: raise (ref OSError)(e.parent)
|
if e.parent of OSError: raise (ref OSError)(e.parent)
|
||||||
elif e.parent of YamlParserError: raise (ref YamlParserError)(e.parent)
|
elif e.parent of YamlParserError: raise (ref YamlParserError)(e.parent)
|
||||||
|
elif e.parent of YamlConstructionError: raise (ref YamlConstructionError)(e.parent)
|
||||||
else: internalError("Unexpected exception: " & $e.parent.name)
|
else: internalError("Unexpected exception: " & $e.parent.name)
|
||||||
|
|
||||||
proc loadAs*[K](input: Stream | string): K {.raises:
|
proc loadAs*[K](input: Stream | string): K {.raises:
|
||||||
|
|
|
@ -827,8 +827,8 @@ proc hasSparse(t: typedesc): bool {.compileTime.} =
|
||||||
|
|
||||||
proc getOptionInner(fType: NimNode): NimNode {.compileTime.} =
|
proc getOptionInner(fType: NimNode): NimNode {.compileTime.} =
|
||||||
if fType.kind == nnkBracketExpr and len(fType) == 2 and
|
if fType.kind == nnkBracketExpr and len(fType) == 2 and
|
||||||
fType[1].kind == nnkSym:
|
fType[0].kind == nnkSym:
|
||||||
return newIdentNode($fType[1])
|
return fType[1]
|
||||||
else: return nil
|
else: return nil
|
||||||
|
|
||||||
proc checkMissing(
|
proc checkMissing(
|
||||||
|
@ -850,7 +850,7 @@ proc checkMissing(
|
||||||
when `o`.`field`.hasCustomPragma(defaultVal):
|
when `o`.`field`.hasCustomPragma(defaultVal):
|
||||||
`o`.`field` = `o`.`field`.getCustomPragmaVal(defaultVal)
|
`o`.`field` = `o`.`field`.getCustomPragmaVal(defaultVal)
|
||||||
elif hasSparse(`t`) and `o`.`field` is Option:
|
elif hasSparse(`t`) and `o`.`field` is Option:
|
||||||
`o`.`field` = none(`optionInner`)
|
`o`.`field` = none[`optionInner`]()
|
||||||
else:
|
else:
|
||||||
raise constructionError(`s`, `m`, "While constructing " & `tName` &
|
raise constructionError(`s`, `m`, "While constructing " & `tName` &
|
||||||
": Missing field: " & `fName`)
|
": Missing field: " & `fName`)
|
||||||
|
@ -1447,17 +1447,20 @@ proc constructChild*[T](
|
||||||
of yamlScalar:
|
of yamlScalar:
|
||||||
if item.scalarProperties.tag notin [yTagQuestionMark, yTagExclamationMark,
|
if item.scalarProperties.tag notin [yTagQuestionMark, yTagExclamationMark,
|
||||||
yamlTag(T)]:
|
yamlTag(T)]:
|
||||||
raise ctx.input.constructionError(item.startPos, "Wrong tag for " & typetraits.name(T))
|
raise ctx.input.constructionError(
|
||||||
|
item.startPos, "Wrong tag for " & typetraits.name(T) & ": " & $item.scalarProperties.tag)
|
||||||
elif item.scalarProperties.anchor != yAnchorNone:
|
elif item.scalarProperties.anchor != yAnchorNone:
|
||||||
raise ctx.input.constructionError(item.startPos, "Anchor on non-ref type")
|
raise ctx.input.constructionError(item.startPos, "Anchor on non-ref type")
|
||||||
of yamlStartMap:
|
of yamlStartMap:
|
||||||
if item.mapProperties.tag notin [yTagQuestionMark, yamlTag(T)]:
|
if item.mapProperties.tag notin [yTagQuestionMark, yamlTag(T)]:
|
||||||
raise ctx.input.constructionError(item.startPos, "Wrong tag for " & typetraits.name(T))
|
raise ctx.input.constructionError(
|
||||||
|
item.startPos, "Wrong tag for " & typetraits.name(T) & ": " & $item.mapProperties.tag)
|
||||||
elif item.mapProperties.anchor != yAnchorNone:
|
elif item.mapProperties.anchor != yAnchorNone:
|
||||||
raise ctx.input.constructionError(item.startPos, "Anchor on non-ref type")
|
raise ctx.input.constructionError(item.startPos, "Anchor on non-ref type")
|
||||||
of yamlStartSeq:
|
of yamlStartSeq:
|
||||||
if item.seqProperties.tag notin [yTagQuestionMark, yamlTag(T)]:
|
if item.seqProperties.tag notin [yTagQuestionMark, yamlTag(T)]:
|
||||||
raise ctx.input.constructionError(item.startPos, "Wrong tag for " & typetraits.name(T))
|
raise ctx.input.constructionError(
|
||||||
|
item.startPos, "Wrong tag for " & typetraits.name(T) & ": " & $item.seqProperties.tag)
|
||||||
elif item.seqProperties.anchor != yAnchorNone:
|
elif item.seqProperties.anchor != yAnchorNone:
|
||||||
raise ctx.input.constructionError(item.startPos, "Anchor on non-ref type")
|
raise ctx.input.constructionError(item.startPos, "Anchor on non-ref type")
|
||||||
of yamlAlias:
|
of yamlAlias:
|
||||||
|
@ -1474,7 +1477,8 @@ proc constructChild*(
|
||||||
if item.kind == yamlScalar:
|
if item.kind == yamlScalar:
|
||||||
if item.scalarProperties.tag notin
|
if item.scalarProperties.tag notin
|
||||||
[yTagQuestionMark, yTagExclamationMark, yamlTag(string)]:
|
[yTagQuestionMark, yTagExclamationMark, yamlTag(string)]:
|
||||||
raise ctx.input.constructionError(item.startPos, "Wrong tag for string")
|
raise ctx.input.constructionError(
|
||||||
|
item.startPos, "Wrong tag for string: " & $item.scalarProperties.tag)
|
||||||
elif item.scalarProperties.anchor != yAnchorNone:
|
elif item.scalarProperties.anchor != yAnchorNone:
|
||||||
raise ctx.input.constructionError(item.startPos, "Anchor on non-ref type")
|
raise ctx.input.constructionError(item.startPos, "Anchor on non-ref type")
|
||||||
ctx.constructObject(result)
|
ctx.constructObject(result)
|
||||||
|
@ -1486,7 +1490,8 @@ proc constructChild*[T](
|
||||||
let item = ctx.input.peek()
|
let item = ctx.input.peek()
|
||||||
if item.kind == yamlStartSeq:
|
if item.kind == yamlStartSeq:
|
||||||
if item.seqProperties.tag notin [yTagQuestionMark, yamlTag(seq[T])]:
|
if item.seqProperties.tag notin [yTagQuestionMark, yamlTag(seq[T])]:
|
||||||
raise ctx.input.constructionError(item.startPos, "Wrong tag for " & typetraits.name(seq[T]))
|
raise ctx.input.constructionError(
|
||||||
|
item.startPos, "Wrong tag for " & typetraits.name(seq[T]) & ": " & $item.seqProperties.tag)
|
||||||
elif item.seqProperties.anchor != yAnchorNone:
|
elif item.seqProperties.anchor != yAnchorNone:
|
||||||
raise ctx.input.constructionError(item.startPos, "Anchor on non-ref type")
|
raise ctx.input.constructionError(item.startPos, "Anchor on non-ref type")
|
||||||
ctx.constructObject(result)
|
ctx.constructObject(result)
|
||||||
|
@ -1498,7 +1503,8 @@ proc constructChild*[I, T](
|
||||||
let item = ctx.input.peek()
|
let item = ctx.input.peek()
|
||||||
if item.kind == yamlStartSeq:
|
if item.kind == yamlStartSeq:
|
||||||
if item.seqProperties.tag notin [yTagQuestionMark, yamlTag(array[I, T])]:
|
if item.seqProperties.tag notin [yTagQuestionMark, yamlTag(array[I, T])]:
|
||||||
raise ctx.input.constructionError(item.startPos, "Wrong tag for " & typetraits.name(array[I, T]))
|
raise ctx.input.constructionError(
|
||||||
|
item.startPos, "Wrong tag for " & typetraits.name(array[I, T]) & ": " & $item.seqProperties.tag)
|
||||||
elif item.seqProperties.anchor != yAnchorNone:
|
elif item.seqProperties.anchor != yAnchorNone:
|
||||||
raise ctx.input.constructionError(item.startPos, "Anchor on non-ref type")
|
raise ctx.input.constructionError(item.startPos, "Anchor on non-ref type")
|
||||||
ctx.constructObject(result)
|
ctx.constructObject(result)
|
||||||
|
@ -1528,7 +1534,7 @@ when defined(JS):
|
||||||
let e = ctx.input.peek()
|
let e = ctx.input.peek()
|
||||||
if e.kind == yamlScalar:
|
if e.kind == yamlScalar:
|
||||||
if e.scalarProperties.tag notin [yTagQuestionMark, yTagTimestamp]:
|
if e.scalarProperties.tag notin [yTagQuestionMark, yTagTimestamp]:
|
||||||
raise ctx.input.constructionError(e.startPos, "Wrong tag for Time")
|
raise ctx.input.constructionError(e.startPos, "Wrong tag for Time: " & $e.scalarProperties.tag)
|
||||||
elif guessType(e.scalarContent) != yTypeTimestamp:
|
elif guessType(e.scalarContent) != yTypeTimestamp:
|
||||||
raise ctx.input.constructionError(e.startPos, "Invalid timestamp")
|
raise ctx.input.constructionError(e.startPos, "Invalid timestamp")
|
||||||
elif e.scalarProperties.anchor != yAnchorNone:
|
elif e.scalarProperties.anchor != yAnchorNone:
|
||||||
|
|
|
@ -547,7 +547,6 @@ proc doPresent(
|
||||||
ctx.target.write("%YAML 1.1")
|
ctx.target.write("%YAML 1.1")
|
||||||
ctx.newline()
|
ctx.newline()
|
||||||
wroteDirectivesEnd = true
|
wroteDirectivesEnd = true
|
||||||
echo "write --- because %YAML"
|
|
||||||
of ovNone: discard
|
of ovNone: discard
|
||||||
for v in ctx.handles:
|
for v in ctx.handles:
|
||||||
if v.handle == "!":
|
if v.handle == "!":
|
||||||
|
|
Loading…
Reference in New Issue