You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import kdl
var doc = parseKdl("""// Nodes can be separated into multiple linestitle \ "Some title" // Files must be utf8 encoded! smile (emoji)"๐" { upside-down (emoji)"๐" } // Instead of anonymous nodes, nodes and properties can be wrapped // in "" for arbitrary node names. "!@#$@$%Q#$%~@!40" "1.2.3" "!!!!!"=true // The following is a legal bare identifier: foo123~!@#$%^&*.:'|?+ "weeee" // And you can also use unicode! ใใผใ ใๅๅ="โ(๏พใฎ๏พโ)" // kdl specifically allows properties and values to be // interspersed with each other, much like CLI commands. foo bar=true "baz" quux=false 1 2 3""") # You can also read files using parseKdlFile("file.kdl")# Nodes are represented like:# type KdlNode* = object# tag*: Option[string]# name*: string# args*: seq[KdlVal]# props*: Table[string, KdlVal]# children*: seq[KdlNode]
assert doc[0].args[0].isString() # title "Some title"
assert doc[1].args[0] =="๐"# smile node
assert doc[1].args[0].tag.isSome and doc[1].args[0].tag.get =="emoji"# Type annotation
assert doc[1].children[0].args[0] =="๐"# smile node's upside-down child
assert doc[2].name =="!@#$@$%Q#$%~@!40"
assert doc[^1]["quux"] ==false
doc[0].args[0].setString("New title")
# toKdlNode is a macro that facilitates the creation of `KdlNode`s, there's also toKdl (to create documents) and toKdlVal
doc[1].children[0] = toKdlNode: sunglasses("๐"[emoji], 3.14)
assert $doc[1].children[0] =="\"sunglasses\" (\"emoji\")\"๐\" 3.14"
assert doc[1].children[0].args[1].get(uint8) ==3u8# Converts 3.14 into an uint8
doc[^1]["bar"].setTo(false) # Same as setBool(false)
writeFile("doc.kdl", doc)