NimYAML/yaml/annotations.nim

69 lines
2.2 KiB
Nim

# NimYAML - YAML implementation in Nim
# (c) Copyright 2016-2020 Felix Krause
#
# See the file "copying.txt", included in this
# distribution, for details about the copyright.
## =======================
## Module yaml.annotations
## =======================
##
## This module provides annotations for object fields that customize
## (de)serialization behavior of those fields.
template defaultVal*(value : typed) {.pragma.}
## This annotation can be assigned to an object field. During deserialization,
## if no value for this field is given, the `value` parameter of this
## annotation is used as value.
##
## Example usage:
##
## .. code-block::
## type MyObject = object
## a {.defaultVal: "foo".}: string
## c {.defaultVal: (1,2).}: tuple[x, y: int]
template transient*() {.pragma.}
## This annotation can be assigned to an object field. Any object field
## carrying this annotation will not be serialized to YAML and cannot be given
## a value when deserializing. Giving a value for this field during
## deserialization is an error.
##
## Example usage:
##
## .. code-block::
## type MyObject = object
## a, b: string
## c: int
## markAsTransient(MyObject, a)
## markAsTransient(MyObject, c)
template ignore*(keys : openarray[string]) {.pragma.}
## This annotation can be assigned to an object type. All keys with the given
## names in the input YAML mapping will be ignored when deserializing a value
## of this type. This can be used to ignore parts of the YAML structure.
##
## Example usage:
##
## .. code-block::
## type MyObject {.ignore("c").} = object
## a, b: string
template implicit*() {.pragma.}
## This annotation declares a variant object type as implicit.
## This requires the type to consist of nothing but a case expression and each
## branch of the case expression containing exactly one field - with the
## exception that one branch may contain zero fields.
##
## Example usage:
##
## .. code-block::
## ContainerKind = enum
## ckString, ckInt
##
## type MyObject {.implicit.} = object
## case kind: ContainerKind
## of ckString:
## strVal: string
## of ckInt:
## intVal: int