Fork me on GitHub
NimYAML Home Testing Ground Docs: Overview Serialization Modules

Module yaml.taglib

The taglib API enables you to query real names of tags emitted by the parser and create own tags. It also enables you to define tags for types used with the serialization API.

Imports

tables, macros, hashes, strutils

Types

TagId = distinct int

A TagId identifies a tag URI, like for example "tag:yaml.org,2002:str". The URI corresponding to a TagId can be queried from the TagLibrary which was used to create this TagId; e.g. when you parse a YAML character stream, the TagLibrary of the parser is the one which generates the resulting TagId s.

URI strings are mapped to TagId s for efficiency reasons (you do not need to compare strings every time) and to be able to discover unknown tag URIs early in the parsing process.

  Source
TagLibrary = ref object
  tags*: Table[string, TagId]
  nextCustomTagId*: TagId
  tagHandles: Table[string, string]

A TagLibrary maps tag URIs to TagId s.

When YamlParser encounters tags not existing in the tag library, it will use registerUri to add the tag to the library.

You can base your tag library on common tag libraries by initializing them with initFailsafeTagLibrary, initCoreTagLibrary or initExtendedTagLibrary.

  Source

Vars

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 serializable.

  Source

Consts

yTagExclamationMark: TagId = 0
! non-specific tag   Source
yTagQuestionMark: TagId = 1
? non-specific tag   Source
yTagString: TagId = 2
!!str tag   Source
yTagSequence: TagId = 3
!!seq tag   Source
yTagMapping: TagId = 4
!!map tag   Source
yTagNull: TagId = 5
!!null tag   Source
yTagBoolean: TagId = 6
!!bool tag   Source
yTagInteger: TagId = 7
!!int tag   Source
yTagFloat: TagId = 8
!!float tag   Source
yTagOrderedMap: TagId = 9
!!omap tag   Source
yTagPairs: TagId = 10
!!pairs tag   Source
yTagSet: TagId = 11
!!set tag   Source
yTagBinary: TagId = 12
!!binary tag   Source
yTagMerge: TagId = 13
!!merge tag   Source
yTagTimestamp: TagId = 14
!!timestamp tag   Source
yTagValue: TagId = 15
!!value tag   Source
yTagYaml: TagId = 16
!!yaml tag   Source
yTagNimField: TagId = 100
This tag is used in serialization for the name of a field of an object. It may contain any string scalar that is a valid Nim symbol.   Source
yTagNimNilString: TagId = 101
for strings that are nil   Source
yTagNimNilSeq: TagId = 102
for seqs that are nil. This tag is used regardless of the seq's generic type parameter.   Source
yFirstStaticTagId: TagId = 1000
The first TagId assigned by the setTagId templates.   Source
yFirstCustomTagId: TagId = 10000
The first TagId which should be assigned to an URI that does not exist in the YamlTagLibrary which is used for parsing.   Source
yamlTagRepositoryPrefix = "tag:yaml.org,2002:"
  Source
nimyamlTagRepositoryPrefix = "tag:nimyaml.org,2016:"
  Source
yTagNimChar = 1000
  Source
yTagNimInt8 = 1001
  Source
yTagNimInt16 = 1002
  Source
yTagNimInt32 = 1003
  Source
yTagNimInt64 = 1004
  Source
yTagNimUInt8 = 1005
  Source
yTagNimUInt16 = 1006
  Source
yTagNimUInt32 = 1007
  Source
yTagNimUInt64 = 1008
  Source
yTagNimFloat32 = 1009
  Source
yTagNimFloat64 = 1010
  Source

Procs

proc `==`(left, right: TagId): bool {.
borrow
.}
  Source
proc hash(id: TagId): Hash {.
borrow
.}
  Source
proc `$`(id: TagId): string {.
raises: [], tags: []
.}
  Source
proc initTagLibrary(): TagLibrary {.
raises: [], tags: []
.}
initializes the tags table and sets nextCustomTagId to yFirstCustomTagId.   Source
proc registerUri(tagLib: TagLibrary; uri: string): TagId {.
raises: [], tags: []
.}
registers a custom tag URI with a TagLibrary. The URI will get the TagId nextCustomTagId, which will be incremented.   Source
proc uri(tagLib: TagLibrary; id: TagId): string {.
raises: [KeyError], tags: []
.}
retrieve the URI a TagId maps to.   Source
proc initFailsafeTagLibrary(): TagLibrary {.
raises: [], tags: []
.}
Contains only:
  • !
  • ?
  • !!str
  • !!map
  • !!seq
  Source
proc initCoreTagLibrary(): TagLibrary {.
raises: [], tags: []
.}
Contains everything in initFailsafeTagLibrary plus:
  • !!null
  • !!bool
  • !!int
  • !!float
  Source
proc initExtendedTagLibrary(): TagLibrary {.
raises: [], tags: []
.}
Contains everything from initCoreTagLibrary plus:
  • !!omap
  • !!pairs
  • !!set
  • !!binary
  • !!merge
  • !!timestamp
  • !!value
  • !!yaml
  Source
proc initSerializationTagLibrary(): TagLibrary {.
raises: [], tags: []
.}
  Source
proc yamlTag(T148327: typedesc[char]): TagId {.
inline, raises: []
.}
autogenerated   Source
proc yamlTag(T148769: typedesc[int8]): TagId {.
inline, raises: []
.}
autogenerated   Source
proc yamlTag(T149169: typedesc[int16]): TagId {.
inline, raises: []
.}
autogenerated   Source
proc yamlTag(T149569: typedesc[int32]): TagId {.
inline, raises: []
.}
autogenerated   Source
proc yamlTag(T149969: typedesc[int64]): TagId {.
inline, raises: []
.}
autogenerated   Source
proc yamlTag(T150369: typedesc[uint8]): TagId {.
inline, raises: []
.}
autogenerated   Source
proc yamlTag(T150769: typedesc[uint16]): TagId {.
inline, raises: []
.}
autogenerated   Source
proc yamlTag(T151169: typedesc[uint32]): TagId {.
inline, raises: []
.}
autogenerated   Source
proc yamlTag(T151569: typedesc[uint64]): TagId {.
inline, raises: []
.}
autogenerated   Source
proc yamlTag(T151969: typedesc[float32]): TagId {.
inline, raises: []
.}
autogenerated   Source
proc yamlTag(T152369: typedesc[float64]): TagId {.
inline, raises: []
.}
autogenerated   Source
proc registerHandle(tagLib: TagLibrary; handle, prefix: string) {.
raises: [], tags: []
.}
Registers a handle for a prefix. When presenting any tag that starts with this prefix, the handle is used instead. Also causes the presenter to output a TAG directive for the handle.   Source
proc searchHandle(tagLib: TagLibrary; tag: string): tuple[handle: string, len: int] {.
raises: [], tags: []
.}
search in the registered tag handles for one whose prefix matches the start of the given tag. If multiple registered handles match, the one with the longest prefix is returned. If no registered handle matches, (nil, 0) is returned.   Source
proc nimTag(suffix: string): string {.
raises: [], tags: []
.}
prepends NimYAML's tag repository prefix to the given suffix. For example, nimTag("system:char") yields "tag:nimyaml.org,2016:system:char".   Source

Iterators

iterator handles(tagLib: TagLibrary): tuple[prefix, handle: string] {.
raises: [], tags: []
.}
iterate over registered tag handles that may be used as shortcuts (e.g. !n! for tag:nimyaml.org,2016:)   Source

Templates

template setTagUri(t: typedesc; uri: string): typed
Associate the given uri with a certain type. This uri is used as YAML tag when loading and dumping values of this type.   Source
template setTagUri(t: typedesc; uri: string; idName: untyped): typed
Like setTagUri, but lets you choose a symbol for the TagId of the uri. This is only necessary if you want to implement serialization / construction yourself.   Source