2016-02-27 12:09:50 +00:00
|
|
|
# NimYAML - YAML implementation in Nim
|
2020-06-26 21:21:22 +00:00
|
|
|
# (c) Copyright 2016 - 2020 Felix Krause
|
2016-02-27 12:09:50 +00:00
|
|
|
#
|
|
|
|
# See the file "copying.txt", included in this
|
|
|
|
# distribution, for details about the copyright.
|
|
|
|
|
2016-09-21 19:20:57 +00:00
|
|
|
## =========================
|
|
|
|
## Module yaml.serialization
|
|
|
|
## =========================
|
|
|
|
##
|
|
|
|
## This is the most high-level API of NimYAML. It enables you to parse YAML
|
|
|
|
## character streams directly into native YAML types and vice versa. It builds
|
|
|
|
## on top of the low-level parser and presenter APIs.
|
|
|
|
##
|
|
|
|
## It is possible to define custom construction and serialization procs for any
|
|
|
|
## type. Please consult the serialization guide on the NimYAML website for more
|
|
|
|
## information.
|
|
|
|
|
2020-03-08 18:58:02 +00:00
|
|
|
import tables, typetraits, strutils, macros, streams, times, parseutils, options
|
2020-11-03 21:08:21 +00:00
|
|
|
import data, parser, taglib, presenter, stream, private/internal, hints, annotations
|
2020-06-26 21:21:22 +00:00
|
|
|
export stream, macros, annotations, options
|
2016-10-08 21:35:33 +00:00
|
|
|
# *something* in here needs externally visible `==`(x,y: AnchorId),
|
|
|
|
# but I cannot figure out what. binding it would be the better option.
|
2016-09-20 19:53:38 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
SerializationContext* = ref object
|
|
|
|
## Context information for the process of serializing YAML from Nim values.
|
2017-03-29 19:42:07 +00:00
|
|
|
when not defined(JS):
|
2020-11-03 21:08:21 +00:00
|
|
|
refs*: Table[pointer, Anchor] # `pointer` does not work with JS
|
2016-09-20 19:53:38 +00:00
|
|
|
style: AnchorStyle
|
2020-11-03 21:08:21 +00:00
|
|
|
nextAnchorId*: string
|
|
|
|
put*: proc(e: Event) {.raises: [], closure.}
|
2016-09-20 19:53:38 +00:00
|
|
|
|
|
|
|
ConstructionContext* = ref object
|
|
|
|
## Context information for the process of constructing Nim values from YAML.
|
2017-03-29 19:42:07 +00:00
|
|
|
when not defined(JS):
|
2020-11-03 21:08:21 +00:00
|
|
|
refs*: Table[Anchor, pointer]
|
2016-09-20 19:53:38 +00:00
|
|
|
|
|
|
|
YamlConstructionError* = object of YamlLoadingError
|
|
|
|
## Exception that may be raised when constructing data objects from a
|
|
|
|
## `YamlStream <#YamlStream>`_. The fields ``line``, ``column`` and
|
|
|
|
## ``lineContent`` are only available if the costructing proc also does
|
|
|
|
## parsing, because otherwise this information is not available to the
|
|
|
|
## costruction proc.
|
|
|
|
|
|
|
|
# forward declares
|
|
|
|
|
|
|
|
proc constructChild*[T](s: var YamlStream, c: ConstructionContext,
|
|
|
|
result: var T)
|
|
|
|
{.raises: [YamlConstructionError, YamlStreamError].}
|
|
|
|
## Constructs an arbitrary Nim value from a part of a YAML stream.
|
|
|
|
## The stream will advance until after the finishing token that was used
|
|
|
|
## for constructing the value. The ``ConstructionContext`` is needed for
|
|
|
|
## potential child objects which may be refs.
|
|
|
|
|
|
|
|
proc constructChild*(s: var YamlStream, c: ConstructionContext,
|
|
|
|
result: var string)
|
|
|
|
{.raises: [YamlConstructionError, YamlStreamError].}
|
|
|
|
## Constructs a Nim value that is a string from a part of a YAML stream.
|
|
|
|
## This specialization takes care of possible nil strings.
|
|
|
|
|
|
|
|
proc constructChild*[T](s: var YamlStream, c: ConstructionContext,
|
|
|
|
result: var seq[T])
|
|
|
|
{.raises: [YamlConstructionError, YamlStreamError].}
|
|
|
|
## Constructs a Nim value that is a string from a part of a YAML stream.
|
|
|
|
## This specialization takes care of possible nil seqs.
|
|
|
|
|
|
|
|
proc constructChild*[O](s: var YamlStream, c: ConstructionContext,
|
|
|
|
result: var ref O)
|
2019-11-30 12:16:40 +00:00
|
|
|
{.raises: [YamlConstructionError, YamlStreamError].}
|
2016-09-20 19:53:38 +00:00
|
|
|
## Constructs an arbitrary Nim value from a part of a YAML stream.
|
|
|
|
## The stream will advance until after the finishing token that was used
|
|
|
|
## for constructing the value. The object may be constructed from an alias
|
|
|
|
## node which will be resolved using the ``ConstructionContext``.
|
|
|
|
|
|
|
|
proc representChild*[O](value: ref O, ts: TagStyle, c: SerializationContext)
|
2016-09-22 12:25:10 +00:00
|
|
|
{.raises: [].}
|
2016-09-20 19:53:38 +00:00
|
|
|
## Represents an arbitrary Nim reference value as YAML object. The object
|
|
|
|
## may be represented as alias node if it is already present in the
|
|
|
|
## ``SerializationContext``.
|
|
|
|
|
|
|
|
proc representChild*(value: string, ts: TagStyle, c: SerializationContext)
|
|
|
|
{.inline, raises: [].}
|
|
|
|
## Represents a Nim string. Supports nil strings.
|
|
|
|
|
2016-09-22 12:25:10 +00:00
|
|
|
proc representChild*[O](value: O, ts: TagStyle, c: SerializationContext)
|
2016-09-20 19:53:38 +00:00
|
|
|
## Represents an arbitrary Nim object as YAML object.
|
|
|
|
|
2016-03-25 22:12:00 +00:00
|
|
|
proc newConstructionContext*(): ConstructionContext =
|
2016-04-02 15:48:22 +00:00
|
|
|
new(result)
|
2017-03-29 19:42:07 +00:00
|
|
|
when defined(JS):
|
|
|
|
{.emit: [result, """.refs = new Map();"""].}
|
|
|
|
else:
|
2020-11-03 21:08:21 +00:00
|
|
|
result.refs = initTable[Anchor, pointer]()
|
2016-01-28 21:29:26 +00:00
|
|
|
|
2016-09-01 18:56:34 +00:00
|
|
|
proc newSerializationContext*(s: AnchorStyle,
|
2020-11-03 21:08:21 +00:00
|
|
|
putImpl: proc(e: Event) {.raises: [], closure.}):
|
2016-09-01 18:56:34 +00:00
|
|
|
SerializationContext =
|
2020-11-03 21:08:21 +00:00
|
|
|
result = SerializationContext(style: s, nextAnchorId: "a",
|
2017-03-29 19:42:07 +00:00
|
|
|
put: putImpl)
|
|
|
|
when defined(JS):
|
|
|
|
{.emit: [result, """.refs = new Map();"""].}
|
2020-11-03 21:08:21 +00:00
|
|
|
else: result.refs = initTable[pointer, Anchor]()
|
2016-07-08 09:35:31 +00:00
|
|
|
|
2016-02-16 18:24:55 +00:00
|
|
|
template presentTag*(t: typedesc, ts: TagStyle): TagId =
|
2016-04-02 15:48:22 +00:00
|
|
|
## Get the TagId that represents the given type in the given style
|
|
|
|
if ts == tsNone: yTagQuestionMark else: yamlTag(t)
|
2016-01-04 20:46:33 +00:00
|
|
|
|
2016-02-26 20:13:40 +00:00
|
|
|
proc lazyLoadTag(uri: string): TagId {.inline, raises: [].} =
|
2016-04-02 15:48:22 +00:00
|
|
|
try: result = serializationTagLibrary.tags[uri]
|
|
|
|
except KeyError: result = serializationTagLibrary.registerUri(uri)
|
2016-01-04 20:46:33 +00:00
|
|
|
|
2016-02-26 20:13:40 +00:00
|
|
|
proc safeTagUri(id: TagId): string {.raises: [].} =
|
2016-04-02 15:48:22 +00:00
|
|
|
try:
|
2016-10-10 18:16:54 +00:00
|
|
|
var
|
|
|
|
uri = serializationTagLibrary.uri(id)
|
|
|
|
i = 0
|
|
|
|
# '!' is not allowed inside a tag handle
|
|
|
|
if uri.len > 0 and uri[0] == '!': uri = uri[1..^1]
|
|
|
|
# ',' is not allowed after a tag handle in the suffix because it's a flow
|
|
|
|
# indicator
|
|
|
|
for c in uri.mitems():
|
|
|
|
if c == ',': c = ';'
|
|
|
|
inc(i)
|
|
|
|
return uri
|
2016-08-09 18:47:22 +00:00
|
|
|
except KeyError: internalError("Unexpected KeyError for TagId " & $id)
|
2016-01-05 15:54:14 +00:00
|
|
|
|
2016-09-23 13:42:24 +00:00
|
|
|
proc constructionError(s: YamlStream, msg: string): ref YamlConstructionError =
|
|
|
|
result = newException(YamlConstructionError, msg)
|
2020-11-03 21:08:21 +00:00
|
|
|
if not s.getLastTokenContext(result.mark.line, result.mark.column, result.lineContent):
|
|
|
|
(result.mark.line, result.mark.column) = (-1, -1)
|
2016-09-23 13:42:24 +00:00
|
|
|
result.lineContent = ""
|
|
|
|
|
2016-08-17 20:50:37 +00:00
|
|
|
template constructScalarItem*(s: var YamlStream, i: untyped,
|
2016-02-26 20:13:40 +00:00
|
|
|
t: typedesc, content: untyped) =
|
2016-04-02 15:48:22 +00:00
|
|
|
## Helper template for implementing ``constructObject`` for types that
|
|
|
|
## are constructed from a scalar. ``i`` is the identifier that holds
|
2020-11-03 21:08:21 +00:00
|
|
|
## the scalar as ``Event`` in the content. Exceptions raised in
|
2016-04-02 15:48:22 +00:00
|
|
|
## the content will be automatically catched and wrapped in
|
|
|
|
## ``YamlConstructionError``, which will then be raised.
|
2016-09-23 13:42:24 +00:00
|
|
|
bind constructionError
|
2016-07-08 09:35:31 +00:00
|
|
|
let i = s.next()
|
2016-04-02 15:48:22 +00:00
|
|
|
if i.kind != yamlScalar:
|
2016-09-23 13:42:24 +00:00
|
|
|
raise constructionError(s, "Expected scalar")
|
2016-04-02 15:48:22 +00:00
|
|
|
try: content
|
2019-06-11 19:30:14 +00:00
|
|
|
except YamlConstructionError as e: raise e
|
2016-04-02 15:48:22 +00:00
|
|
|
except Exception:
|
2016-09-23 13:42:24 +00:00
|
|
|
var e = constructionError(s,
|
2016-11-10 09:12:32 +00:00
|
|
|
"Cannot construct to " & name(t) & ": " & item.scalarContent &
|
|
|
|
"; error: " & getCurrentExceptionMsg())
|
2016-04-02 15:48:22 +00:00
|
|
|
e.parent = getCurrentException()
|
|
|
|
raise e
|
2016-01-24 19:38:30 +00:00
|
|
|
|
2016-02-15 18:46:21 +00:00
|
|
|
proc yamlTag*(T: typedesc[string]): TagId {.inline, noSideEffect, raises: [].} =
|
2016-04-02 15:48:22 +00:00
|
|
|
yTagString
|
2016-01-24 19:38:30 +00:00
|
|
|
|
2016-02-12 18:53:25 +00:00
|
|
|
proc constructObject*(s: var YamlStream, c: ConstructionContext,
|
|
|
|
result: var string)
|
2016-04-02 15:48:22 +00:00
|
|
|
{.raises: [YamlConstructionError, YamlStreamError].} =
|
|
|
|
## costructs a string from a YAML scalar
|
|
|
|
constructScalarItem(s, item, string):
|
|
|
|
result = item.scalarContent
|
2015-12-29 14:09:37 +00:00
|
|
|
|
2016-03-25 20:27:41 +00:00
|
|
|
proc representObject*(value: string, ts: TagStyle,
|
2016-09-01 18:56:34 +00:00
|
|
|
c: SerializationContext, tag: TagId) {.raises: [].} =
|
2016-04-02 15:48:22 +00:00
|
|
|
## represents a string as YAML scalar
|
2016-09-01 18:56:34 +00:00
|
|
|
c.put(scalarEvent(value, tag, yAnchorNone))
|
2016-01-04 20:46:33 +00:00
|
|
|
|
2016-09-23 13:42:24 +00:00
|
|
|
proc parseHex[T: int8|int16|int32|int64|uint8|uint16|uint32|uint64](
|
|
|
|
s: YamlStream, val: string): T =
|
2016-09-19 18:51:50 +00:00
|
|
|
result = 0
|
2016-09-23 13:42:24 +00:00
|
|
|
for i in 2..<val.len:
|
|
|
|
case val[i]
|
2016-09-19 18:51:50 +00:00
|
|
|
of '_': discard
|
2016-09-23 13:42:24 +00:00
|
|
|
of '0'..'9': result = result shl 4 or T(ord(val[i]) - ord('0'))
|
|
|
|
of 'a'..'f': result = result shl 4 or T(ord(val[i]) - ord('a') + 10)
|
|
|
|
of 'A'..'F': result = result shl 4 or T(ord(val[i]) - ord('A') + 10)
|
|
|
|
else:
|
|
|
|
raise s.constructionError("Invalid character in hex: " &
|
|
|
|
escape("" & val[i]))
|
|
|
|
|
|
|
|
proc parseOctal[T: int8|int16|int32|int64|uint8|uint16|uint32|uint64](
|
|
|
|
s: YamlStream, val: string): T =
|
|
|
|
for i in 2..<val.len:
|
|
|
|
case val[i]
|
2016-09-19 18:51:50 +00:00
|
|
|
of '_': discard
|
2016-09-23 13:42:24 +00:00
|
|
|
of '0'..'7': result = result shl 3 + T((ord(val[i]) - ord('0')))
|
|
|
|
else:
|
|
|
|
raise s.constructionError("Invalid character in hex: " &
|
|
|
|
escape("" & val[i]))
|
2016-09-19 18:51:50 +00:00
|
|
|
|
2016-01-28 21:29:26 +00:00
|
|
|
proc constructObject*[T: int8|int16|int32|int64](
|
2016-04-02 15:48:22 +00:00
|
|
|
s: var YamlStream, c: ConstructionContext, result: var T)
|
|
|
|
{.raises: [YamlConstructionError, YamlStreamError].} =
|
|
|
|
## constructs an integer value from a YAML scalar
|
|
|
|
constructScalarItem(s, item, T):
|
2018-08-29 13:25:58 +00:00
|
|
|
if item.scalarContent[0] == '0' and item.scalarContent.len > 1 and item.scalarContent[1] in {'x', 'X' }:
|
2016-09-23 13:42:24 +00:00
|
|
|
result = parseHex[T](s, item.scalarContent)
|
2018-08-29 13:25:58 +00:00
|
|
|
elif item.scalarContent[0] == '0' and item.scalarContent.len > 1 and item.scalarContent[1] in {'o', 'O'}:
|
2016-09-23 13:42:24 +00:00
|
|
|
result = parseOctal[T](s, item.scalarContent)
|
2016-09-19 18:51:50 +00:00
|
|
|
else:
|
2018-10-03 18:00:12 +00:00
|
|
|
let nInt = parseBiggestInt(item.scalarContent)
|
|
|
|
if nInt <= T.high:
|
|
|
|
# make sure we don't produce a range error
|
|
|
|
result = T(nInt)
|
|
|
|
else:
|
2018-10-09 16:31:09 +00:00
|
|
|
raise s.constructionError("Cannot construct int; out of range: " &
|
|
|
|
$nInt & " for type " & T.name & " with max of: " & $T.high)
|
2015-12-29 14:09:37 +00:00
|
|
|
|
2016-05-25 21:32:49 +00:00
|
|
|
proc constructObject*(s: var YamlStream, c: ConstructionContext,
|
|
|
|
result: var int)
|
|
|
|
{.raises: [YamlConstructionError, YamlStreamError], inline.} =
|
2016-04-04 18:44:16 +00:00
|
|
|
## constructs an integer of architecture-defined length by loading it into
|
|
|
|
## int32 and then converting it.
|
|
|
|
var i32Result: int32
|
|
|
|
constructObject(s, c, i32Result)
|
|
|
|
result = int(i32Result)
|
2016-01-04 20:46:33 +00:00
|
|
|
|
2016-03-25 20:27:41 +00:00
|
|
|
proc representObject*[T: int8|int16|int32|int64](value: T, ts: TagStyle,
|
2016-09-01 18:56:34 +00:00
|
|
|
c: SerializationContext, tag: TagId) {.raises: [].} =
|
2016-04-02 15:48:22 +00:00
|
|
|
## represents an integer value as YAML scalar
|
2016-09-01 18:56:34 +00:00
|
|
|
c.put(scalarEvent($value, tag, yAnchorNone))
|
2016-01-26 19:00:28 +00:00
|
|
|
|
2016-05-25 21:32:49 +00:00
|
|
|
proc representObject*(value: int, tagStyle: TagStyle,
|
2016-09-01 18:56:34 +00:00
|
|
|
c: SerializationContext, tag: TagId)
|
|
|
|
{.raises: [YamlStreamError], inline.}=
|
2016-04-04 18:44:16 +00:00
|
|
|
## represent an integer of architecture-defined length by casting it to int32.
|
2020-11-03 21:08:21 +00:00
|
|
|
## on 64-bit systems, this may cause a RangeDefect.
|
2016-07-08 09:35:31 +00:00
|
|
|
|
2016-04-04 18:44:16 +00:00
|
|
|
# currently, sizeof(int) is at least sizeof(int32).
|
2016-09-01 18:56:34 +00:00
|
|
|
try: c.put(scalarEvent($int32(value), tag, yAnchorNone))
|
2020-11-03 21:08:21 +00:00
|
|
|
except RangeDefect:
|
2016-09-01 18:56:34 +00:00
|
|
|
var e = newException(YamlStreamError, getCurrentExceptionMsg())
|
|
|
|
e.parent = getCurrentException()
|
|
|
|
raise e
|
2016-01-26 19:00:28 +00:00
|
|
|
|
2017-02-13 16:10:56 +00:00
|
|
|
when defined(JS):
|
|
|
|
type DefiniteUIntTypes = uint8 | uint16 | uint32
|
|
|
|
else:
|
|
|
|
type DefiniteUIntTypes = uint8 | uint16 | uint32 | uint64
|
|
|
|
|
|
|
|
proc constructObject*[T: DefiniteUIntTypes](
|
2016-04-02 15:48:22 +00:00
|
|
|
s: var YamlStream, c: ConstructionContext, result: var T)
|
|
|
|
{.raises: [YamlConstructionError, YamlStreamError].} =
|
|
|
|
## construct an unsigned integer value from a YAML scalar
|
|
|
|
constructScalarItem(s, item, T):
|
2016-09-20 19:53:38 +00:00
|
|
|
if item.scalarContent[0] == '0' and item.scalarContent[1] in {'x', 'X'}:
|
2016-09-23 13:42:24 +00:00
|
|
|
result = parseHex[T](s, item.scalarContent)
|
2016-09-20 19:53:38 +00:00
|
|
|
elif item.scalarContent[0] == '0' and item.scalarContent[1] in {'o', 'O'}:
|
2016-09-23 13:42:24 +00:00
|
|
|
result = parseOctal[T](s, item.scalarContent)
|
2016-09-20 19:53:38 +00:00
|
|
|
else: result = T(parseBiggestUInt(item.scalarContent))
|
2016-01-26 19:00:28 +00:00
|
|
|
|
2016-05-25 21:32:49 +00:00
|
|
|
proc constructObject*(s: var YamlStream, c: ConstructionContext,
|
|
|
|
result: var uint)
|
|
|
|
{.raises: [YamlConstructionError, YamlStreamError], inline.} =
|
2016-04-04 18:44:16 +00:00
|
|
|
## represent an unsigned integer of architecture-defined length by loading it
|
|
|
|
## into uint32 and then converting it.
|
|
|
|
var u32Result: uint32
|
|
|
|
constructObject(s, c, u32Result)
|
|
|
|
result= uint(u32Result)
|
2016-01-24 19:38:30 +00:00
|
|
|
|
2016-09-01 19:08:27 +00:00
|
|
|
when defined(JS):
|
|
|
|
# TODO: this is a dirty hack and may lead to overflows!
|
|
|
|
proc `$`(x: uint8|uint16|uint32|uint64|uint): string =
|
|
|
|
result = $BiggestInt(x)
|
|
|
|
|
2016-03-25 20:27:41 +00:00
|
|
|
proc representObject*[T: uint8|uint16|uint32|uint64](value: T, ts: TagStyle,
|
2016-09-01 18:56:34 +00:00
|
|
|
c: SerializationContext, tag: TagId) {.raises: [].} =
|
2016-04-02 15:48:22 +00:00
|
|
|
## represents an unsigned integer value as YAML scalar
|
2016-09-01 18:56:34 +00:00
|
|
|
c.put(scalarEvent($value, tag, yAnchorNone))
|
2016-01-04 20:46:33 +00:00
|
|
|
|
2016-05-25 21:32:49 +00:00
|
|
|
proc representObject*(value: uint, ts: TagStyle, c: SerializationContext,
|
2016-09-01 18:56:34 +00:00
|
|
|
tag: TagId) {.raises: [YamlStreamError], inline.} =
|
2016-04-04 18:44:16 +00:00
|
|
|
## represent an unsigned integer of architecture-defined length by casting it
|
2020-11-03 21:08:21 +00:00
|
|
|
## to int32. on 64-bit systems, this may cause a RangeDefect.
|
2016-09-01 18:56:34 +00:00
|
|
|
try: c.put(scalarEvent($uint32(value), tag, yAnchorNone))
|
2020-11-03 21:08:21 +00:00
|
|
|
except RangeDefect:
|
2016-09-01 18:56:34 +00:00
|
|
|
var e = newException(YamlStreamError, getCurrentExceptionMsg())
|
|
|
|
e.parent = getCurrentException()
|
|
|
|
raise e
|
2015-12-29 15:10:47 +00:00
|
|
|
|
2016-04-04 18:44:16 +00:00
|
|
|
proc constructObject*[T: float|float32|float64](
|
2016-04-02 15:48:22 +00:00
|
|
|
s: var YamlStream, c: ConstructionContext, result: var T)
|
|
|
|
{.raises: [YamlConstructionError, YamlStreamError].} =
|
|
|
|
## construct a float value from a YAML scalar
|
|
|
|
constructScalarItem(s, item, T):
|
|
|
|
let hint = guessType(item.scalarContent)
|
|
|
|
case hint
|
2016-06-26 10:41:06 +00:00
|
|
|
of yTypeFloat:
|
|
|
|
discard parseBiggestFloat(item.scalarContent, result)
|
2016-07-08 09:35:31 +00:00
|
|
|
of yTypeInteger:
|
|
|
|
discard parseBiggestFloat(item.scalarContent, result)
|
2016-04-02 15:48:22 +00:00
|
|
|
of yTypeFloatInf:
|
|
|
|
if item.scalarContent[0] == '-': result = NegInf
|
|
|
|
else: result = Inf
|
|
|
|
of yTypeFloatNaN: result = NaN
|
|
|
|
else:
|
2016-09-23 13:42:24 +00:00
|
|
|
raise s.constructionError("Cannot construct to float: " &
|
|
|
|
escape(item.scalarContent))
|
2015-12-29 14:09:37 +00:00
|
|
|
|
2016-04-04 18:44:16 +00:00
|
|
|
proc representObject*[T: float|float32|float64](value: T, ts: TagStyle,
|
2016-09-01 18:56:34 +00:00
|
|
|
c: SerializationContext, tag: TagId) {.raises: [].} =
|
2016-04-02 15:48:22 +00:00
|
|
|
## represents a float value as YAML scalar
|
2016-09-01 18:56:34 +00:00
|
|
|
case value
|
|
|
|
of Inf: c.put(scalarEvent(".inf", tag))
|
|
|
|
of NegInf: c.put(scalarEvent("-.inf", tag))
|
|
|
|
of NaN: c.put(scalarEvent(".nan", tag))
|
|
|
|
else: c.put(scalarEvent($value, tag))
|
2016-01-26 19:00:28 +00:00
|
|
|
|
2016-01-24 19:38:30 +00:00
|
|
|
proc yamlTag*(T: typedesc[bool]): TagId {.inline, raises: [].} = yTagBoolean
|
2016-01-04 20:46:33 +00:00
|
|
|
|
2016-02-12 18:53:25 +00:00
|
|
|
proc constructObject*(s: var YamlStream, c: ConstructionContext,
|
|
|
|
result: var bool)
|
2016-04-02 15:48:22 +00:00
|
|
|
{.raises: [YamlConstructionError, YamlStreamError].} =
|
|
|
|
## constructs a bool value from a YAML scalar
|
|
|
|
constructScalarItem(s, item, bool):
|
|
|
|
case guessType(item.scalarContent)
|
|
|
|
of yTypeBoolTrue: result = true
|
|
|
|
of yTypeBoolFalse: result = false
|
|
|
|
else:
|
2016-09-23 13:42:24 +00:00
|
|
|
raise s.constructionError("Cannot construct to bool: " &
|
|
|
|
escape(item.scalarContent))
|
2016-07-08 09:35:31 +00:00
|
|
|
|
2016-03-25 20:27:41 +00:00
|
|
|
proc representObject*(value: bool, ts: TagStyle, c: SerializationContext,
|
2016-09-01 18:56:34 +00:00
|
|
|
tag: TagId) {.raises: [].} =
|
2016-04-02 15:48:22 +00:00
|
|
|
## represents a bool value as a YAML scalar
|
2016-09-01 18:56:34 +00:00
|
|
|
c.put(scalarEvent(if value: "y" else: "n", tag, yAnchorNone))
|
2016-01-04 20:46:33 +00:00
|
|
|
|
2016-02-12 18:53:25 +00:00
|
|
|
proc constructObject*(s: var YamlStream, c: ConstructionContext,
|
|
|
|
result: var char)
|
2016-04-02 15:48:22 +00:00
|
|
|
{.raises: [YamlConstructionError, YamlStreamError].} =
|
|
|
|
## constructs a char value from a YAML scalar
|
|
|
|
constructScalarItem(s, item, char):
|
|
|
|
if item.scalarContent.len != 1:
|
2016-09-23 13:42:24 +00:00
|
|
|
raise s.constructionError("Cannot construct to char (length != 1): " &
|
|
|
|
escape(item.scalarContent))
|
2016-04-02 15:48:22 +00:00
|
|
|
else: result = item.scalarContent[0]
|
2016-01-26 19:00:28 +00:00
|
|
|
|
2016-03-25 20:27:41 +00:00
|
|
|
proc representObject*(value: char, ts: TagStyle, c: SerializationContext,
|
2016-09-01 18:56:34 +00:00
|
|
|
tag: TagId) {.raises: [].} =
|
2016-04-02 15:48:22 +00:00
|
|
|
## represents a char value as YAML scalar
|
2016-09-01 18:56:34 +00:00
|
|
|
c.put(scalarEvent("" & value, tag, yAnchorNone))
|
2016-01-26 19:00:28 +00:00
|
|
|
|
2016-11-08 20:13:01 +00:00
|
|
|
proc yamlTag*(T: typedesc[Time]): TagId {.inline, raises: [].} = yTagTimestamp
|
|
|
|
|
|
|
|
proc constructObject*(s: var YamlStream, c: ConstructionContext,
|
|
|
|
result: var Time)
|
|
|
|
{.raises: [YamlConstructionError, YamlStreamError].} =
|
|
|
|
constructScalarItem(s, item, Time):
|
|
|
|
if guessType(item.scalarContent) == yTypeTimestamp:
|
|
|
|
var
|
|
|
|
tmp = newStringOfCap(60)
|
|
|
|
pos = 8
|
|
|
|
c: char
|
|
|
|
while pos < item.scalarContent.len():
|
|
|
|
c = item.scalarContent[pos]
|
|
|
|
if c in {' ', '\t', 'T', 't'}: break
|
|
|
|
inc(pos)
|
|
|
|
if pos == item.scalarContent.len():
|
|
|
|
tmp.add(item.scalarContent)
|
|
|
|
tmp.add("T00:00:00+00:00")
|
|
|
|
else:
|
|
|
|
tmp.add(item.scalarContent[0 .. pos - 1])
|
|
|
|
if c in {' ', '\t'}:
|
|
|
|
while true:
|
|
|
|
inc(pos)
|
|
|
|
c = item.scalarContent[pos]
|
|
|
|
if c notin {' ', '\t'}: break
|
|
|
|
else: inc(pos)
|
|
|
|
tmp.add("T")
|
|
|
|
let timeStart = pos
|
|
|
|
inc(pos, 7)
|
|
|
|
var fractionStart = -1
|
|
|
|
while pos < item.scalarContent.len():
|
|
|
|
c = item.scalarContent[pos]
|
|
|
|
if c in {'+', '-', 'Z', ' ', '\t'}: break
|
|
|
|
elif c == '.': fractionStart = pos
|
|
|
|
inc(pos)
|
|
|
|
if fractionStart == -1:
|
|
|
|
tmp.add(item.scalarContent[timeStart .. pos - 1])
|
|
|
|
else:
|
|
|
|
tmp.add(item.scalarContent[timeStart .. fractionStart - 1])
|
|
|
|
if c in {'Z', ' ', '\t'}: tmp.add("+00:00")
|
|
|
|
else:
|
|
|
|
tmp.add(c)
|
|
|
|
inc(pos)
|
|
|
|
let tzStart = pos
|
|
|
|
inc(pos)
|
|
|
|
if pos < item.scalarContent.len() and item.scalarContent[pos] != ':':
|
|
|
|
inc(pos)
|
|
|
|
if pos - tzStart == 1: tmp.add('0')
|
|
|
|
tmp.add(item.scalarContent[tzStart .. pos - 1])
|
|
|
|
if pos == item.scalarContent.len(): tmp.add(":00")
|
|
|
|
elif pos + 2 == item.scalarContent.len():
|
|
|
|
tmp.add(":0")
|
|
|
|
tmp.add(item.scalarContent[pos + 1])
|
|
|
|
else:
|
|
|
|
tmp.add(item.scalarContent[pos .. pos + 2])
|
2018-10-03 17:43:27 +00:00
|
|
|
let info = tmp.parse("yyyy-M-d'T'H:mm:sszzz")
|
2016-11-08 20:13:01 +00:00
|
|
|
result = info.toTime()
|
|
|
|
else:
|
|
|
|
raise s.constructionError("Not a parsable timestamp: " &
|
|
|
|
escape(item.scalarContent))
|
|
|
|
|
|
|
|
proc representObject*(value: Time, ts: TagStyle, c: SerializationContext,
|
|
|
|
tag: TagId) {.raises: [ValueError].} =
|
2020-07-22 09:30:19 +00:00
|
|
|
let tmp = value.utc()
|
2016-11-08 20:13:01 +00:00
|
|
|
c.put(scalarEvent(tmp.format("yyyy-MM-dd'T'HH:mm:ss'Z'")))
|
|
|
|
|
2016-01-24 19:38:30 +00:00
|
|
|
proc yamlTag*[I](T: typedesc[seq[I]]): TagId {.inline, raises: [].} =
|
2016-10-10 18:16:54 +00:00
|
|
|
let uri = nimTag("system:seq(" & safeTagUri(yamlTag(I)) & ')')
|
2016-04-02 16:29:26 +00:00
|
|
|
result = lazyLoadTag(uri)
|
|
|
|
|
|
|
|
proc yamlTag*[I](T: typedesc[set[I]]): TagId {.inline, raises: [].} =
|
2016-10-10 18:16:54 +00:00
|
|
|
let uri = nimTag("system:set(" & safeTagUri(yamlTag(I)) & ')')
|
2016-04-02 15:48:22 +00:00
|
|
|
result = lazyLoadTag(uri)
|
2015-12-29 15:10:47 +00:00
|
|
|
|
2016-02-12 18:53:25 +00:00
|
|
|
proc constructObject*[T](s: var YamlStream, c: ConstructionContext,
|
2016-01-28 21:29:26 +00:00
|
|
|
result: var seq[T])
|
2016-04-02 15:48:22 +00:00
|
|
|
{.raises: [YamlConstructionError, YamlStreamError].} =
|
|
|
|
## constructs a Nim seq from a YAML sequence
|
|
|
|
let event = s.next()
|
|
|
|
if event.kind != yamlStartSeq:
|
2016-09-23 13:42:24 +00:00
|
|
|
raise s.constructionError("Expected sequence start")
|
2016-04-02 15:48:22 +00:00
|
|
|
result = newSeq[T]()
|
|
|
|
while s.peek().kind != yamlEndSeq:
|
|
|
|
var item: T
|
|
|
|
constructChild(s, c, item)
|
|
|
|
result.add(item)
|
|
|
|
discard s.next()
|
2015-12-29 14:09:37 +00:00
|
|
|
|
2016-04-02 16:29:26 +00:00
|
|
|
proc constructObject*[T](s: var YamlStream, c: ConstructionContext,
|
|
|
|
result: var set[T])
|
|
|
|
{.raises: [YamlConstructionError, YamlStreamError].} =
|
|
|
|
## constructs a Nim seq from a YAML sequence
|
|
|
|
let event = s.next()
|
|
|
|
if event.kind != yamlStartSeq:
|
2016-09-23 13:42:24 +00:00
|
|
|
raise s.constructionError("Expected sequence start")
|
2016-04-02 16:29:26 +00:00
|
|
|
result = {}
|
|
|
|
while s.peek().kind != yamlEndSeq:
|
|
|
|
var item: T
|
|
|
|
constructChild(s, c, item)
|
|
|
|
result.incl(item)
|
|
|
|
discard s.next()
|
|
|
|
|
|
|
|
proc representObject*[T](value: seq[T]|set[T], ts: TagStyle,
|
2016-09-22 12:25:10 +00:00
|
|
|
c: SerializationContext, tag: TagId) =
|
2016-04-02 15:48:22 +00:00
|
|
|
## represents a Nim seq as YAML sequence
|
2016-09-01 18:56:34 +00:00
|
|
|
let childTagStyle = if ts == tsRootOnly: tsNone else: ts
|
|
|
|
c.put(startSeqEvent(tag))
|
|
|
|
for item in value:
|
|
|
|
representChild(item, childTagStyle, c)
|
|
|
|
c.put(endSeqEvent())
|
2015-12-29 15:10:47 +00:00
|
|
|
|
2016-04-03 09:29:49 +00:00
|
|
|
proc yamlTag*[I, V](T: typedesc[array[I, V]]): TagId {.inline, raises: [].} =
|
|
|
|
const rangeName = name(I)
|
2016-10-10 18:16:54 +00:00
|
|
|
let uri = nimTag("system:array(" & rangeName[6..rangeName.high()] & ';' &
|
|
|
|
safeTagUri(yamlTag(V)) & ')')
|
2016-04-03 09:29:49 +00:00
|
|
|
result = lazyLoadTag(uri)
|
|
|
|
|
|
|
|
proc constructObject*[I, T](s: var YamlStream, c: ConstructionContext,
|
|
|
|
result: var array[I, T])
|
|
|
|
{.raises: [YamlConstructionError, YamlStreamError].} =
|
|
|
|
## constructs a Nim array from a YAML sequence
|
|
|
|
var event = s.next()
|
|
|
|
if event.kind != yamlStartSeq:
|
2016-09-23 13:42:24 +00:00
|
|
|
raise s.constructionError("Expected sequence start")
|
2016-04-03 09:29:49 +00:00
|
|
|
for index in low(I)..high(I):
|
|
|
|
event = s.peek()
|
|
|
|
if event.kind == yamlEndSeq:
|
2016-09-23 13:42:24 +00:00
|
|
|
raise s.constructionError("Too few array values")
|
2016-04-03 09:29:49 +00:00
|
|
|
constructChild(s, c, result[index])
|
|
|
|
event = s.next()
|
|
|
|
if event.kind != yamlEndSeq:
|
2016-09-23 13:42:24 +00:00
|
|
|
raise s.constructionError("Too many array values")
|
2016-04-03 09:29:49 +00:00
|
|
|
|
|
|
|
proc representObject*[I, T](value: array[I, T], ts: TagStyle,
|
2016-09-22 12:25:10 +00:00
|
|
|
c: SerializationContext, tag: TagId) =
|
2016-04-03 09:29:49 +00:00
|
|
|
## represents a Nim array as YAML sequence
|
2016-09-01 18:56:34 +00:00
|
|
|
let childTagStyle = if ts == tsRootOnly: tsNone else: ts
|
|
|
|
c.put(startSeqEvent(tag))
|
|
|
|
for item in value:
|
|
|
|
representChild(item, childTagStyle, c)
|
|
|
|
c.put(endSeqEvent())
|
2016-07-08 09:35:31 +00:00
|
|
|
|
2016-01-24 19:38:30 +00:00
|
|
|
proc yamlTag*[K, V](T: typedesc[Table[K, V]]): TagId {.inline, raises: [].} =
|
2016-04-02 15:48:22 +00:00
|
|
|
try:
|
2016-10-10 18:16:54 +00:00
|
|
|
let uri = nimTag("tables:Table(" & safeTagUri(yamlTag(K)) & ';' &
|
|
|
|
safeTagUri(yamlTag(V)) & ")")
|
2016-04-02 15:48:22 +00:00
|
|
|
result = lazyLoadTag(uri)
|
|
|
|
except KeyError:
|
|
|
|
# cannot happen (theoretically, you know)
|
2016-08-09 18:47:22 +00:00
|
|
|
internalError("Unexpected KeyError")
|
2016-01-04 20:46:33 +00:00
|
|
|
|
2016-02-12 18:53:25 +00:00
|
|
|
proc constructObject*[K, V](s: var YamlStream, c: ConstructionContext,
|
2016-01-28 21:29:26 +00:00
|
|
|
result: var Table[K, V])
|
2016-04-02 15:48:22 +00:00
|
|
|
{.raises: [YamlConstructionError, YamlStreamError].} =
|
|
|
|
## constructs a Nim Table from a YAML mapping
|
|
|
|
let event = s.next()
|
|
|
|
if event.kind != yamlStartMap:
|
2016-09-23 13:42:24 +00:00
|
|
|
raise s.constructionError("Expected map start, got " & $event.kind)
|
2016-04-02 15:48:22 +00:00
|
|
|
result = initTable[K, V]()
|
|
|
|
while s.peek.kind != yamlEndMap:
|
|
|
|
var
|
|
|
|
key: K
|
|
|
|
value: V
|
|
|
|
constructChild(s, c, key)
|
|
|
|
constructChild(s, c, value)
|
|
|
|
if result.contains(key):
|
2016-09-23 13:42:24 +00:00
|
|
|
raise s.constructionError("Duplicate table key!")
|
2016-04-02 15:48:22 +00:00
|
|
|
result[key] = value
|
|
|
|
discard s.next()
|
2015-12-29 15:10:47 +00:00
|
|
|
|
2016-02-02 17:19:40 +00:00
|
|
|
proc representObject*[K, V](value: Table[K, V], ts: TagStyle,
|
2016-09-22 12:25:10 +00:00
|
|
|
c: SerializationContext, tag: TagId) =
|
2016-04-02 15:48:22 +00:00
|
|
|
## represents a Nim Table as YAML mapping
|
2016-09-01 18:56:34 +00:00
|
|
|
let childTagStyle = if ts == tsRootOnly: tsNone else: ts
|
|
|
|
c.put(startMapEvent(tag))
|
|
|
|
for key, value in value.pairs:
|
|
|
|
representChild(key, childTagStyle, c)
|
|
|
|
representChild(value, childTagStyle, c)
|
|
|
|
c.put(endMapEvent())
|
2016-03-25 21:22:42 +00:00
|
|
|
|
|
|
|
proc yamlTag*[K, V](T: typedesc[OrderedTable[K, V]]): TagId
|
2016-04-02 15:48:22 +00:00
|
|
|
{.inline, raises: [].} =
|
|
|
|
try:
|
2016-10-10 18:16:54 +00:00
|
|
|
let uri = nimTag("tables:OrderedTable(" & safeTagUri(yamlTag(K)) & ';' &
|
|
|
|
safeTagUri(yamlTag(V)) & ")")
|
2016-04-02 15:48:22 +00:00
|
|
|
result = lazyLoadTag(uri)
|
|
|
|
except KeyError:
|
|
|
|
# cannot happen (theoretically, you know)
|
2016-08-09 18:47:22 +00:00
|
|
|
internalError("Unexpected KeyError")
|
2016-03-25 21:22:42 +00:00
|
|
|
|
|
|
|
proc constructObject*[K, V](s: var YamlStream, c: ConstructionContext,
|
|
|
|
result: var OrderedTable[K, V])
|
2016-04-02 15:48:22 +00:00
|
|
|
{.raises: [YamlConstructionError, YamlStreamError].} =
|
|
|
|
## constructs a Nim OrderedTable from a YAML mapping
|
2016-09-23 13:42:24 +00:00
|
|
|
var event = s.next()
|
2016-04-02 15:48:22 +00:00
|
|
|
if event.kind != yamlStartSeq:
|
2016-09-23 13:42:24 +00:00
|
|
|
raise s.constructionError("Expected seq start, got " & $event.kind)
|
2016-04-02 15:48:22 +00:00
|
|
|
result = initOrderedTable[K, V]()
|
|
|
|
while s.peek.kind != yamlEndSeq:
|
|
|
|
var
|
|
|
|
key: K
|
|
|
|
value: V
|
2016-09-23 13:42:24 +00:00
|
|
|
event = s.next()
|
|
|
|
if event.kind != yamlStartMap:
|
|
|
|
raise s.constructionError("Expected map start, got " & $event.kind)
|
2016-04-02 15:48:22 +00:00
|
|
|
constructChild(s, c, key)
|
|
|
|
constructChild(s, c, value)
|
2016-09-23 13:42:24 +00:00
|
|
|
event = s.next()
|
|
|
|
if event.kind != yamlEndMap:
|
|
|
|
raise s.constructionError("Expected map end, got " & $event.kind)
|
2016-04-02 15:48:22 +00:00
|
|
|
if result.contains(key):
|
2016-09-23 13:42:24 +00:00
|
|
|
raise s.constructionError("Duplicate table key!")
|
2016-04-02 15:48:22 +00:00
|
|
|
result.add(key, value)
|
|
|
|
discard s.next()
|
2016-03-25 21:22:42 +00:00
|
|
|
|
|
|
|
proc representObject*[K, V](value: OrderedTable[K, V], ts: TagStyle,
|
2016-09-22 12:25:10 +00:00
|
|
|
c: SerializationContext, tag: TagId) =
|
2016-09-01 18:56:34 +00:00
|
|
|
let childTagStyle = if ts == tsRootOnly: tsNone else: ts
|
|
|
|
c.put(startSeqEvent(tag))
|
|
|
|
for key, value in value.pairs:
|
|
|
|
c.put(startMapEvent())
|
|
|
|
representChild(key, childTagStyle, c)
|
|
|
|
representChild(value, childTagStyle, c)
|
|
|
|
c.put(endMapEvent())
|
|
|
|
c.put(endSeqEvent())
|
2015-12-29 17:22:55 +00:00
|
|
|
|
2016-05-25 21:32:49 +00:00
|
|
|
proc yamlTag*(T: typedesc[object|enum]):
|
2016-06-05 13:43:39 +00:00
|
|
|
TagId {.inline, raises: [].} =
|
2016-10-10 18:16:54 +00:00
|
|
|
var uri = nimTag("custom:" & (typetraits.name(type(T))))
|
2016-06-05 13:43:39 +00:00
|
|
|
try: serializationTagLibrary.tags[uri]
|
|
|
|
except KeyError: serializationTagLibrary.registerUri(uri)
|
2016-02-01 18:48:42 +00:00
|
|
|
|
2016-05-25 21:32:49 +00:00
|
|
|
proc yamlTag*(T: typedesc[tuple]):
|
2016-06-05 13:43:39 +00:00
|
|
|
TagId {.inline, raises: [].} =
|
2016-04-02 15:48:22 +00:00
|
|
|
var
|
|
|
|
i: T
|
2016-10-10 18:16:54 +00:00
|
|
|
uri = nimTag("tuple(")
|
2016-04-02 15:48:22 +00:00
|
|
|
first = true
|
|
|
|
for name, value in fieldPairs(i):
|
|
|
|
if first: first = false
|
|
|
|
else: uri.add(",")
|
|
|
|
uri.add(safeTagUri(yamlTag(type(value))))
|
|
|
|
uri.add(")")
|
|
|
|
try: serializationTagLibrary.tags[uri]
|
|
|
|
except KeyError: serializationTagLibrary.registerUri(uri)
|
|
|
|
|
2016-11-30 17:01:03 +00:00
|
|
|
iterator recListItems(n: NimNode): NimNode =
|
|
|
|
if n.kind == nnkRecList:
|
|
|
|
for item in n.children: yield item
|
|
|
|
else: yield n
|
|
|
|
|
|
|
|
proc recListLen(n: NimNode): int {.compileTime.} =
|
|
|
|
if n.kind == nnkRecList: result = n.len
|
|
|
|
else: result = 1
|
|
|
|
|
|
|
|
proc recListNode(n: NimNode): NimNode {.compileTime.} =
|
|
|
|
if n.kind == nnkRecList: result = n[0]
|
|
|
|
else: result = n
|
|
|
|
|
2019-06-11 19:30:14 +00:00
|
|
|
proc fieldCount(t: NimNode): int {.compiletime.} =
|
|
|
|
result = 0
|
|
|
|
let tDesc = getType(getType(t)[1])
|
|
|
|
if tDesc.kind == nnkBracketExpr:
|
|
|
|
# tuple
|
|
|
|
result = tDesc.len - 1
|
|
|
|
else:
|
|
|
|
# object
|
|
|
|
for child in tDesc[2].children:
|
|
|
|
inc(result)
|
|
|
|
if child.kind == nnkRecCase:
|
|
|
|
for bIndex in 1..<len(child):
|
|
|
|
var increment = 0
|
|
|
|
case child[bIndex].kind
|
|
|
|
of nnkOfBranch:
|
2020-06-26 19:29:41 +00:00
|
|
|
let content = child[bIndex][len(child[bIndex])-1]
|
2019-06-11 19:30:14 +00:00
|
|
|
# We cannot assume that child[bIndex][1] is a RecList due to
|
|
|
|
# a one-liner like 'of akDog: barkometer' not resulting in a
|
|
|
|
# RecList but in an Ident node.
|
2020-06-26 19:29:41 +00:00
|
|
|
case content.kind
|
2019-06-11 19:30:14 +00:00
|
|
|
of nnkRecList:
|
2020-06-26 19:29:41 +00:00
|
|
|
increment = len(content)
|
2019-06-11 19:30:14 +00:00
|
|
|
else:
|
|
|
|
increment = 1
|
|
|
|
of nnkElse:
|
|
|
|
# Same goes for the else branch.
|
|
|
|
case child[bIndex][0].kind
|
|
|
|
of nnkRecList:
|
|
|
|
increment = len(child[bIndex][0])
|
|
|
|
else:
|
|
|
|
increment = 1
|
|
|
|
else:
|
|
|
|
internalError("Unexpected child kind: " & $child[bIndex].kind)
|
|
|
|
inc(result, increment)
|
|
|
|
|
2016-09-21 13:40:03 +00:00
|
|
|
|
|
|
|
macro matchMatrix(t: typedesc): untyped =
|
|
|
|
result = newNimNode(nnkBracket)
|
2016-09-22 12:16:10 +00:00
|
|
|
let numFields = fieldCount(t)
|
|
|
|
for i in 0..<numFields:
|
|
|
|
result.add(newLit(false))
|
2016-09-21 13:40:03 +00:00
|
|
|
|
2016-09-24 14:45:49 +00:00
|
|
|
proc checkDuplicate(s: NimNode, tName: string, name: string, i: int,
|
2016-09-23 13:42:24 +00:00
|
|
|
matched: NimNode): NimNode {.compileTime.} =
|
2016-09-21 13:40:03 +00:00
|
|
|
result = newIfStmt((newNimNode(nnkBracketExpr).add(matched, newLit(i)),
|
2016-09-23 13:42:24 +00:00
|
|
|
newNimNode(nnkRaiseStmt).add(newCall(bindSym("constructionError"), s,
|
2016-09-24 14:45:49 +00:00
|
|
|
newLit("While constructing " & tName & ": Duplicate field: " &
|
|
|
|
escape(name))))))
|
2016-09-21 13:40:03 +00:00
|
|
|
|
2016-10-26 16:32:54 +00:00
|
|
|
proc addDefaultOr(tName: string, i: int, o: NimNode,
|
2020-06-26 19:29:41 +00:00
|
|
|
field, elseBranch: NimNode): NimNode {.compileTime.} =
|
2016-10-26 16:32:54 +00:00
|
|
|
result = quote do:
|
2020-06-26 19:29:41 +00:00
|
|
|
when `o`.`field`.hasCustomPragma(defaultVal):
|
|
|
|
`o`.`field` = `o`.`field`.getCustomPragmaVal(defaultVal)
|
2016-10-26 16:32:54 +00:00
|
|
|
else: `elseBranch`
|
|
|
|
|
2020-06-26 21:21:22 +00:00
|
|
|
proc hasSparse(t: typedesc): bool {.compileTime.} =
|
|
|
|
when compiles(t.hasCustomPragma(sparse)):
|
|
|
|
return t.hasCustomPragma(sparse)
|
|
|
|
else:
|
|
|
|
return false
|
|
|
|
|
|
|
|
proc getOptionInner(fType: NimNode): NimNode {.compileTime.} =
|
|
|
|
if fType.kind == nnkBracketExpr and len(fType) == 2 and
|
|
|
|
fType[1].kind == nnkSym:
|
|
|
|
return newIdentNode($fType[1])
|
|
|
|
else: return nil
|
|
|
|
|
2019-06-11 19:30:14 +00:00
|
|
|
proc checkMissing(s: NimNode, t: NimNode, tName: string, field: NimNode,
|
2020-06-26 19:29:41 +00:00
|
|
|
i: int, matched, o: NimNode):
|
2016-10-26 16:32:54 +00:00
|
|
|
NimNode {.compileTime.} =
|
2020-06-26 21:21:22 +00:00
|
|
|
let
|
|
|
|
fType = getTypeInst(field)
|
|
|
|
fName = escape($field)
|
|
|
|
optionInner = getOptionInner(fType)
|
2020-06-26 19:29:41 +00:00
|
|
|
result = quote do:
|
|
|
|
when not `o`.`field`.hasCustomPragma(transient):
|
|
|
|
if not `matched`[`i`]:
|
|
|
|
when `o`.`field`.hasCustomPragma(defaultVal):
|
|
|
|
`o`.`field` = `o`.`field`.getCustomPragmaVal(defaultVal)
|
2020-06-26 21:21:22 +00:00
|
|
|
elif hasSparse(`t`) and `o`.`field` is Option:
|
|
|
|
`o`.`field` = none(`optionInner`)
|
2020-06-26 19:29:41 +00:00
|
|
|
else:
|
|
|
|
raise constructionError(`s`, "While constructing " & `tName` &
|
|
|
|
": Missing field: " & `fName`)
|
2016-10-26 16:32:54 +00:00
|
|
|
|
|
|
|
proc markAsFound(i: int, matched: NimNode): NimNode {.compileTime.} =
|
|
|
|
newAssignment(newNimNode(nnkBracketExpr).add(matched, newLit(i)),
|
|
|
|
newLit(true))
|
2016-10-19 20:04:46 +00:00
|
|
|
|
2020-06-26 19:29:41 +00:00
|
|
|
proc ifNotTransient(o, field: NimNode,
|
|
|
|
content: openarray[NimNode],
|
|
|
|
elseError: bool, s: NimNode, tName, fName: string = ""):
|
2016-10-19 20:04:46 +00:00
|
|
|
NimNode {.compileTime.} =
|
|
|
|
var stmts = newStmtList(content)
|
|
|
|
if elseError:
|
2020-06-26 19:29:41 +00:00
|
|
|
result = quote do:
|
|
|
|
when `o`.`field`.hasCustomPragma(transient):
|
|
|
|
raise constructionError(`s`, "While constructing " & `tName` &
|
|
|
|
": Field \"" & `fName` & "\" is transient and may not occur in input")
|
|
|
|
else:
|
|
|
|
`stmts`
|
|
|
|
else:
|
|
|
|
result = quote do:
|
|
|
|
when not `o`.`field`.hasCustomPragma(transient):
|
|
|
|
`stmts`
|
2016-10-19 20:04:46 +00:00
|
|
|
|
2020-06-26 19:29:41 +00:00
|
|
|
macro ensureAllFieldsPresent(s: YamlStream, t: typedesc, o: typed,
|
2020-03-08 18:58:02 +00:00
|
|
|
matched: typed) =
|
2020-06-26 19:29:41 +00:00
|
|
|
result = newStmtList()
|
2016-09-21 13:40:03 +00:00
|
|
|
let
|
2016-09-24 14:45:49 +00:00
|
|
|
tDecl = getType(t)
|
|
|
|
tName = $tDecl[1]
|
|
|
|
tDesc = getType(tDecl[1])
|
2016-09-22 12:16:10 +00:00
|
|
|
var field = 0
|
2016-09-21 13:40:03 +00:00
|
|
|
for child in tDesc[2].children:
|
|
|
|
if child.kind == nnkRecCase:
|
2020-06-26 21:21:22 +00:00
|
|
|
result.add(checkMissing(
|
|
|
|
s, t, tName, child[0], field, matched, o))
|
2016-09-21 13:40:03 +00:00
|
|
|
for bIndex in 1 .. len(child) - 1:
|
|
|
|
let discChecks = newStmtList()
|
2016-10-19 20:04:46 +00:00
|
|
|
var
|
|
|
|
curValues = newNimNode(nnkCurly)
|
|
|
|
recListIndex = 0
|
|
|
|
case child[bIndex].kind
|
|
|
|
of nnkOfBranch:
|
2016-11-30 17:01:03 +00:00
|
|
|
while recListIndex < child[bIndex].len - 1:
|
|
|
|
expectKind(child[bIndex][recListIndex], nnkIntLit)
|
2016-10-19 20:04:46 +00:00
|
|
|
curValues.add(child[bIndex][recListIndex])
|
|
|
|
inc(recListIndex)
|
|
|
|
of nnkElse: discard
|
|
|
|
else: internalError("Unexpected child kind: " & $child[bIndex].kind)
|
2016-11-30 17:01:03 +00:00
|
|
|
for item in child[bIndex][recListIndex].recListItems:
|
2016-09-22 12:16:10 +00:00
|
|
|
inc(field)
|
2020-06-26 21:21:22 +00:00
|
|
|
discChecks.add(checkMissing(
|
|
|
|
s, t, tName, item, field, matched, o))
|
2020-06-26 19:29:41 +00:00
|
|
|
result.add(newIfStmt((infix(newDotExpr(o, newIdentNode($child[0])),
|
|
|
|
"in", curValues), discChecks)))
|
2016-09-21 13:40:03 +00:00
|
|
|
else:
|
2020-06-26 19:29:41 +00:00
|
|
|
result.add(checkMissing(s, t, tName, child, field, matched, o))
|
2016-09-22 12:16:10 +00:00
|
|
|
inc(field)
|
2016-09-21 13:40:03 +00:00
|
|
|
|
2020-06-26 19:29:41 +00:00
|
|
|
macro constructFieldValue(t: typedesc, stream: untyped,
|
2016-10-19 20:04:46 +00:00
|
|
|
context: untyped, name: untyped, o: untyped,
|
2020-03-08 18:58:02 +00:00
|
|
|
matched: untyped, failOnUnknown: bool) =
|
2016-09-24 14:45:49 +00:00
|
|
|
let
|
|
|
|
tDecl = getType(t)
|
|
|
|
tName = $tDecl[1]
|
|
|
|
tDesc = getType(tDecl[1])
|
2016-10-19 20:04:46 +00:00
|
|
|
result = newStmtList()
|
|
|
|
var caseStmt = newNimNode(nnkCaseStmt).add(name)
|
2016-09-21 13:40:03 +00:00
|
|
|
var fieldIndex = 0
|
2016-06-05 18:52:43 +00:00
|
|
|
for child in tDesc[2].children:
|
2016-06-05 17:29:16 +00:00
|
|
|
if child.kind == nnkRecCase:
|
|
|
|
let
|
|
|
|
discriminant = newDotExpr(o, newIdentNode($child[0]))
|
|
|
|
discType = newCall("type", discriminant)
|
|
|
|
var disOb = newNimNode(nnkOfBranch).add(newStrLitNode($child[0]))
|
2019-06-16 11:43:08 +00:00
|
|
|
var objConstr = newNimNode(nnkObjConstr).add(newCall("type", o))
|
|
|
|
objConstr.add(newColonExpr(newIdentNode($child[0]), newIdentNode(
|
|
|
|
"value")))
|
|
|
|
for otherChild in tDesc[2].children:
|
|
|
|
if otherChild == child:
|
|
|
|
continue
|
2019-06-16 11:45:21 +00:00
|
|
|
if otherChild.kind != nnkSym:
|
|
|
|
error("Unexpected kind of field '" & $otherChild[0] &
|
|
|
|
"': " & $otherChild.kind)
|
2019-06-16 11:43:08 +00:00
|
|
|
objConstr.add(newColonExpr(newIdentNode($otherChild), newDotExpr(o,
|
|
|
|
newIdentNode($otherChild))))
|
2016-06-05 17:29:16 +00:00
|
|
|
disOb.add(newStmtList(
|
2016-09-24 14:45:49 +00:00
|
|
|
checkDuplicate(stream, tName, $child[0], fieldIndex, matched),
|
2016-06-05 17:29:16 +00:00
|
|
|
newNimNode(nnkVarSection).add(
|
|
|
|
newNimNode(nnkIdentDefs).add(
|
|
|
|
newIdentNode("value"), discType, newEmptyNode())),
|
|
|
|
newCall("constructChild", stream, context, newIdentNode("value")),
|
2019-06-16 11:43:08 +00:00
|
|
|
newAssignment(o, objConstr),
|
2016-09-21 13:40:03 +00:00
|
|
|
markAsFound(fieldIndex, matched)))
|
2016-10-19 20:04:46 +00:00
|
|
|
caseStmt.add(disOb)
|
|
|
|
var alreadyUsedSet = newNimNode(nnkCurly)
|
2016-06-05 17:29:16 +00:00
|
|
|
for bIndex in 1 .. len(child) - 1:
|
2016-10-19 20:04:46 +00:00
|
|
|
var recListIndex = 0
|
|
|
|
var discTest: NimNode
|
|
|
|
case child[bIndex].kind
|
|
|
|
of nnkOfBranch:
|
|
|
|
discTest = newNimNode(nnkCurly)
|
2016-11-30 17:01:03 +00:00
|
|
|
while recListIndex < child[bIndex].len - 1:
|
|
|
|
yAssert child[bIndex][recListIndex].kind == nnkIntLit
|
2016-10-19 20:04:46 +00:00
|
|
|
discTest.add(child[bIndex][recListIndex])
|
|
|
|
alreadyUsedSet.add(child[bIndex][recListIndex])
|
|
|
|
inc(recListIndex)
|
|
|
|
discTest = infix(discriminant, "in", discTest)
|
|
|
|
of nnkElse:
|
|
|
|
discTest = infix(discriminant, "notin", alreadyUsedSet)
|
|
|
|
else:
|
|
|
|
internalError("Unexpected child kind: " & $child[bIndex].kind)
|
|
|
|
|
2016-11-30 17:01:03 +00:00
|
|
|
for item in child[bIndex][recListIndex].recListItems:
|
2016-09-22 12:16:10 +00:00
|
|
|
inc(fieldIndex)
|
2016-08-09 18:47:22 +00:00
|
|
|
yAssert item.kind == nnkSym
|
2016-06-05 17:29:16 +00:00
|
|
|
var ob = newNimNode(nnkOfBranch).add(newStrLitNode($item))
|
|
|
|
let field = newDotExpr(o, newIdentNode($item))
|
|
|
|
var ifStmt = newIfStmt((cond: discTest, body: newStmtList(
|
|
|
|
newCall("constructChild", stream, context, field))))
|
2016-06-08 17:15:50 +00:00
|
|
|
ifStmt.add(newNimNode(nnkElse).add(newNimNode(nnkRaiseStmt).add(
|
2016-09-23 13:42:24 +00:00
|
|
|
newCall(bindSym("constructionError"), stream,
|
2016-06-08 17:15:50 +00:00
|
|
|
infix(newStrLitNode("Field " & $item & " not allowed for " &
|
|
|
|
$child[0] & " == "), "&", prefix(discriminant, "$"))))))
|
2020-06-26 19:29:41 +00:00
|
|
|
ob.add(ifNotTransient(o, item,
|
2016-10-19 20:04:46 +00:00
|
|
|
[checkDuplicate(stream, tName, $item, fieldIndex, matched),
|
|
|
|
ifStmt, markAsFound(fieldIndex, matched)], true, stream, tName,
|
|
|
|
$item))
|
|
|
|
caseStmt.add(ob)
|
2016-06-05 17:29:16 +00:00
|
|
|
else:
|
2016-08-09 18:47:22 +00:00
|
|
|
yAssert child.kind == nnkSym
|
2016-06-05 17:29:16 +00:00
|
|
|
var ob = newNimNode(nnkOfBranch).add(newStrLitNode($child))
|
|
|
|
let field = newDotExpr(o, newIdentNode($child))
|
2020-06-26 19:29:41 +00:00
|
|
|
ob.add(ifNotTransient(o, child,
|
2016-10-19 20:04:46 +00:00
|
|
|
[checkDuplicate(stream, tName, $child, fieldIndex, matched),
|
2016-09-21 13:40:03 +00:00
|
|
|
newCall("constructChild", stream, context, field),
|
2016-10-19 20:04:46 +00:00
|
|
|
markAsFound(fieldIndex, matched)], true, stream, tName, $child))
|
|
|
|
caseStmt.add(ob)
|
2016-09-21 13:40:03 +00:00
|
|
|
inc(fieldIndex)
|
2017-09-20 16:58:09 +00:00
|
|
|
caseStmt.add(newNimNode(nnkElse).add(newNimNode(nnkWhenStmt).add(
|
|
|
|
newNimNode(nnkElifBranch).add(failOnUnknown,
|
|
|
|
newNimNode(nnkRaiseStmt).add(
|
|
|
|
newCall(bindSym("constructionError"), stream,
|
|
|
|
infix(newLit("While constructing " & tName & ": Unknown field: "), "&",
|
|
|
|
newCall(bindSym("escape"), name))))))))
|
2016-10-19 20:04:46 +00:00
|
|
|
result.add(caseStmt)
|
2016-06-05 17:29:16 +00:00
|
|
|
|
2019-06-11 19:30:14 +00:00
|
|
|
proc isVariantObject(t: NimNode): bool {.compileTime.} =
|
2016-10-15 15:58:29 +00:00
|
|
|
var tDesc = getType(t)
|
|
|
|
if tDesc.kind == nnkBracketExpr: tDesc = getType(tDesc[1])
|
|
|
|
if tDesc.kind != nnkObjectTy:
|
|
|
|
return false
|
2016-06-08 17:15:50 +00:00
|
|
|
for child in tDesc[2].children:
|
|
|
|
if child.kind == nnkRecCase: return true
|
|
|
|
return false
|
|
|
|
|
2020-06-26 19:29:41 +00:00
|
|
|
proc hasIgnore(t: typedesc): bool {.compileTime.} =
|
|
|
|
when compiles(t.hasCustomPragma(ignore)):
|
|
|
|
return t.hasCustomPragma(ignore)
|
|
|
|
else:
|
|
|
|
return false
|
2017-09-20 16:58:09 +00:00
|
|
|
|
2017-03-29 19:42:07 +00:00
|
|
|
proc constructObjectDefault*[O: object|tuple](
|
2016-04-02 15:48:22 +00:00
|
|
|
s: var YamlStream, c: ConstructionContext, result: var O)
|
|
|
|
{.raises: [YamlConstructionError, YamlStreamError].} =
|
2017-03-29 19:42:07 +00:00
|
|
|
## Constructs a Nim object or tuple from a YAML mapping.
|
|
|
|
## This is the default implementation for custom objects and tuples and should
|
|
|
|
## not be redefined. If you are adding a custom constructObject()
|
|
|
|
## implementation, you can use this proc to call the default implementation
|
|
|
|
## within it.
|
2016-09-21 13:40:03 +00:00
|
|
|
var matched = matchMatrix(O)
|
|
|
|
var e = s.next()
|
2016-06-08 17:15:50 +00:00
|
|
|
const
|
2019-06-11 19:30:14 +00:00
|
|
|
startKind = when isVariantObject(getType(O)): yamlStartSeq else: yamlStartMap
|
|
|
|
endKind = when isVariantObject(getType(O)): yamlEndSeq else: yamlEndMap
|
2016-06-08 17:15:50 +00:00
|
|
|
if e.kind != startKind:
|
2016-09-23 13:42:24 +00:00
|
|
|
raise s.constructionError("While constructing " &
|
|
|
|
typetraits.name(O) & ": Expected " & $startKind & ", got " & $e.kind)
|
2020-06-26 19:29:41 +00:00
|
|
|
when hasIgnore(O):
|
|
|
|
const ignoredKeyList = O.getCustomPragmaVal(ignore)
|
|
|
|
const failOnUnknown = len(ignoredKeyList) > 0
|
|
|
|
else:
|
|
|
|
const failOnUnknown = true
|
2016-06-08 17:15:50 +00:00
|
|
|
while s.peek.kind != endKind:
|
2016-09-21 13:40:03 +00:00
|
|
|
e = s.next()
|
2019-06-11 19:30:14 +00:00
|
|
|
when isVariantObject(getType(O)):
|
2016-06-08 17:15:50 +00:00
|
|
|
if e.kind != yamlStartMap:
|
2016-09-23 13:42:24 +00:00
|
|
|
raise s.constructionError("Expected single-pair map, got " & $e.kind)
|
2016-06-08 17:15:50 +00:00
|
|
|
e = s.next()
|
2016-04-02 15:48:22 +00:00
|
|
|
if e.kind != yamlScalar:
|
2016-09-23 13:42:24 +00:00
|
|
|
raise s.constructionError("Expected field name, got " & $e.kind)
|
2016-04-02 15:48:22 +00:00
|
|
|
let name = e.scalarContent
|
2016-06-05 18:52:43 +00:00
|
|
|
when result is tuple:
|
2016-09-21 13:40:03 +00:00
|
|
|
var i = 0
|
|
|
|
var found = false
|
2016-06-05 18:52:43 +00:00
|
|
|
for fname, value in fieldPairs(result):
|
|
|
|
if fname == name:
|
2016-09-21 13:40:03 +00:00
|
|
|
if matched[i]:
|
2016-09-23 13:42:24 +00:00
|
|
|
raise s.constructionError("While constructing " &
|
2016-09-21 13:40:03 +00:00
|
|
|
typetraits.name(O) & ": Duplicate field: " & escape(name))
|
2016-06-05 18:52:43 +00:00
|
|
|
constructChild(s, c, value)
|
2016-09-21 13:40:03 +00:00
|
|
|
matched[i] = true
|
|
|
|
found = true
|
2016-06-05 18:52:43 +00:00
|
|
|
break
|
2016-09-21 13:40:03 +00:00
|
|
|
inc(i)
|
2017-09-20 16:58:09 +00:00
|
|
|
when failOnUnknown:
|
|
|
|
if not found:
|
|
|
|
raise s.constructionError("While constructing " &
|
|
|
|
typetraits.name(O) & ": Unknown field: " & escape(name))
|
2016-06-08 17:15:50 +00:00
|
|
|
else:
|
2020-06-26 19:29:41 +00:00
|
|
|
when hasIgnore(O) and failOnUnknown:
|
|
|
|
if name notin ignoredKeyList:
|
|
|
|
constructFieldValue(O, s, c, name, result, matched, failOnUnknown)
|
|
|
|
else:
|
|
|
|
e = s.next()
|
|
|
|
var depth = int(e.kind in {yamlStartMap, yamlStartSeq})
|
|
|
|
while depth > 0:
|
|
|
|
case s.next().kind
|
|
|
|
of yamlStartMap, yamlStartSeq: inc(depth)
|
|
|
|
of yamlEndMap, yamlEndSeq: dec(depth)
|
|
|
|
of yamlScalar: discard
|
|
|
|
else: internalError("Unexpected event kind.")
|
2016-10-27 15:58:14 +00:00
|
|
|
else:
|
2020-06-26 19:29:41 +00:00
|
|
|
constructFieldValue(O, s, c, name, result, matched, failOnUnknown)
|
2019-06-11 19:30:14 +00:00
|
|
|
when isVariantObject(getType(O)):
|
2016-06-08 17:15:50 +00:00
|
|
|
e = s.next()
|
|
|
|
if e.kind != yamlEndMap:
|
2016-09-23 13:42:24 +00:00
|
|
|
raise s.constructionError("Expected end of single-pair map, got " &
|
|
|
|
$e.kind)
|
2016-04-02 15:48:22 +00:00
|
|
|
discard s.next()
|
2016-09-21 13:40:03 +00:00
|
|
|
when result is tuple:
|
|
|
|
var i = 0
|
|
|
|
for fname, value in fieldPairs(result):
|
|
|
|
if not matched[i]:
|
2016-09-23 13:42:24 +00:00
|
|
|
raise s.constructionError("While constructing " &
|
2016-09-24 14:45:49 +00:00
|
|
|
typetraits.name(O) & ": Missing field: " & escape(fname))
|
2016-09-21 13:40:03 +00:00
|
|
|
inc(i)
|
2020-06-26 19:29:41 +00:00
|
|
|
else: ensureAllFieldsPresent(s, O, result, matched)
|
2016-02-01 18:48:42 +00:00
|
|
|
|
2017-03-29 19:42:07 +00:00
|
|
|
proc constructObject*[O: object|tuple](
|
|
|
|
s: var YamlStream, c: ConstructionContext, result: var O)
|
|
|
|
{.raises: [YamlConstructionError, YamlStreamError].} =
|
|
|
|
## Overridable default implementation for custom object and tuple types
|
|
|
|
constructObjectDefault(s, c, result)
|
|
|
|
|
2020-03-08 18:58:02 +00:00
|
|
|
macro genRepresentObject(t: typedesc, value, childTagStyle: typed) =
|
2016-10-15 15:58:29 +00:00
|
|
|
result = newStmtList()
|
|
|
|
let
|
|
|
|
tDecl = getType(t)
|
|
|
|
tDesc = getType(tDecl[1])
|
|
|
|
isVO = isVariantObject(t)
|
|
|
|
var fieldIndex = 0'i16
|
|
|
|
for child in tDesc[2].children:
|
|
|
|
if child.kind == nnkRecCase:
|
|
|
|
let
|
|
|
|
fieldName = $child[0]
|
|
|
|
fieldAccessor = newDotExpr(value, newIdentNode(fieldName))
|
|
|
|
result.add(quote do:
|
|
|
|
c.put(startMapEvent(yTagQuestionMark, yAnchorNone))
|
|
|
|
c.put(scalarEvent(`fieldName`, if `childTagStyle` == tsNone:
|
|
|
|
yTagQuestionMark else: yTagNimField, yAnchorNone))
|
|
|
|
representChild(`fieldAccessor`, `childTagStyle`, c)
|
|
|
|
c.put(endMapEvent())
|
|
|
|
)
|
|
|
|
let enumName = $getTypeInst(child[0])
|
|
|
|
var caseStmt = newNimNode(nnkCaseStmt).add(fieldAccessor)
|
|
|
|
for bIndex in 1 .. len(child) - 1:
|
|
|
|
var curBranch: NimNode
|
|
|
|
var recListIndex = 0
|
|
|
|
case child[bIndex].kind
|
|
|
|
of nnkOfBranch:
|
|
|
|
curBranch = newNimNode(nnkOfBranch)
|
2016-11-30 17:01:03 +00:00
|
|
|
while recListIndex < child[bIndex].len - 1:
|
|
|
|
expectKind(child[bIndex][recListIndex], nnkIntLit)
|
2016-10-15 15:58:29 +00:00
|
|
|
curBranch.add(newCall(enumName, newLit(child[bIndex][recListIndex].intVal)))
|
|
|
|
inc(recListIndex)
|
|
|
|
of nnkElse:
|
|
|
|
curBranch = newNimNode(nnkElse)
|
|
|
|
else:
|
|
|
|
internalError("Unexpected child kind: " & $child[bIndex].kind)
|
|
|
|
var curStmtList = newStmtList()
|
2016-11-30 17:01:03 +00:00
|
|
|
if child[bIndex][recListIndex].recListLen > 0:
|
|
|
|
for item in child[bIndex][recListIndex].recListItems():
|
2016-10-15 15:58:29 +00:00
|
|
|
inc(fieldIndex)
|
|
|
|
let
|
|
|
|
name = $item
|
|
|
|
itemAccessor = newDotExpr(value, newIdentNode(name))
|
|
|
|
curStmtList.add(quote do:
|
2020-06-26 19:29:41 +00:00
|
|
|
when not `itemAccessor`.hasCustomPragma(transient):
|
2016-10-15 15:58:29 +00:00
|
|
|
c.put(startMapEvent(yTagQuestionMark, yAnchorNone))
|
|
|
|
c.put(scalarEvent(`name`, if `childTagStyle` == tsNone:
|
|
|
|
yTagQuestionMark else: yTagNimField, yAnchorNone))
|
|
|
|
representChild(`itemAccessor`, `childTagStyle`, c)
|
|
|
|
c.put(endMapEvent())
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
curStmtList.add(newNimNode(nnkDiscardStmt).add(newEmptyNode()))
|
|
|
|
curBranch.add(curStmtList)
|
|
|
|
caseStmt.add(curBranch)
|
|
|
|
result.add(caseStmt)
|
|
|
|
else:
|
|
|
|
let
|
|
|
|
name = $child
|
|
|
|
childAccessor = newDotExpr(value, newIdentNode(name))
|
|
|
|
result.add(quote do:
|
2020-06-26 19:29:41 +00:00
|
|
|
when not `childAccessor`.hasCustomPragma(transient):
|
2018-10-03 17:44:31 +00:00
|
|
|
when bool(`isVO`): c.put(startMapEvent(yTagQuestionMark, yAnchorNone))
|
2016-10-15 15:58:29 +00:00
|
|
|
c.put(scalarEvent(`name`, if `childTagStyle` == tsNone:
|
|
|
|
yTagQuestionMark else: yTagNimField, yAnchorNone))
|
|
|
|
representChild(`childAccessor`, `childTagStyle`, c)
|
2018-10-03 17:44:31 +00:00
|
|
|
when bool(`isVO`): c.put(endMapEvent())
|
2016-10-15 15:58:29 +00:00
|
|
|
)
|
|
|
|
inc(fieldIndex)
|
|
|
|
|
|
|
|
proc representObject*[O: object](value: O, ts: TagStyle,
|
2016-09-22 12:25:10 +00:00
|
|
|
c: SerializationContext, tag: TagId) =
|
2016-04-02 15:48:22 +00:00
|
|
|
## represents a Nim object or tuple as YAML mapping
|
2016-09-01 18:56:34 +00:00
|
|
|
let childTagStyle = if ts == tsRootOnly: tsNone else: ts
|
2019-06-11 19:30:14 +00:00
|
|
|
when isVariantObject(getType(O)): c.put(startSeqEvent(tag, yAnchorNone))
|
2016-09-01 18:56:34 +00:00
|
|
|
else: c.put(startMapEvent(tag, yAnchorNone))
|
2016-10-12 20:39:59 +00:00
|
|
|
genRepresentObject(O, value, childTagStyle)
|
2019-06-11 19:30:14 +00:00
|
|
|
when isVariantObject(getType(O)): c.put(endSeqEvent())
|
2016-09-01 18:56:34 +00:00
|
|
|
else: c.put(endMapEvent())
|
2016-10-15 15:58:29 +00:00
|
|
|
|
|
|
|
proc representObject*[O: tuple](value: O, ts: TagStyle,
|
|
|
|
c: SerializationContext, tag: TagId) =
|
|
|
|
let childTagStyle = if ts == tsRootOnly: tsNone else: ts
|
|
|
|
var fieldIndex = 0'i16
|
|
|
|
c.put(startMapEvent(tag, yAnchorNone))
|
|
|
|
for name, fvalue in fieldPairs(value):
|
|
|
|
c.put(scalarEvent(name, if childTagStyle == tsNone:
|
|
|
|
yTagQuestionMark else: yTagNimField, yAnchorNone))
|
|
|
|
representChild(fvalue, childTagStyle, c)
|
|
|
|
inc(fieldIndex)
|
|
|
|
c.put(endMapEvent())
|
2016-02-01 18:48:42 +00:00
|
|
|
|
2016-02-12 18:53:25 +00:00
|
|
|
proc constructObject*[O: enum](s: var YamlStream, c: ConstructionContext,
|
2016-02-01 19:16:35 +00:00
|
|
|
result: var O)
|
2016-04-02 15:48:22 +00:00
|
|
|
{.raises: [YamlConstructionError, YamlStreamError].} =
|
|
|
|
## constructs a Nim enum from a YAML scalar
|
|
|
|
let e = s.next()
|
|
|
|
if e.kind != yamlScalar:
|
2016-09-23 13:42:24 +00:00
|
|
|
raise s.constructionError("Expected scalar, got " & $e.kind)
|
2016-04-02 15:48:22 +00:00
|
|
|
try: result = parseEnum[O](e.scalarContent)
|
|
|
|
except ValueError:
|
2016-09-23 13:42:24 +00:00
|
|
|
var ex = s.constructionError("Cannot parse '" &
|
|
|
|
escape(e.scalarContent) & "' as " & type(O).name)
|
2016-04-02 15:48:22 +00:00
|
|
|
ex.parent = getCurrentException()
|
|
|
|
raise ex
|
2016-02-01 19:16:35 +00:00
|
|
|
|
2016-02-02 17:19:40 +00:00
|
|
|
proc representObject*[O: enum](value: O, ts: TagStyle,
|
2016-09-01 18:56:34 +00:00
|
|
|
c: SerializationContext, tag: TagId) {.raises: [].} =
|
2016-04-02 15:48:22 +00:00
|
|
|
## represents a Nim enum as YAML scalar
|
2016-09-01 18:56:34 +00:00
|
|
|
c.put(scalarEvent($value, tag, yAnchorNone))
|
2016-02-01 19:16:35 +00:00
|
|
|
|
2016-01-28 20:59:26 +00:00
|
|
|
proc yamlTag*[O](T: typedesc[ref O]): TagId {.inline, raises: [].} = yamlTag(O)
|
|
|
|
|
2016-08-17 20:50:37 +00:00
|
|
|
macro constructImplicitVariantObject(s, c, r, possibleTagIds: untyped,
|
2020-03-08 18:58:02 +00:00
|
|
|
t: typedesc) =
|
2016-06-05 17:29:16 +00:00
|
|
|
let tDesc = getType(getType(t)[1])
|
2016-08-09 18:47:22 +00:00
|
|
|
yAssert tDesc.kind == nnkObjectTy
|
2016-06-05 18:52:43 +00:00
|
|
|
let recCase = tDesc[2][0]
|
2016-08-09 18:47:22 +00:00
|
|
|
yAssert recCase.kind == nnkRecCase
|
2019-06-16 11:43:08 +00:00
|
|
|
result = newNimNode(nnkIfStmt)
|
2016-06-05 17:29:16 +00:00
|
|
|
for i in 1 .. recCase.len - 1:
|
2016-08-09 18:47:22 +00:00
|
|
|
yAssert recCase[i].kind == nnkOfBranch
|
2016-06-05 17:29:16 +00:00
|
|
|
var branch = newNimNode(nnkElifBranch)
|
2019-06-16 11:43:08 +00:00
|
|
|
var branchContent = newStmtList(newAssignment(r,
|
|
|
|
newNimNode(nnkObjConstr).add(
|
|
|
|
newCall("type", r),
|
|
|
|
newColonExpr(newIdentNode($recCase[0]), recCase[i][0])
|
|
|
|
)))
|
2016-11-30 17:01:03 +00:00
|
|
|
case recCase[i][1].recListLen
|
2016-06-05 17:29:16 +00:00
|
|
|
of 0:
|
|
|
|
branch.add(infix(newIdentNode("yTagNull"), "in", possibleTagIds))
|
|
|
|
branchContent.add(newNimNode(nnkDiscardStmt).add(newCall("next", s)))
|
|
|
|
of 1:
|
2016-11-30 17:01:03 +00:00
|
|
|
let field = newDotExpr(r, newIdentNode($recCase[i][1].recListNode))
|
2016-06-05 17:29:16 +00:00
|
|
|
branch.add(infix(
|
|
|
|
newCall("yamlTag", newCall("type", field)), "in", possibleTagIds))
|
|
|
|
branchContent.add(newCall("constructChild", s, c, field))
|
2018-05-17 21:18:09 +00:00
|
|
|
else:
|
|
|
|
block:
|
|
|
|
internalError("Too many children: " & $recCase[i][1].recListlen)
|
2016-06-05 17:29:16 +00:00
|
|
|
branch.add(branchContent)
|
2019-06-16 11:43:08 +00:00
|
|
|
result.add(branch)
|
2016-06-05 17:29:16 +00:00
|
|
|
let raiseStmt = newNimNode(nnkRaiseStmt).add(
|
2016-09-23 13:42:24 +00:00
|
|
|
newCall(bindSym("constructionError"), s,
|
2016-06-05 17:29:16 +00:00
|
|
|
infix(newStrLitNode("This value type does not map to any field in " &
|
2019-06-11 19:50:52 +00:00
|
|
|
getTypeImpl(t)[1].repr & ": "), "&",
|
2016-06-05 17:29:16 +00:00
|
|
|
newCall("uri", newIdentNode("serializationTagLibrary"),
|
|
|
|
newNimNode(nnkBracketExpr).add(possibleTagIds, newIntLitNode(0)))
|
|
|
|
)
|
|
|
|
))
|
2019-06-16 11:43:08 +00:00
|
|
|
result.add(newNimNode(nnkElse).add(newNimNode(nnkTryStmt).add(
|
2016-06-05 17:29:16 +00:00
|
|
|
newStmtList(raiseStmt), newNimNode(nnkExceptBranch).add(
|
2016-10-10 18:16:54 +00:00
|
|
|
newIdentNode("KeyError"),
|
2016-10-08 18:57:53 +00:00
|
|
|
newNimNode(nnkDiscardStmt).add(newEmptyNode())
|
2016-06-05 17:29:16 +00:00
|
|
|
))))
|
|
|
|
|
2020-06-26 19:29:41 +00:00
|
|
|
proc isImplicitVariantObject(t: typedesc): bool {.compileTime.} =
|
|
|
|
when compiles(t.hasCustomPragma(implicit)):
|
|
|
|
return t.hasCustomPragma(implicit)
|
|
|
|
else:
|
|
|
|
return false
|
|
|
|
|
|
|
|
proc canBeImplicit(t: typedesc): bool {.compileTime.} =
|
|
|
|
let tDesc = getType(t)
|
|
|
|
if tDesc.kind != nnkObjectTy: return false
|
|
|
|
if tDesc[2].len != 1: return false
|
|
|
|
if tDesc[2][0].kind != nnkRecCase: return false
|
|
|
|
var foundEmptyBranch = false
|
|
|
|
for i in 1.. tDesc[2][0].len - 1:
|
|
|
|
case tDesc[2][0][i][1].recListlen # branch contents
|
|
|
|
of 0:
|
|
|
|
if foundEmptyBranch: return false
|
|
|
|
else: foundEmptyBranch = true
|
|
|
|
of 1: discard
|
|
|
|
else: return false
|
|
|
|
return true
|
2016-10-08 18:57:53 +00:00
|
|
|
|
2016-02-16 18:24:55 +00:00
|
|
|
proc constructChild*[T](s: var YamlStream, c: ConstructionContext,
|
|
|
|
result: var T) =
|
2016-04-02 15:48:22 +00:00
|
|
|
let item = s.peek()
|
2020-06-26 19:29:41 +00:00
|
|
|
when isImplicitVariantObject(T):
|
|
|
|
when not canBeImplicit(T):
|
|
|
|
{. fatal: "This type cannot be marked as implicit" .}
|
2016-06-05 17:29:16 +00:00
|
|
|
var possibleTagIds = newSeq[TagId]()
|
|
|
|
case item.kind
|
|
|
|
of yamlScalar:
|
|
|
|
case item.scalarTag
|
|
|
|
of yTagQuestionMark:
|
|
|
|
case guessType(item.scalarContent)
|
|
|
|
of yTypeInteger:
|
|
|
|
possibleTagIds.add([yamlTag(int), yamlTag(int8), yamlTag(int16),
|
|
|
|
yamlTag(int32), yamlTag(int64)])
|
|
|
|
if item.scalarContent[0] != '-':
|
|
|
|
possibleTagIds.add([yamlTag(uint), yamlTag(uint8), yamlTag(uint16),
|
|
|
|
yamlTag(uint32), yamlTag(uint64)])
|
|
|
|
of yTypeFloat, yTypeFloatInf, yTypeFloatNaN:
|
|
|
|
possibleTagIds.add([yamlTag(float), yamlTag(float32),
|
|
|
|
yamlTag(float64)])
|
|
|
|
of yTypeBoolTrue, yTypeBoolFalse:
|
|
|
|
possibleTagIds.add(yamlTag(bool))
|
|
|
|
of yTypeNull:
|
2016-09-23 13:42:24 +00:00
|
|
|
raise s.constructionError("not implemented!")
|
2016-06-05 17:29:16 +00:00
|
|
|
of yTypeUnknown:
|
|
|
|
possibleTagIds.add(yamlTag(string))
|
2016-11-08 20:13:01 +00:00
|
|
|
of yTypeTimestamp:
|
|
|
|
possibleTagIds.add(yamlTag(Time))
|
2016-06-05 17:29:16 +00:00
|
|
|
of yTagExclamationMark:
|
|
|
|
possibleTagIds.add(yamlTag(string))
|
|
|
|
else:
|
|
|
|
possibleTagIds.add(item.scalarTag)
|
|
|
|
of yamlStartMap:
|
|
|
|
if item.mapTag in [yTagQuestionMark, yTagExclamationMark]:
|
2016-09-23 13:42:24 +00:00
|
|
|
raise s.constructionError(
|
2016-06-05 17:29:16 +00:00
|
|
|
"Complex value of implicit variant object type must have a tag.")
|
|
|
|
possibleTagIds.add(item.mapTag)
|
|
|
|
of yamlStartSeq:
|
|
|
|
if item.seqTag in [yTagQuestionMark, yTagExclamationMark]:
|
2016-09-23 13:42:24 +00:00
|
|
|
raise s.constructionError(
|
2016-06-05 17:29:16 +00:00
|
|
|
"Complex value of implicit variant object type must have a tag.")
|
|
|
|
possibleTagIds.add(item.seqTag)
|
2016-08-09 18:47:22 +00:00
|
|
|
else: internalError("Unexpected item kind: " & $item.kind)
|
2016-06-05 17:29:16 +00:00
|
|
|
constructImplicitVariantObject(s, c, result, possibleTagIds, T)
|
|
|
|
else:
|
|
|
|
case item.kind
|
|
|
|
of yamlScalar:
|
|
|
|
if item.scalarTag notin [yTagQuestionMark, yTagExclamationMark,
|
|
|
|
yamlTag(T)]:
|
2016-09-23 13:42:24 +00:00
|
|
|
raise s.constructionError("Wrong tag for " & typetraits.name(T))
|
2016-06-05 17:29:16 +00:00
|
|
|
elif item.scalarAnchor != yAnchorNone:
|
2016-09-23 13:42:24 +00:00
|
|
|
raise s.constructionError("Anchor on non-ref type")
|
2016-06-05 17:29:16 +00:00
|
|
|
of yamlStartMap:
|
|
|
|
if item.mapTag notin [yTagQuestionMark, yamlTag(T)]:
|
2016-09-23 13:42:24 +00:00
|
|
|
raise s.constructionError("Wrong tag for " & typetraits.name(T))
|
2016-06-05 17:29:16 +00:00
|
|
|
elif item.mapAnchor != yAnchorNone:
|
2016-09-23 13:42:24 +00:00
|
|
|
raise s.constructionError("Anchor on non-ref type")
|
2016-06-05 17:29:16 +00:00
|
|
|
of yamlStartSeq:
|
|
|
|
if item.seqTag notin [yTagQuestionMark, yamlTag(T)]:
|
2016-09-23 13:42:24 +00:00
|
|
|
raise s.constructionError("Wrong tag for " & typetraits.name(T))
|
2016-06-05 17:29:16 +00:00
|
|
|
elif item.seqAnchor != yAnchorNone:
|
2016-09-23 13:42:24 +00:00
|
|
|
raise s.constructionError("Anchor on non-ref type")
|
2016-08-09 18:47:22 +00:00
|
|
|
else: internalError("Unexpected item kind: " & $item.kind)
|
2016-06-05 17:29:16 +00:00
|
|
|
constructObject(s, c, result)
|
2016-02-16 18:24:55 +00:00
|
|
|
|
2016-04-04 19:21:24 +00:00
|
|
|
proc constructChild*(s: var YamlStream, c: ConstructionContext,
|
|
|
|
result: var string) =
|
|
|
|
let item = s.peek()
|
|
|
|
if item.kind == yamlScalar:
|
2020-11-03 21:08:21 +00:00
|
|
|
if item.scalarProperties.tag notin
|
2016-04-04 19:21:24 +00:00
|
|
|
[yTagQuestionMark, yTagExclamationMark, yamlTag(string)]:
|
2016-09-23 13:42:24 +00:00
|
|
|
raise s.constructionError("Wrong tag for string")
|
2020-11-03 21:08:21 +00:00
|
|
|
elif item.scalarProperties.anchor != yAnchorNone:
|
2016-09-23 13:42:24 +00:00
|
|
|
raise s.constructionError("Anchor on non-ref type")
|
2016-04-04 19:21:24 +00:00
|
|
|
constructObject(s, c, result)
|
|
|
|
|
|
|
|
proc constructChild*[T](s: var YamlStream, c: ConstructionContext,
|
|
|
|
result: var seq[T]) =
|
|
|
|
let item = s.peek()
|
2018-10-10 15:43:39 +00:00
|
|
|
if item.kind == yamlStartSeq:
|
2016-04-04 19:21:24 +00:00
|
|
|
if item.seqTag notin [yTagQuestionMark, yamlTag(seq[T])]:
|
2016-09-23 13:42:24 +00:00
|
|
|
raise s.constructionError("Wrong tag for " & typetraits.name(seq[T]))
|
2016-04-04 19:21:24 +00:00
|
|
|
elif item.seqAnchor != yAnchorNone:
|
2016-09-23 13:42:24 +00:00
|
|
|
raise s.constructionError("Anchor on non-ref type")
|
2016-04-04 19:21:24 +00:00
|
|
|
constructObject(s, c, result)
|
2016-07-08 09:35:31 +00:00
|
|
|
|
2020-03-08 18:58:02 +00:00
|
|
|
proc constructChild*[T](s: var YamlStream, c: ConstructionContext,
|
|
|
|
result: var Option[T]) =
|
|
|
|
## constructs an optional value. A value with a !!null tag will be loaded
|
|
|
|
## an empty value.
|
|
|
|
let event = s.peek()
|
|
|
|
if event.kind == yamlScalar and event.scalarTag == yTagNull:
|
|
|
|
result = none(T)
|
|
|
|
discard s.next()
|
|
|
|
else:
|
|
|
|
var inner: T
|
|
|
|
constructChild(s, c, inner)
|
|
|
|
result = some(inner)
|
|
|
|
|
2017-02-13 16:10:56 +00:00
|
|
|
when defined(JS):
|
|
|
|
# in JS, Time is a ref type. Therefore, we need this specialization so that
|
|
|
|
# it is not handled by the general ref-type handler.
|
|
|
|
proc constructChild*(s: var YamlStream, c: ConstructionContext,
|
|
|
|
result: var Time) =
|
|
|
|
let e = s.peek()
|
|
|
|
if e.kind == yamlScalar:
|
|
|
|
if e.scalarTag notin [yTagQuestionMark, yTagTimestamp]:
|
|
|
|
raise s.constructionError("Wrong tag for Time")
|
|
|
|
elif guessType(e.scalarContent) != yTypeTimestamp:
|
|
|
|
raise s.constructionError("Invalid timestamp")
|
|
|
|
elif e.scalarAnchor != yAnchorNone:
|
|
|
|
raise s.constructionError("Anchor on non-ref type")
|
|
|
|
constructObject(s, c, result)
|
|
|
|
else:
|
|
|
|
raise s.constructionError("Unexpected structure, expected timestamp")
|
|
|
|
|
2016-02-16 18:24:55 +00:00
|
|
|
proc constructChild*[O](s: var YamlStream, c: ConstructionContext,
|
2016-04-02 15:48:22 +00:00
|
|
|
result: var ref O) =
|
|
|
|
var e = s.peek()
|
|
|
|
if e.kind == yamlScalar:
|
|
|
|
if e.scalarTag == yTagNull or (e.scalarTag == yTagQuestionMark and
|
|
|
|
guessType(e.scalarContent) == yTypeNull):
|
|
|
|
result = nil
|
|
|
|
discard s.next()
|
|
|
|
return
|
|
|
|
elif e.kind == yamlAlias:
|
2017-03-29 19:42:07 +00:00
|
|
|
when defined(JS):
|
|
|
|
{.emit: [result, """ = """, c, """.refs.get(""", e.aliasTarget, """);"""].}
|
|
|
|
else:
|
|
|
|
result = cast[ref O](c.refs.getOrDefault(e.aliasTarget))
|
2016-08-09 18:47:22 +00:00
|
|
|
discard s.next()
|
|
|
|
return
|
2016-04-02 15:48:22 +00:00
|
|
|
new(result)
|
2020-11-03 21:08:21 +00:00
|
|
|
template removeAnchor(anchor: var Anchor) {.dirty.} =
|
2016-04-02 15:48:22 +00:00
|
|
|
if anchor != yAnchorNone:
|
2017-03-29 19:42:07 +00:00
|
|
|
when defined(JS):
|
|
|
|
{.emit: [c, """.refs.set(""", anchor, """, """, result, """);"""].}
|
|
|
|
else:
|
|
|
|
yAssert(not c.refs.hasKey(anchor))
|
|
|
|
c.refs[anchor] = cast[pointer](result)
|
2016-04-02 15:48:22 +00:00
|
|
|
anchor = yAnchorNone
|
2016-07-08 09:35:31 +00:00
|
|
|
|
2016-04-02 15:48:22 +00:00
|
|
|
case e.kind
|
|
|
|
of yamlScalar: removeAnchor(e.scalarAnchor)
|
|
|
|
of yamlStartMap: removeAnchor(e.mapAnchor)
|
|
|
|
of yamlStartSeq: removeAnchor(e.seqAnchor)
|
2016-08-09 18:47:22 +00:00
|
|
|
else: internalError("Unexpected event kind: " & $e.kind)
|
2016-04-02 15:48:22 +00:00
|
|
|
s.peek = e
|
|
|
|
try: constructChild(s, c, result[])
|
2019-11-30 12:16:40 +00:00
|
|
|
except YamlConstructionError as e:
|
2019-06-11 19:30:14 +00:00
|
|
|
raise e
|
|
|
|
except YamlStreamError as e:
|
|
|
|
raise e
|
2016-04-02 15:48:22 +00:00
|
|
|
except Exception:
|
|
|
|
var e = newException(YamlStreamError, getCurrentExceptionMsg())
|
|
|
|
e.parent = getCurrentException()
|
|
|
|
raise e
|
2016-01-28 20:59:26 +00:00
|
|
|
|
2016-09-01 18:56:34 +00:00
|
|
|
proc representChild*(value: string, ts: TagStyle, c: SerializationContext) =
|
2018-10-03 17:49:05 +00:00
|
|
|
let tag = presentTag(string, ts)
|
|
|
|
representObject(value, ts, c,
|
|
|
|
if tag == yTagQuestionMark and guessType(value) != yTypeUnknown:
|
|
|
|
yTagExclamationMark
|
|
|
|
else:
|
|
|
|
tag)
|
2016-09-01 18:56:34 +00:00
|
|
|
|
|
|
|
proc representChild*[T](value: seq[T], ts: TagStyle, c: SerializationContext) =
|
2018-10-03 17:49:05 +00:00
|
|
|
representObject(value, ts, c, presentTag(seq[T], ts))
|
2016-09-01 18:56:34 +00:00
|
|
|
|
|
|
|
proc representChild*[O](value: ref O, ts: TagStyle, c: SerializationContext) =
|
|
|
|
if isNil(value): c.put(scalarEvent("~", yTagNull))
|
|
|
|
elif c.style == asNone: representChild(value[], ts, c)
|
2016-04-02 15:48:22 +00:00
|
|
|
else:
|
2020-11-03 21:08:21 +00:00
|
|
|
var val: Anchor
|
2017-03-29 19:42:07 +00:00
|
|
|
when defined(JS):
|
|
|
|
{.emit: ["""
|
|
|
|
if (""", c, """.refs.has(""", value, """) {
|
|
|
|
""", val, """ = """, c, """.refs.get(""", value, """);
|
|
|
|
if (val == """, yAnchorNone, ") {"].}
|
|
|
|
val = c.nextAnchorId
|
|
|
|
{.emit: [c, """.refs.set(""", value, """, """, val, """);"""].}
|
2020-11-03 21:08:21 +00:00
|
|
|
nextAnchor(c, len(c.nextAnchorId) - 1)
|
2017-03-29 19:42:07 +00:00
|
|
|
{.emit: "}".}
|
|
|
|
c.put(aliasEvent(val))
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
let p = cast[pointer](value)
|
|
|
|
if c.refs.hasKey(p):
|
|
|
|
val = c.refs.getOrDefault(p)
|
|
|
|
if val == yAnchorNone:
|
|
|
|
val = c.nextAnchorId
|
|
|
|
c.refs[p] = val
|
2020-11-03 21:08:21 +00:00
|
|
|
nextAnchor(c, len(c.nextAnchorId) - 1)
|
2017-03-29 19:42:07 +00:00
|
|
|
c.put(aliasEvent(val))
|
|
|
|
return
|
2017-03-30 15:58:53 +00:00
|
|
|
if c.style == asAlways:
|
|
|
|
val = c.nextAnchorId
|
|
|
|
when defined(JS):
|
|
|
|
{.emit: [c, ".refs.set(", p, ", ", val, ");"].}
|
|
|
|
else: c.refs[p] = val
|
2020-11-03 21:08:21 +00:00
|
|
|
nextAnchor(c, len(c.nextAnchorId) - 1)
|
2017-03-30 15:58:53 +00:00
|
|
|
else: c.refs[p] = yAnchorNone
|
2016-08-09 18:47:22 +00:00
|
|
|
let
|
2020-11-03 21:08:21 +00:00
|
|
|
a = if c.style == asAlways: val else: cast[Anchor](p)
|
2016-08-09 18:47:22 +00:00
|
|
|
childTagStyle = if ts == tsAll: tsAll else: tsRootOnly
|
2017-03-29 19:42:07 +00:00
|
|
|
origPut = c.put
|
2020-11-03 21:08:21 +00:00
|
|
|
c.put = proc(e: Event) =
|
2016-09-01 18:56:34 +00:00
|
|
|
var ex = e
|
|
|
|
case ex.kind
|
2016-08-09 18:47:22 +00:00
|
|
|
of yamlStartMap:
|
2016-09-01 18:56:34 +00:00
|
|
|
ex.mapAnchor = a
|
|
|
|
if ts == tsNone: ex.mapTag = yTagQuestionMark
|
2016-08-09 18:47:22 +00:00
|
|
|
of yamlStartSeq:
|
2016-09-01 18:56:34 +00:00
|
|
|
ex.seqAnchor = a
|
|
|
|
if ts == tsNone: ex.seqTag = yTagQuestionMark
|
2016-08-09 18:47:22 +00:00
|
|
|
of yamlScalar:
|
2016-09-01 18:56:34 +00:00
|
|
|
ex.scalarAnchor = a
|
|
|
|
if ts == tsNone and guessType(ex.scalarContent) != yTypeNull:
|
|
|
|
ex.scalarTag = yTagQuestionMark
|
2016-08-09 18:47:22 +00:00
|
|
|
else: discard
|
2016-09-01 18:56:34 +00:00
|
|
|
c.put = origPut
|
|
|
|
c.put(ex)
|
|
|
|
representChild(value[], childTagStyle, c)
|
2016-01-28 20:59:26 +00:00
|
|
|
|
2020-03-08 18:58:02 +00:00
|
|
|
proc representChild*[T](value: Option[T], ts: TagStyle,
|
|
|
|
c: SerializationContext) =
|
|
|
|
## represents an optional value. If the value is missing, a !!null scalar
|
|
|
|
## will be produced.
|
|
|
|
if value.isSome:
|
|
|
|
representChild(value.get(), ts, c)
|
|
|
|
else:
|
|
|
|
let childTagStyle = if ts == tsRootOnly: tsNone else: ts
|
|
|
|
c.put(scalarEvent("~", yTagNull))
|
|
|
|
|
2016-06-05 13:43:39 +00:00
|
|
|
proc representChild*[O](value: O, ts: TagStyle,
|
2016-09-01 18:56:34 +00:00
|
|
|
c: SerializationContext) =
|
2020-06-26 19:29:41 +00:00
|
|
|
when isImplicitVariantObject(O):
|
2016-06-05 17:29:16 +00:00
|
|
|
# todo: this would probably be nicer if constructed with a macro
|
|
|
|
var count = 0
|
|
|
|
for name, field in fieldPairs(value):
|
|
|
|
if count > 0:
|
2016-09-01 18:56:34 +00:00
|
|
|
representChild(field, if ts == tsAll: tsAll else: tsRootOnly, c)
|
2016-06-05 17:29:16 +00:00
|
|
|
inc(count)
|
2016-09-01 18:56:34 +00:00
|
|
|
if count == 1: c.put(scalarEvent("~", yTagNull))
|
2016-06-05 17:29:16 +00:00
|
|
|
else:
|
2016-09-01 18:56:34 +00:00
|
|
|
representObject(value, ts, c,
|
|
|
|
if ts == tsNone: yTagQuestionMark else: yamlTag(O))
|
2016-06-05 13:43:39 +00:00
|
|
|
|
2016-09-20 19:53:38 +00:00
|
|
|
proc construct*[T](s: var YamlStream, target: var T)
|
2019-06-11 19:30:14 +00:00
|
|
|
{.raises: [YamlStreamError, YamlConstructionError].} =
|
2016-09-20 19:53:38 +00:00
|
|
|
## Constructs a Nim value from a YAML stream.
|
2016-04-02 15:48:22 +00:00
|
|
|
var context = newConstructionContext()
|
|
|
|
try:
|
|
|
|
var e = s.next()
|
2016-08-09 18:47:22 +00:00
|
|
|
yAssert(e.kind == yamlStartDoc)
|
2016-07-08 09:35:31 +00:00
|
|
|
|
2016-04-02 15:48:22 +00:00
|
|
|
constructChild(s, context, target)
|
|
|
|
e = s.next()
|
2016-08-09 18:47:22 +00:00
|
|
|
yAssert(e.kind == yamlEndDoc)
|
2016-04-02 15:48:22 +00:00
|
|
|
except YamlConstructionError:
|
|
|
|
raise (ref YamlConstructionError)(getCurrentException())
|
|
|
|
except YamlStreamError:
|
2016-09-13 10:01:21 +00:00
|
|
|
let cur = getCurrentException()
|
|
|
|
var e = newException(YamlStreamError, cur.msg)
|
|
|
|
e.parent = cur.parent
|
|
|
|
raise e
|
2016-04-02 15:48:22 +00:00
|
|
|
except Exception:
|
|
|
|
# may occur while calling s()
|
|
|
|
var ex = newException(YamlStreamError, "")
|
|
|
|
ex.parent = getCurrentException()
|
|
|
|
raise ex
|
2016-01-26 19:51:21 +00:00
|
|
|
|
2016-09-20 19:53:38 +00:00
|
|
|
proc load*[K](input: Stream | string, target: var K)
|
|
|
|
{.raises: [YamlConstructionError, IOError, YamlParserError].} =
|
|
|
|
## Loads a Nim value from a YAML character stream.
|
2020-11-04 15:40:37 +00:00
|
|
|
var parser: YamlParser
|
|
|
|
parser.init(serializationTagLibrary)
|
|
|
|
var events = parser.parse(input)
|
2016-04-02 15:48:22 +00:00
|
|
|
try: construct(events, target)
|
|
|
|
except YamlStreamError:
|
|
|
|
let e = (ref YamlStreamError)(getCurrentException())
|
2016-09-19 18:51:50 +00:00
|
|
|
if e.parent of IOError: raise (ref IOError)(e.parent)
|
|
|
|
elif e.parent of YamlParserError: raise (ref YamlParserError)(e.parent)
|
2017-02-13 16:10:56 +00:00
|
|
|
else: internalError("Unexpected exception: " & $e.parent.name)
|
2016-09-19 18:51:50 +00:00
|
|
|
|
2016-09-19 20:51:33 +00:00
|
|
|
proc loadMultiDoc*[K](input: Stream | string, target: var seq[K]) =
|
2020-11-04 15:40:37 +00:00
|
|
|
var parser: YamlParser
|
|
|
|
parser.init(serializationTagLibrary)
|
|
|
|
var events = parser.parse(input)
|
2016-09-19 18:51:50 +00:00
|
|
|
try:
|
|
|
|
while not events.finished():
|
|
|
|
var item: K
|
|
|
|
construct(events, item)
|
|
|
|
target.add(item)
|
|
|
|
except YamlConstructionError:
|
|
|
|
var e = (ref YamlConstructionError)(getCurrentException())
|
|
|
|
discard events.getLastTokenContext(e.line, e.column, e.lineContent)
|
|
|
|
raise e
|
|
|
|
except YamlStreamError:
|
|
|
|
let e = (ref YamlStreamError)(getCurrentException())
|
2016-04-02 15:48:22 +00:00
|
|
|
if e.parent of IOError: raise (ref IOError)(e.parent)
|
|
|
|
elif e.parent of YamlParserError: raise (ref YamlParserError)(e.parent)
|
2017-02-13 16:10:56 +00:00
|
|
|
else: internalError("Unexpected exception: " & $e.parent.name)
|
2015-12-29 17:22:55 +00:00
|
|
|
|
2020-11-03 21:08:21 +00:00
|
|
|
proc setAnchor(a: var Anchor, c: var SerializationContext)
|
2016-04-02 15:48:22 +00:00
|
|
|
{.inline.} =
|
2017-03-29 19:42:07 +00:00
|
|
|
if a != yAnchorNone:
|
|
|
|
when defined(JS):
|
|
|
|
{.emit: [a, """ = """, c, """.refs.get(""", a, """);"""].}
|
|
|
|
else:
|
|
|
|
a = c.refs.getOrDefault(cast[pointer](a))
|
2016-07-08 09:35:31 +00:00
|
|
|
|
2016-02-02 17:19:40 +00:00
|
|
|
proc represent*[T](value: T, ts: TagStyle = tsRootOnly,
|
2016-09-22 12:25:10 +00:00
|
|
|
a: AnchorStyle = asTidy): YamlStream =
|
2016-09-20 19:53:38 +00:00
|
|
|
## Represents a Nim value as ``YamlStream``
|
2016-09-01 18:56:34 +00:00
|
|
|
var bys = newBufferYamlStream()
|
2020-11-03 21:08:21 +00:00
|
|
|
var context = newSerializationContext(a, proc(e: Event) =
|
2016-09-20 19:53:38 +00:00
|
|
|
bys.put(e)
|
2016-09-01 18:56:34 +00:00
|
|
|
)
|
2016-09-20 19:53:38 +00:00
|
|
|
bys.put(startDocEvent())
|
2016-09-01 18:56:34 +00:00
|
|
|
representChild(value, ts, context)
|
2016-09-20 19:53:38 +00:00
|
|
|
bys.put(endDocEvent())
|
2016-04-02 15:48:22 +00:00
|
|
|
if a == asTidy:
|
2016-09-20 19:53:38 +00:00
|
|
|
for item in bys.mitems():
|
2016-09-01 18:56:34 +00:00
|
|
|
case item.kind
|
2017-03-29 19:42:07 +00:00
|
|
|
of yamlStartMap: setAnchor(item.mapAnchor, context)
|
|
|
|
of yamlStartSeq: setAnchor(item.seqAnchor, context)
|
|
|
|
of yamlScalar: setAnchor(item.scalarAnchor, context)
|
2016-09-01 18:56:34 +00:00
|
|
|
else: discard
|
|
|
|
result = bys
|
2016-01-28 20:59:26 +00:00
|
|
|
|
2016-02-26 20:55:59 +00:00
|
|
|
proc dump*[K](value: K, target: Stream, tagStyle: TagStyle = tsRootOnly,
|
|
|
|
anchorStyle: AnchorStyle = asTidy,
|
2016-09-20 19:53:38 +00:00
|
|
|
options: PresentationOptions = defaultPresentationOptions)
|
|
|
|
{.raises: [YamlPresenterJsonError, YamlPresenterOutputError,
|
|
|
|
YamlStreamError].} =
|
|
|
|
## Dump a Nim value as YAML character stream.
|
2016-04-02 15:48:22 +00:00
|
|
|
var events = represent(value,
|
|
|
|
if options.style == psCanonical: tsAll else: tagStyle,
|
|
|
|
if options.style == psJson: asNone else: anchorStyle)
|
|
|
|
try: present(events, target, serializationTagLibrary, options)
|
|
|
|
except YamlStreamError:
|
2017-02-13 16:10:56 +00:00
|
|
|
internalError("Unexpected exception: " & $getCurrentException().name)
|
2016-09-14 12:35:41 +00:00
|
|
|
|
|
|
|
proc dump*[K](value: K, tagStyle: TagStyle = tsRootOnly,
|
|
|
|
anchorStyle: AnchorStyle = asTidy,
|
|
|
|
options: PresentationOptions = defaultPresentationOptions):
|
2016-09-22 12:25:10 +00:00
|
|
|
string =
|
2016-09-20 19:53:38 +00:00
|
|
|
## Dump a Nim value as YAML into a string
|
2016-09-19 17:33:29 +00:00
|
|
|
var events = represent(value,
|
|
|
|
if options.style == psCanonical: tsAll else: tagStyle,
|
|
|
|
if options.style == psJson: asNone else: anchorStyle)
|
|
|
|
try: result = present(events, serializationTagLibrary, options)
|
|
|
|
except YamlStreamError:
|
2017-02-13 16:10:56 +00:00
|
|
|
internalError("Unexpected exception: " & $getCurrentException().name)
|