mirror of
https://github.com/status-im/NimYAML.git
synced 2025-02-05 16:04:48 +00:00
Documented serialization macros
This commit is contained in:
parent
927de32d8d
commit
40e303049f
@ -1402,6 +1402,21 @@ macro getFieldIndex(t: typedesc, field: untyped): untyped =
|
|||||||
result.intVal = fieldIndex
|
result.intVal = fieldIndex
|
||||||
|
|
||||||
macro markAsTransient*(t: typedesc, field: untyped): typed =
|
macro markAsTransient*(t: typedesc, field: untyped): typed =
|
||||||
|
## Mark an object field as *transient*, meaning that this object field will
|
||||||
|
## not be serialized when an object instance is dumped as YAML, and also that
|
||||||
|
## the field is not expected to be given in YAML input that is loaded to an
|
||||||
|
## object instance.
|
||||||
|
##
|
||||||
|
## Example usage:
|
||||||
|
##
|
||||||
|
## .. code-block::
|
||||||
|
## type MyObject = object
|
||||||
|
## a, b: string
|
||||||
|
## c: int
|
||||||
|
## markAsTransient(MyObject, a)
|
||||||
|
## markAsTransient(MyObject, c)
|
||||||
|
##
|
||||||
|
## This does not work if the object has been marked as implicit.
|
||||||
let nextBitvectorIndex = transientVectors.len
|
let nextBitvectorIndex = transientVectors.len
|
||||||
result = quote do:
|
result = quote do:
|
||||||
when compiles(`implicitVariantObjectMarker`(`t`)):
|
when compiles(`implicitVariantObjectMarker`(`t`)):
|
||||||
@ -1412,9 +1427,23 @@ macro markAsTransient*(t: typedesc, field: untyped): typed =
|
|||||||
`nextBitvectorIndex`
|
`nextBitvectorIndex`
|
||||||
static: transientVectors.add({})
|
static: transientVectors.add({})
|
||||||
static:
|
static:
|
||||||
transientVectors[`transientBitvectorProc`(`t`)].incl(getFieldIndex(`t`, `field`))
|
transientVectors[`transientBitvectorProc`(`t`)].incl(
|
||||||
|
getFieldIndex(`t`, `field`))
|
||||||
|
|
||||||
macro setDefaultValue*(t: typedesc, field: untyped, value: typed): typed =
|
macro setDefaultValue*(t: typedesc, field: untyped, value: typed): typed =
|
||||||
|
## Set the default value of an object field. Fields with default values may
|
||||||
|
## be absent in YAML input when loading an instance of the object. If the
|
||||||
|
## field is absent in the YAML input, the default value is assigned to the
|
||||||
|
## field.
|
||||||
|
##
|
||||||
|
## Example usage:
|
||||||
|
##
|
||||||
|
## .. code-block::
|
||||||
|
## type MyObject = object
|
||||||
|
## a, b: string
|
||||||
|
## c: tuple[x, y: int]
|
||||||
|
## setDefaultValue(MyObject, a, "foo")
|
||||||
|
## setDefaultValue(MyObject, c, (1, 2))
|
||||||
let
|
let
|
||||||
dSym = genSym(nskVar, ":default")
|
dSym = genSym(nskVar, ":default")
|
||||||
nextBitvectorIndex = defaultVectors.len
|
nextBitvectorIndex = defaultVectors.len
|
||||||
@ -1435,6 +1464,16 @@ macro setDefaultValue*(t: typedesc, field: untyped, value: typed): typed =
|
|||||||
defaultVectors[`defaultBitvectorProc`(`t`)].incl(getFieldIndex(`t`, `field`))
|
defaultVectors[`defaultBitvectorProc`(`t`)].incl(getFieldIndex(`t`, `field`))
|
||||||
|
|
||||||
macro ignoreInputKey*(t: typedesc, name: string{lit}): typed =
|
macro ignoreInputKey*(t: typedesc, name: string{lit}): typed =
|
||||||
|
## Tell NimYAML that when loading an object of type ``t``, any mapping key
|
||||||
|
## named ``name`` shall be ignored. This makes it possible to only load
|
||||||
|
## relevant parts of a YAML input and ignoring other portions of the input.
|
||||||
|
##
|
||||||
|
## Example usage:
|
||||||
|
##
|
||||||
|
## .. code-block::
|
||||||
|
## type MyObject = object
|
||||||
|
## a, b: string
|
||||||
|
## ignoreInputKey(MyObject, "c")
|
||||||
let nextIgnoredKeyList = ignoredKeyLists.len
|
let nextIgnoredKeyList = ignoredKeyLists.len
|
||||||
result = quote do:
|
result = quote do:
|
||||||
when not compiles(`ignoredKeyListProc`(`t`)):
|
when not compiles(`ignoredKeyListProc`(`t`)):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user