src/yanyl

Search:
Group by:
Source   Edit  

Types

YNode = object
  case kind*: YNodeKind
  of ynNil:
    nil
  of ynString:
    strVal*: string
  of ynList:
    listVal*: seq[YNode]
  of ynMap:
    mapVal*: TableRef[string, YNode]
Source   Edit  
YNodeKind = enum
  ynString, ynList, ynMap, ynNil
Source   Edit  

Procs

proc `==`(a: YNode; b: YNode): bool {.noSideEffect, ...raises: [Exception],
                                      tags: [RootEffect], forbids: [].}
Compare two yaml documents for equality Source   Edit  
proc elems(n: YNode): seq[YNode] {....raises: [], tags: [], forbids: [].}

Get the list value of the node

Throws if n is not a list

Example:

let l = newYList(@[newYNil(),
                   newYString("abc")])
let items = l.elems()
doAssert len(items) == 2
doAssert items[0].kind == ynNil
doAssert items[1].strVal == "abc"
Source   Edit  
proc get(n: YNode; k: string): YNode {....raises: [KeyError], tags: [], forbids: [].}
Source   Edit  
proc get[T](n: YNode; k: string; t: typedesc[Option[T]]): Option[T]
Source   Edit  
proc get[T](n: YNode; k: string; t: typedesc[seq[T]]): seq[T]
Source   Edit  
proc get[T](n: YNode; k: string; t: typedesc[T]): T
Source   Edit  
proc getStr(n: YNode; k: string): string {....raises: [KeyError], tags: [],
    forbids: [].}
Source   Edit  
proc loadNode(s: string | Stream): YNode
Load a YNode from a YAML string or stream

Example:

let sample = """
      s: x
      i: 3
      f: 0.32
    """
let n = sample.loadNode()
doAssert n.kind == ynMap
doAssert n.get("s", string) == "x"
doAssert n.get("i", int) == 3
doAssert n.get("f", float) == 0.32
Source   Edit  
proc newYList(elems: seq[string]): YNode {....raises: [], tags: [], forbids: [].}
Source   Edit  
proc newYList(elems: seq[YNode]): YNode {....raises: [], tags: [], forbids: [].}

Example:

let list = @[newYNil(), newYNil()]
let node = newYList(list)
doAssert node.kind == ynList
doAssert node.listVal.len == 2
Source   Edit  
proc newYMap(a: openArray[(string, YNode)]): YNode {....raises: [], tags: [],
    forbids: [].}
Source   Edit  
proc newYMap(t: TableRef[string, YNode]): YNode {....raises: [], tags: [],
    forbids: [].}
Source   Edit  
proc newYMapRemoveNils(a: openArray[(string, YNode)]): YNode {....raises: [],
    tags: [], forbids: [].}

Example:

import std/tables
let node = newYMapRemoveNils(
            [("a", newYString("astring")), 
             ("b", newYNil())])
doAssert node.kind == ynMap
doAssert node.mapVal.len == 1
Source   Edit  
proc newYNil(): YNode {....raises: [], tags: [], forbids: [].}
Source   Edit  
proc newYString(s: string): YNode {....raises: [], tags: [], forbids: [].}
Source   Edit  
proc ofYaml(n: YNode; t: typedesc[bool]): bool

Example:

doAssert ofYaml(newYString("true"), bool) == true
doAssert ofYaml(newYString("false"), bool) == false
Source   Edit  
proc ofYaml(n: YNode; t: typedesc[char]): char
Source   Edit  
proc ofYaml(n: YNode; t: typedesc[float]): float

Example:

let n = newYString("3.14")
doAssert ofYaml(n, float) == 3.14
Source   Edit  
proc ofYaml(n: YNode; t: typedesc[int]): int

Example:

let n = newYString("8675309")
doAssert ofYaml(n, int) == 8675309
Source   Edit  
proc ofYaml(n: YNode; t: typedesc[string]): string

Example:

let n = newYString("yep")
doAssert ofYaml(n, string) == "yep"
Source   Edit  
proc ofYaml[T](n: YNode; t: typedesc[Option[T]]): Option[T]

Example:

import std/options

let n1 = newYNil()
let o1 = ofYaml(n1, Option[string])
doAssert o1.isNone
let n2 = newYString("heyo")
let o2 = ofYaml(n2, Option[string])
doAssert o2.isSome
doAssert o2.get() == "heyo"
Source   Edit  
proc ofYaml[T](n: YNode; t: typedesc[ref T]): ref T
Source   Edit  
proc ofYaml[T](n: YNode; t: typedesc[seq[T]]): seq[T]

Example:

let l = newYList(@[
  newYString("1"),
  newYString("2"),
  newYString("3")
])
let res = ofYaml(l, seq[int])
doAssert res.len == 3
doAssert res[0] == 1
doAssert res[1] == 2
doAssert res[2] == 3
Source   Edit  
proc ofYaml[T](n: YNode; t: typedesc[Table[string, T]]): Table[string, T]
Source   Edit  
proc ofYaml[T](n: YNode; t: typedesc[TableRef[string, T]]): TableRef[string, T]
Source   Edit  
proc ofYamlStr[T](s: string; t: typedesc[T]): T
Source   Edit  
proc str(n: YNode): string {....raises: [], tags: [], forbids: [].}

Get the string value of the node

Throws if n is not a string

Example:

let s = newYString("abc")
doAssert s.str() == "abc"
Source   Edit  
proc toChar(n: YNode): char {....raises: [ValueError], tags: [], forbids: [].}

Get the char value of the node

Throws if n is not a string

Example:

let n = newYString("8")
doAssert n.toChar() == '8'
Source   Edit  
proc toFloat(n: YNode): float {....raises: [ValueError], tags: [], forbids: [].}

Get the float value of the node

Throws if n is not a string

Example:

let n = newYString("3.14")
doAssert n.toFloat() == 3.14
Source   Edit  
proc toInt(n: YNode): int {....raises: [ValueError], tags: [], forbids: [].}

Get the int value of the node

Throws if n is not a string

Example:

let n = newYString("123")
doAssert n.toInt() == 123
Source   Edit  
proc toString(n: YNode; indentLevel = 0): string {....raises: [ValueError],
    tags: [], forbids: [].}
Convert a YNode to a YAML string Source   Edit  
proc toYaml(b: bool): YNode {....raises: [], tags: [], forbids: [].}
Source   Edit  
proc toYaml(c: char): YNode {....raises: [], tags: [], forbids: [].}
Source   Edit  
proc toYaml(f: float): YNode {....raises: [], tags: [], forbids: [].}
Source   Edit  
proc toYaml(i: int): YNode {....raises: [], tags: [], forbids: [].}
Source   Edit  
proc toYaml(s: string): YNode {....raises: [], tags: [], forbids: [].}
Source   Edit  
proc toYaml[T](l: seq[T]): YNode
Source   Edit  
proc toYaml[T](o: Option[T]): YNode
Source   Edit  
proc toYaml[T](t: Table[string, T]): YNode
Source   Edit  
proc toYaml[T](t: TableRef[string, T]): YNode
Source   Edit  
proc toYaml[T](x: ref T): YNode
Source   Edit  
proc toYamlStr[T](x: T): string
Source   Edit  

Macros

macro deriveYaml(v: typed)
Generate ofYaml and toYaml procs for a type

Example:

type
  Obj = object
    i: int
    s: string

deriveYaml Obj

var sample: string = """
    i: 99
    s: hello world
    """
var o: Obj = ofYamlStr(sample, Obj)
doAssert o.i == 99
doAssert o.s == "hello world"
Source   Edit  
macro deriveYamls(body: untyped)
Derive yamls for multiple types

Example:

type
  Owner = ref object of RootObj
    name: string
  Pet = ref object of RootObj
    name: string
    kind: string
    owner: Owner

deriveYamls:
  Owner
  Pet

let sample = """
      name: Garfield
      kind: cat
      owner:
          name: J. Arbuckle
    """
let garf = ofYamlStr(sample, Pet)
doAssert garf.name == "Garfield"
doAssert garf.kind == "cat"
doAssert garf.owner.name == "J. Arbuckle"
Source   Edit  

Templates

template assertYList(n: YNode)
Source   Edit  
template assertYMap(n: YNode)
Source   Edit  
template assertYString(n: YNode)
Source   Edit  
template expectYList(n: YNode; body: untyped) {....deprecated.}
Deprecated
Source   Edit  
template expectYMap(n: YNode; body: untyped) {....deprecated.}
Deprecated
Source   Edit  
template expectYString(n: YNode; body: untyped) {....deprecated.}
Deprecated
Source   Edit