2015-12-29 14:09:37 +00:00
|
|
|
import "../yaml"
|
2016-01-05 15:54:14 +00:00
|
|
|
import macros, strutils, streams, tables, json, hashes, re
|
2015-12-29 14:09:37 +00:00
|
|
|
export yaml, streams, tables, json
|
|
|
|
|
2016-01-04 20:46:33 +00:00
|
|
|
type
|
|
|
|
YamlTagStyle* = enum
|
|
|
|
ytsNone,
|
|
|
|
ytsRootOnly,
|
|
|
|
ytsAll
|
|
|
|
|
|
|
|
proc initSerializationTagLibrary(): YamlTagLibrary =
|
|
|
|
result = initTagLibrary()
|
|
|
|
result.tags["!"] = yTagExclamationMark
|
|
|
|
result.tags["?"] = yTagQuestionMark
|
|
|
|
result.tags["tag:yaml.org,2002:str"] = yTagString
|
|
|
|
result.tags["tag:yaml.org,2002:null"] = yTagNull
|
|
|
|
result.tags["tag:yaml.org,2002:bool"] = yTagBoolean
|
|
|
|
result.tags["tag:yaml.org,2002:int"] = yTagInteger
|
|
|
|
result.tags["tag:yaml.org,2002:float"] = yTagFloat
|
|
|
|
result.tags["tag:yaml.org,2002:timestamp"] = yTagTimestamp
|
|
|
|
result.tags["tag:yaml.org,2002:value"] = yTagValue
|
|
|
|
result.tags["tag:yaml.org,2002:binary"] = yTagBinary
|
|
|
|
|
|
|
|
var
|
|
|
|
serializationTagLibrary* = initSerializationTagLibrary() ## \
|
|
|
|
## contains all local tags that are used for type serialization. Does
|
|
|
|
## not contain any of the specific default tags for sequences or maps,
|
|
|
|
## as those are not suited for Nim's static type system.
|
|
|
|
##
|
|
|
|
## Should not be modified manually. Will be extended by
|
|
|
|
## `make_serializable <#make_serializable,stmt,stmt`_.
|
|
|
|
|
|
|
|
|
2015-12-29 14:09:37 +00:00
|
|
|
static:
|
|
|
|
iterator objectFields(n: NimNode): tuple[name: NimNode, t: NimNode] =
|
2016-01-05 15:54:14 +00:00
|
|
|
assert n.kind in [nnkRecList, nnkTupleTy]
|
2015-12-29 14:09:37 +00:00
|
|
|
for identDefs in n.children:
|
|
|
|
let numFields = identDefs.len - 2
|
|
|
|
for i in 0..numFields - 1:
|
|
|
|
yield (name: identDefs[i], t: identDefs[^2])
|
2016-01-05 15:54:14 +00:00
|
|
|
|
|
|
|
var existingTuples = newSeq[NimNode]()
|
2015-12-29 14:09:37 +00:00
|
|
|
|
2016-01-04 21:18:55 +00:00
|
|
|
template presentTag(t: typedesc, tagStyle: YamlTagStyle): TagId =
|
2016-01-04 20:46:33 +00:00
|
|
|
if tagStyle == ytsNone: yTagQuestionMark else: yamlTag(t)
|
|
|
|
|
|
|
|
proc lazyLoadTag*(uri: string): TagId {.inline.} =
|
|
|
|
try:
|
|
|
|
result = serializationTagLibrary.tags[uri]
|
|
|
|
except KeyError:
|
|
|
|
result = serializationTagLibrary.registerUri(uri)
|
|
|
|
|
2015-12-29 14:09:37 +00:00
|
|
|
macro make_serializable*(types: stmt): stmt =
|
|
|
|
assert types.kind == nnkTypeSection
|
|
|
|
result = newStmtList(types)
|
|
|
|
for typedef in types.children:
|
|
|
|
assert typedef.kind == nnkTypeDef
|
|
|
|
let
|
|
|
|
tName = $typedef[0].symbol
|
|
|
|
tIdent = newIdentNode(tName)
|
2016-01-05 15:54:14 +00:00
|
|
|
var
|
|
|
|
tUri: NimNode
|
|
|
|
recList: NimNode
|
2015-12-29 14:09:37 +00:00
|
|
|
assert typedef[1].kind == nnkEmpty
|
|
|
|
let objectTy = typedef[2]
|
2016-01-05 15:54:14 +00:00
|
|
|
case objectTy.kind
|
|
|
|
of nnkObjectTy:
|
|
|
|
assert objectTy[0].kind == nnkEmpty
|
|
|
|
assert objectTy[1].kind == nnkEmpty
|
|
|
|
tUri = newStrLitNode("!nim:" & tName)
|
|
|
|
recList = objectTy[2]
|
|
|
|
of nnkTupleTy:
|
|
|
|
if objectTy in existingTuples:
|
|
|
|
continue
|
|
|
|
existingTuples.add(objectTy)
|
|
|
|
|
|
|
|
recList = objectTy
|
|
|
|
tUri = newStmtList()
|
|
|
|
var
|
|
|
|
first = true
|
|
|
|
curStrLit = "!nim:tuple("
|
|
|
|
curInfix = tUri
|
|
|
|
for field in objectFields(recList):
|
|
|
|
if first:
|
|
|
|
first = false
|
|
|
|
else:
|
|
|
|
curStrLit &= ","
|
|
|
|
curStrLit &= $field.name & "="
|
|
|
|
var tmp = newNimNode(nnkInfix).add(newIdentNode("&"),
|
|
|
|
newStrLitNode(curStrLit))
|
|
|
|
curInfix.add(tmp)
|
|
|
|
curInfix = tmp
|
|
|
|
tmp = newNimNode(nnkInfix).add(newIdentNode("&"),
|
|
|
|
newCall("safeTagUri", newCall("yamlTag",
|
|
|
|
newCall("type", field.t))))
|
|
|
|
curInfix.add(tmp)
|
|
|
|
curInfix = tmp
|
|
|
|
curStrLit = ""
|
|
|
|
curInfix.add(newStrLitNode(curStrLit & ")"))
|
|
|
|
tUri = tUri[0]
|
|
|
|
else:
|
|
|
|
assert false
|
|
|
|
|
2016-01-04 20:46:33 +00:00
|
|
|
# yamlTag()
|
|
|
|
|
|
|
|
var yamlTagProc = newProc(newIdentNode("yamlTag"), [
|
|
|
|
newIdentNode("TagId"),
|
|
|
|
newIdentDefs(newIdentNode("T"), newNimNode(nnkBracketExpr).add(
|
|
|
|
newIdentNode("typedesc"), tIdent))])
|
2016-01-05 15:54:14 +00:00
|
|
|
var impl = newStmtList(newCall("lazyLoadTag", tUri))
|
2016-01-04 20:46:33 +00:00
|
|
|
yamlTagProc[6] = impl
|
|
|
|
result.add(yamlTagProc)
|
|
|
|
|
2015-12-29 15:10:47 +00:00
|
|
|
# construct()
|
|
|
|
|
2015-12-29 14:09:37 +00:00
|
|
|
var constructProc = newProc(newIdentNode("construct"), [
|
|
|
|
newEmptyNode(),
|
2015-12-29 15:10:47 +00:00
|
|
|
newIdentDefs(newIdentNode("s"), newIdentNode("YamlStream")),
|
2015-12-29 14:09:37 +00:00
|
|
|
newIdentDefs(newIdentNode("result"),
|
|
|
|
newNimNode(nnkVarTy).add(tIdent))])
|
2016-01-04 20:46:33 +00:00
|
|
|
impl = quote do:
|
2015-12-29 14:09:37 +00:00
|
|
|
var event = s()
|
|
|
|
if finished(s) or event.kind != yamlStartMap:
|
2016-01-04 20:46:33 +00:00
|
|
|
raise newException(ValueError, "Construction error!")
|
|
|
|
if event.mapTag != yTagQuestionMark and
|
|
|
|
event.mapTag != yamlTag(type(`tIdent`)):
|
2015-12-29 14:09:37 +00:00
|
|
|
raise newException(ValueError, "Wrong tag for " & `tName`)
|
|
|
|
event = s()
|
|
|
|
if finished(s):
|
2016-01-04 20:46:33 +00:00
|
|
|
raise newException(ValueError, "Construction error!")
|
2015-12-29 14:09:37 +00:00
|
|
|
while event.kind != yamlEndMap:
|
|
|
|
assert event.kind == yamlScalar
|
2016-01-04 20:46:33 +00:00
|
|
|
assert event.scalarTag in [yTagQuestionMark, yTagString]
|
2015-12-29 14:09:37 +00:00
|
|
|
case hash(event.scalarContent)
|
|
|
|
else:
|
|
|
|
raise newException(ValueError, "Unknown key for " &
|
|
|
|
`tName` & ": " & event.scalarContent)
|
|
|
|
event = s()
|
|
|
|
if finished(s):
|
2016-01-04 20:46:33 +00:00
|
|
|
raise newException(ValueError, "Construction error!")
|
2016-01-05 17:14:11 +00:00
|
|
|
var keyCase = impl[5][1][2]
|
2015-12-29 14:09:37 +00:00
|
|
|
assert keyCase.kind == nnkCaseStmt
|
|
|
|
for field in objectFields(recList):
|
|
|
|
let nameHash = hash($field.name.ident)
|
|
|
|
keyCase.insert(1, newNimNode(nnkOfBranch).add(
|
|
|
|
newIntLitNode(nameHash)).add(newStmtList(
|
|
|
|
newCall("construct", [newIdentNode("s"), newDotExpr(
|
|
|
|
newIdentNode("result"), field.name)])
|
|
|
|
))
|
|
|
|
)
|
|
|
|
|
|
|
|
constructProc[6] = impl
|
|
|
|
result.add(constructProc)
|
2015-12-29 15:10:47 +00:00
|
|
|
|
|
|
|
# serialize()
|
|
|
|
|
|
|
|
var serializeProc = newProc(newIdentNode("serialize"), [
|
|
|
|
newIdentNode("YamlStream"),
|
|
|
|
newIdentDefs(newIdentNode("value"), tIdent),
|
2016-01-04 20:46:33 +00:00
|
|
|
newIdentDefs(newIdentNode("tagStyle"),
|
|
|
|
newIdentNode("YamlTagStyle"),
|
|
|
|
newIdentNode("ytsNone"))])
|
2016-01-04 21:18:55 +00:00
|
|
|
var iterBody = newStmtList(
|
|
|
|
newLetStmt(newIdentNode("childTagStyle"), newNimNode(nnkIfExpr).add(
|
|
|
|
newNimNode(nnkElifExpr).add(
|
|
|
|
newNimNode(nnkInfix).add(newIdentNode("=="),
|
|
|
|
newIdentNode("tagStyle"), newIdentNode("ytsRootOnly")),
|
|
|
|
newIdentNode("ytsNone")
|
|
|
|
), newNimNode(nnkElseExpr).add(newIdentNode("tagStyle")))),
|
|
|
|
newNimNode(nnkYieldStmt).add(
|
|
|
|
newNimNode(nnkObjConstr).add(newIdentNode("YamlStreamEvent"),
|
|
|
|
newNimNode(nnkExprColonExpr).add(newIdentNode("kind"),
|
|
|
|
newIdentNode("yamlStartMap")),
|
|
|
|
newNimNode(nnkExprColonExpr).add(newIdentNode("mapTag"),
|
|
|
|
newNimNode(nnkIfExpr).add(newNimNode(nnkElifExpr).add(
|
|
|
|
newNimNode(nnkInfix).add(newIdentNode("=="),
|
|
|
|
newIdentNode("tagStyle"),
|
|
|
|
newIdentNode("ytsNone")),
|
|
|
|
newIdentNode("yTagQuestionMark")
|
|
|
|
), newNimNode(nnkElseExpr).add(
|
|
|
|
newCall("yamlTag", newCall("type", tIdent))
|
|
|
|
))),
|
|
|
|
newNimNode(nnkExprColonExpr).add(newIdentNode("mapAnchor"),
|
|
|
|
newIdentNode("yAnchorNone"))
|
|
|
|
)
|
2016-01-04 20:46:33 +00:00
|
|
|
), newNimNode(nnkYieldStmt).add(newNimNode(nnkObjConstr).add(
|
|
|
|
newIdentNode("YamlStreamEvent"), newNimNode(nnkExprColonExpr).add(
|
|
|
|
newIdentNode("kind"), newIdentNode("yamlEndMap")
|
|
|
|
)
|
|
|
|
)))
|
2015-12-29 15:10:47 +00:00
|
|
|
|
2016-01-04 21:18:55 +00:00
|
|
|
var i = 2
|
2015-12-29 15:10:47 +00:00
|
|
|
for field in objectFields(recList):
|
|
|
|
let
|
|
|
|
fieldIterIdent = newIdentNode($field.name & "Events")
|
|
|
|
fieldNameString = newStrLitNode($field.name)
|
|
|
|
iterbody.insert(i, quote do:
|
|
|
|
yield YamlStreamEvent(kind: yamlScalar,
|
2016-01-04 21:18:55 +00:00
|
|
|
scalarTag: presentTag(string,
|
|
|
|
childTagStyle),
|
2015-12-29 15:10:47 +00:00
|
|
|
scalarAnchor: yAnchorNone,
|
|
|
|
scalarContent: `fieldNameString`)
|
|
|
|
)
|
|
|
|
iterbody.insert(i + 1, newVarStmt(fieldIterIdent,
|
|
|
|
newCall("serialize", newDotExpr(newIdentNode("value"),
|
2016-01-04 21:18:55 +00:00
|
|
|
field.name), newIdentNode("childTagStyle"))))
|
2015-12-29 15:10:47 +00:00
|
|
|
iterbody.insert(i + 2, quote do:
|
|
|
|
for event in `fieldIterIdent`():
|
|
|
|
yield event
|
|
|
|
)
|
|
|
|
i += 3
|
|
|
|
impl = newStmtList(newAssignment(newIdentNode("result"), newProc(
|
|
|
|
newEmptyNode(), [newIdentNode("YamlStreamEvent")], iterBody,
|
|
|
|
nnkIteratorDef)))
|
|
|
|
serializeProc[6] = impl
|
|
|
|
result.add(serializeProc)
|
2015-12-29 14:09:37 +00:00
|
|
|
|
|
|
|
proc prepend*(event: YamlStreamEvent, s: YamlStream): YamlStream =
|
|
|
|
result = iterator(): YamlStreamEvent =
|
|
|
|
yield event
|
|
|
|
for e in s():
|
|
|
|
yield e
|
|
|
|
|
2016-01-04 20:46:33 +00:00
|
|
|
proc yamlTag*(T: typedesc[string]): TagId {.inline.} = yTagString
|
|
|
|
|
2016-01-05 15:54:14 +00:00
|
|
|
proc safeTagUri*(id: TagId): string =
|
|
|
|
let uri = serializationTagLibrary.uri(id)
|
|
|
|
if uri.len > 0 and uri[0] == '!':
|
|
|
|
return uri[1..uri.len - 1]
|
|
|
|
else:
|
|
|
|
return uri
|
|
|
|
|
2015-12-29 15:10:47 +00:00
|
|
|
proc construct*(s: YamlStream, result: var string) =
|
2015-12-29 14:09:37 +00:00
|
|
|
let item = s()
|
|
|
|
if finished(s) or item.kind != yamlScalar:
|
2016-01-05 18:06:55 +00:00
|
|
|
raise newException(ValueError, "Construction error!")
|
2015-12-29 14:09:37 +00:00
|
|
|
if item.scalarTag notin [yTagQuestionMark, yTagExclamationMark, yTagString]:
|
|
|
|
raise newException(ValueError, "Wrong tag for string.")
|
|
|
|
result = item.scalarContent
|
|
|
|
|
2016-01-04 20:46:33 +00:00
|
|
|
proc serialize*(value: string,
|
|
|
|
tagStyle: YamlTagStyle = ytsNone): YamlStream =
|
2015-12-29 15:10:47 +00:00
|
|
|
result = iterator(): YamlStreamEvent =
|
2016-01-04 21:18:55 +00:00
|
|
|
yield YamlStreamEvent(kind: yamlScalar,
|
|
|
|
scalarTag: presentTag(string, tagStyle),
|
2016-01-04 20:46:33 +00:00
|
|
|
scalarAnchor: yAnchorNone, scalarContent: value)
|
|
|
|
|
|
|
|
proc yamlTag*(T: typedesc[int]): TagId {.inline.} = yTagInteger
|
2015-12-29 15:10:47 +00:00
|
|
|
|
|
|
|
proc construct*(s: YamlStream, result: var int) =
|
2015-12-29 14:09:37 +00:00
|
|
|
let item = s()
|
|
|
|
if finished(s) or item.kind != yamlScalar:
|
|
|
|
raise newException(ValueError, "Construction error!")
|
2016-01-05 15:54:14 +00:00
|
|
|
if item.scalarTag != yTagInteger and not (
|
|
|
|
item.scalarTag == yTagQuestionMark and item.scalarType == yTypeInteger):
|
2015-12-29 14:09:37 +00:00
|
|
|
raise newException(ValueError, "Wrong scalar type for int.")
|
|
|
|
result = parseInt(item.scalarContent)
|
|
|
|
|
2016-01-04 20:46:33 +00:00
|
|
|
proc serialize*(value: int,
|
|
|
|
tagStyle: YamlTagStyle = ytsNone): YamlStream =
|
2015-12-29 15:10:47 +00:00
|
|
|
result = iterator(): YamlStreamEvent =
|
2016-01-04 20:46:33 +00:00
|
|
|
yield YamlStreamEvent(kind: yamlScalar,
|
2016-01-04 21:18:55 +00:00
|
|
|
scalarTag: presentTag(int, tagStyle),
|
2016-01-04 20:46:33 +00:00
|
|
|
scalarAnchor: yAnchorNone, scalarContent: $value)
|
|
|
|
|
|
|
|
proc yamlTag*(T: typedesc[int64]): TagId {.inline.} = yTagInteger
|
2015-12-29 15:10:47 +00:00
|
|
|
|
|
|
|
proc contruct*(s: YamlStream, result: var int64) =
|
2015-12-29 14:09:37 +00:00
|
|
|
let item = s()
|
|
|
|
if finished(s) or item.kind != yamlScalar:
|
|
|
|
raise newException(ValueError, "Construction error!")
|
2016-01-05 15:54:14 +00:00
|
|
|
if item.scalarTag != yTagInteger and not (
|
|
|
|
item.scalarTag == yTagQuestionMark and item.scalarType == yTypeInteger):
|
2015-12-29 14:09:37 +00:00
|
|
|
raise newException(ValueError, "Wrong scalar type for int64.")
|
|
|
|
result = parseBiggestInt(item.scalarContent)
|
|
|
|
|
2016-01-04 20:46:33 +00:00
|
|
|
proc serialize*(value: int64,
|
|
|
|
tagStyle: YamlTagStyle = ytsNone): YamlStream =
|
2015-12-29 15:10:47 +00:00
|
|
|
result = iterator(): YamlStreamEvent =
|
2016-01-04 21:18:55 +00:00
|
|
|
yield YamlStreamEvent(kind: yamlScalar,
|
|
|
|
scalarTag: presentTag(int64, tagStyle),
|
2016-01-04 20:46:33 +00:00
|
|
|
scalarAnchor: yAnchorNone, scalarContent: $value)
|
|
|
|
|
|
|
|
proc yamlTag*(T: typedesc[float]): TagId {.inline.} = yTagFloat
|
2015-12-29 15:10:47 +00:00
|
|
|
|
|
|
|
proc construct*(s: YamlStream, result: var float) =
|
2015-12-29 14:09:37 +00:00
|
|
|
let item = s()
|
|
|
|
if finished(s) or item.kind != yamlScalar:
|
|
|
|
raise newException(ValueError, "Construction error!")
|
2016-01-05 15:54:14 +00:00
|
|
|
if item.scalarTag != yTagFloat and not (
|
|
|
|
item.scalarTag == yTagQuestionMark and item.scalarType == yTypeFloat):
|
2015-12-29 14:09:37 +00:00
|
|
|
raise newException(ValueError, "Wrong scalar type for float.")
|
|
|
|
case item.scalarType
|
|
|
|
of yTypeFloat:
|
|
|
|
result = parseFloat(item.scalarContent)
|
|
|
|
of yTypeFloatInf:
|
|
|
|
if item.scalarContent[0] == '-':
|
|
|
|
result = NegInf
|
|
|
|
else:
|
|
|
|
result = Inf
|
|
|
|
of yTypeFloatNaN:
|
|
|
|
result = NaN
|
|
|
|
else:
|
|
|
|
raise newException(ValueError, "Wrong scalar type for float.")
|
|
|
|
|
2016-01-04 20:46:33 +00:00
|
|
|
proc serialize*(value: float,
|
|
|
|
tagStyle: YamlTagStyle = ytsNone): YamlStream =
|
2015-12-29 15:10:47 +00:00
|
|
|
result = iterator(): YamlStreamEvent =
|
2016-01-05 18:06:55 +00:00
|
|
|
var
|
|
|
|
asString: string
|
|
|
|
hint: YamlTypeHint
|
|
|
|
case value
|
|
|
|
of Inf:
|
|
|
|
asString = ".inf"
|
|
|
|
hint = yTypeFloatInf
|
|
|
|
of NegInf:
|
|
|
|
asString = "-.inf"
|
|
|
|
hint = yTypeFloatInf
|
|
|
|
of NaN:
|
|
|
|
asString = ".nan"
|
|
|
|
hint = yTypeFloatNaN
|
|
|
|
else:
|
|
|
|
asString = $value
|
|
|
|
hint = yTypeFloat
|
2015-12-29 15:10:47 +00:00
|
|
|
|
2016-01-04 21:18:55 +00:00
|
|
|
yield YamlStreamEvent(kind: yamlScalar,
|
|
|
|
scalarTag: presentTag(float, tagStyle),
|
2016-01-05 18:06:55 +00:00
|
|
|
scalarAnchor: yAnchorNone, scalarContent: asString,
|
|
|
|
scalarType: hint)
|
2015-12-29 15:10:47 +00:00
|
|
|
|
2016-01-04 20:46:33 +00:00
|
|
|
proc yamlTag*(T: typedesc[bool]): TagId {.inline.} = yTagBoolean
|
|
|
|
|
2015-12-29 15:10:47 +00:00
|
|
|
proc construct*(s: YamlStream, result: var bool) =
|
2015-12-29 14:09:37 +00:00
|
|
|
let item = s()
|
|
|
|
if finished(s) or item.kind != yamlScalar:
|
|
|
|
raise newException(ValueError, "Construction error!")
|
2016-01-05 15:54:14 +00:00
|
|
|
case item.scalarTag
|
|
|
|
of yTagQuestionMark:
|
|
|
|
case item.scalarType
|
|
|
|
of yTypeBoolTrue:
|
|
|
|
result = true
|
|
|
|
of yTypeBoolFalse:
|
|
|
|
result = false
|
|
|
|
else:
|
|
|
|
raise newException(ValueError, "Wrong scalar type for bool.")
|
|
|
|
of yTagBoolean:
|
|
|
|
if item.scalarContent.match(
|
|
|
|
re"y|Y|yes|Yes|YES|true|True|TRUE|on|On|ON"):
|
|
|
|
result = true
|
|
|
|
elif item.scalarContent.match(
|
|
|
|
re"n|N|no|No|NO|false|False|FALSE|off|Off|OFF"):
|
|
|
|
result = false
|
|
|
|
else:
|
|
|
|
raise newException(ValueError, "Wrong content for bool.")
|
2015-12-29 14:09:37 +00:00
|
|
|
else:
|
2016-01-05 15:54:14 +00:00
|
|
|
raise newException(ValueError, "Wrong scalar type for bool")
|
|
|
|
|
2016-01-04 20:46:33 +00:00
|
|
|
proc serialize*(value: bool,
|
|
|
|
tagStyle: YamlTagStyle = ytsNone): YamlStream =
|
2015-12-29 15:10:47 +00:00
|
|
|
result = iterator(): YamlStreamEvent =
|
2016-01-04 20:46:33 +00:00
|
|
|
yield YamlStreamEvent(kind: yamlScalar,
|
2016-01-04 21:18:55 +00:00
|
|
|
scalarTag: presentTag(bool, tagStyle),
|
2016-01-04 20:46:33 +00:00
|
|
|
scalarAnchor: yAnchorNone, scalarContent:
|
|
|
|
if value: "y" else: "n")
|
|
|
|
|
|
|
|
proc yamlTag*[I](T: typedesc[seq[I]]): TagId {.inline.} =
|
2016-01-05 15:54:14 +00:00
|
|
|
let uri = "!nim:seq(" & safeTagUri(yamlTag(I)) & ")"
|
2016-01-04 20:46:33 +00:00
|
|
|
result = lazyLoadTag(uri)
|
2015-12-29 15:10:47 +00:00
|
|
|
|
|
|
|
proc construct*[T](s: YamlStream, result: var seq[T]) =
|
2015-12-29 14:09:37 +00:00
|
|
|
var event = s()
|
|
|
|
if finished(s) or event.kind != yamlStartSequence:
|
2016-01-04 20:46:33 +00:00
|
|
|
raise newException(ValueError, "Construction error!1")
|
|
|
|
if event.seqTag != yTagQuestionMark and
|
|
|
|
event.seqTag != yamlTag(seq[T]):
|
2015-12-29 14:09:37 +00:00
|
|
|
raise newException(ValueError, "Wrong sequence type for seq[T]")
|
|
|
|
result = newSeq[T]()
|
|
|
|
event = s()
|
|
|
|
if finished(s):
|
2016-01-04 20:46:33 +00:00
|
|
|
raise newException(ValueError, "Construction error!2")
|
2015-12-29 14:09:37 +00:00
|
|
|
while event.kind != yamlEndSequence:
|
|
|
|
var
|
|
|
|
item: T
|
|
|
|
events = prepend(event, s)
|
|
|
|
construct(events, item)
|
|
|
|
result.add(item)
|
|
|
|
event = s()
|
|
|
|
if finished(s):
|
2016-01-04 20:46:33 +00:00
|
|
|
raise newException(ValueError, "Construction error!3")
|
2015-12-29 14:09:37 +00:00
|
|
|
|
2016-01-04 20:46:33 +00:00
|
|
|
proc serialize*[T](value: seq[T],
|
|
|
|
tagStyle: YamlTagStyle = ytsNone): YamlStream =
|
2015-12-29 15:10:47 +00:00
|
|
|
result = iterator(): YamlStreamEvent =
|
2016-01-04 21:18:55 +00:00
|
|
|
let childTagStyle = if tagStyle == ytsRootOnly: ytsNone else: tagStyle
|
2016-01-04 20:46:33 +00:00
|
|
|
yield YamlStreamEvent(kind: yamlStartSequence,
|
2016-01-04 21:18:55 +00:00
|
|
|
seqTag: presentTag(seq[T], tagStyle),
|
2015-12-29 15:10:47 +00:00
|
|
|
seqAnchor: yAnchorNone)
|
|
|
|
for item in value:
|
2016-01-04 21:18:55 +00:00
|
|
|
var events = serialize(item, childTagStyle)
|
2015-12-29 15:10:47 +00:00
|
|
|
for event in events():
|
|
|
|
yield event
|
|
|
|
yield YamlStreamEvent(kind: yamlEndSequence)
|
|
|
|
|
2016-01-04 20:46:33 +00:00
|
|
|
proc yamlTag*[K, V](T: typedesc[Table[K, V]]): TagId {.inline.} =
|
|
|
|
let
|
|
|
|
keyUri = serializationTagLibrary.uri(yamlTag(K))
|
|
|
|
valueUri = serializationTagLibrary.uri(yamlTag(V))
|
|
|
|
keyIdent = if keyUri[0] == '!': keyUri[1..keyUri.len - 1] else: keyUri
|
|
|
|
valueIdent = if valueUri[0] == '!':
|
|
|
|
valueUri[1..valueUri.len - 1] else: valueUri
|
|
|
|
uri = "!nim:Table(" & keyUri & "," & valueUri & ")"
|
|
|
|
result = lazyLoadTag(uri)
|
|
|
|
|
2015-12-29 15:10:47 +00:00
|
|
|
proc construct*[K, V](s: YamlStream, result: var Table[K, V]) =
|
2015-12-29 14:09:37 +00:00
|
|
|
var event = s()
|
|
|
|
if finished(s) or event.kind != yamlStartMap:
|
|
|
|
raise newException(ValueError, "Construction error!")
|
2016-01-04 20:46:33 +00:00
|
|
|
if event.mapTag != yTagQuestionMark and
|
|
|
|
event.mapTag != yamlTag(Table[K, V]):
|
2015-12-29 14:09:37 +00:00
|
|
|
raise newException(ValueError, "Wrong map type for Table[K, V]")
|
|
|
|
result = initTable[K, V]()
|
|
|
|
event = s()
|
|
|
|
if finished(s):
|
|
|
|
raise newException(ValueError, "Construction error!")
|
|
|
|
while event.kind != yamlEndMap:
|
|
|
|
var
|
|
|
|
key: K
|
|
|
|
value: V
|
|
|
|
events = prepend(event, s)
|
|
|
|
construct(events, key)
|
|
|
|
construct(s, value)
|
|
|
|
result[key] = value
|
|
|
|
event = s()
|
|
|
|
if finished(s):
|
2015-12-29 15:10:47 +00:00
|
|
|
raise newException(ValueError, "Construction error!")
|
|
|
|
|
|
|
|
proc serialize*[K, V](value: Table[K, V],
|
2016-01-04 20:46:33 +00:00
|
|
|
tagStyle: YamlTagStyle = ytsNone): YamlStream =
|
2015-12-29 15:10:47 +00:00
|
|
|
result = iterator(): YamlStreamEvent =
|
2016-01-04 21:18:55 +00:00
|
|
|
let childTagStyle = if tagStyle == ytsRootOnly: ytsNone else: tagStyle
|
|
|
|
yield YamlStreamEvent(kind: yamlStartMap,
|
|
|
|
mapTag: presentTag(Table[K, V], tagStyle),
|
2015-12-29 15:10:47 +00:00
|
|
|
mapAnchor: yAnchorNone)
|
|
|
|
for key, value in value.pairs:
|
2016-01-04 21:18:55 +00:00
|
|
|
var events = serialize(key, childTagStyle)
|
2015-12-29 15:10:47 +00:00
|
|
|
for event in events():
|
|
|
|
yield event
|
2016-01-04 21:18:55 +00:00
|
|
|
events = serialize(value, childTagStyle)
|
2015-12-29 15:10:47 +00:00
|
|
|
for event in events():
|
|
|
|
yield event
|
2015-12-29 17:22:55 +00:00
|
|
|
yield YamlStreamEvent(kind: yamlEndMap)
|
|
|
|
|
|
|
|
proc load*[K](input: Stream, target: var K) =
|
|
|
|
var
|
2016-01-05 15:54:14 +00:00
|
|
|
parser = newParser(serializationTagLibrary)
|
2015-12-29 17:22:55 +00:00
|
|
|
events = parser.parse(input)
|
|
|
|
assert events().kind == yamlStartDocument
|
|
|
|
construct(events, target)
|
|
|
|
assert events().kind == yamlEndDocument
|
|
|
|
|
2016-01-04 20:46:33 +00:00
|
|
|
proc dump*[K](value: K, target: Stream,
|
|
|
|
style: YamlPresentationStyle = ypsDefault,
|
|
|
|
tagStyle: YamlTagStyle = ytsRootOnly, indentationStep: int = 2) =
|
|
|
|
var serialized = serialize(value,
|
|
|
|
if style == ypsCanonical: ytsAll else: tagStyle)
|
2015-12-29 17:22:55 +00:00
|
|
|
var events = iterator(): YamlStreamEvent =
|
|
|
|
yield YamlStreamEvent(kind: yamlStartDocument)
|
|
|
|
for event in serialized():
|
|
|
|
yield event
|
|
|
|
yield YamlStreamEvent(kind: yamlEndDocument)
|
2016-01-04 20:46:33 +00:00
|
|
|
present(events, target, serializationTagLibrary, style, indentationStep)
|