pkg

ctxcode/valk-yaml

YAML 1.2 for Valk: read and write documents and your own classes

github.com/ctxcode/valk-yaml ↗
0.3.0 latest Valk ≥ 0.9.1

Install

vman install github.com/ctxcode/valk-yaml

Adds the package to your valk.json. To allow only some versions, add them: vman install github.com/ctxcode/valk-yaml 0.3.x

Documentation

Namespaces: main


main

Functions for 'main'

// Reads one YAML document and returns it as a `json.Value`, so everything that works with JSON works with it.
+ fn decode(text: String) Value !Error
// Reads every document in a YAML text, the ones that `---` starts.
+ fn decode_all(text: String) Array[Value] !Error
// Reads one YAML document into a class or struct of your own.
+ fn decode_to[T](text: String) T !Error
// Writes a `json.Value` as a YAML document in block style.
+ fn encode(value: Value) String
// Writes several YAML documents, with `---` between them.
+ fn encode_all(documents: Array[Value]) String
// Writes a class or struct of your own as YAML, the way `json.from` would read it; a `time.DateTime` field is written as a date and time in UTC.
+ fn encode_of(value: $T) String

decode

Reads one YAML document and returns it as a json.Value, so everything that works with JSON
works with it.

Scalars follow YAML 1.2's core schema: null, ~ and nothing are null, true/false are
bools, 12, 0x1f and 0o17 are integers, 1.5, 1e3, .inf and .nan are floats, and
anything else, or anything quoted, is a string. Mapping keys are strings, as written. An
empty text is null; a text with more than one document throws, see decode_all.

let config = yaml.decode(fs.read("config.yaml") !!) ! panic("%{E.message}")
println(config["server"]["port"].int)

decode_all

Reads every document in a YAML text, the ones that --- starts.

each yaml.decode_all(text) ! panic("%{E.message}") as document {
    println(document["kind"].string)
}

decode_to

Reads one YAML document into a class or struct of your own.

Every field must be in the document, unless it is nullable or has a default. Field tags
control decoding: $tag(yaml: "name") (or `yaml: "name"` ) matches a renamed key,
$tag(yaml: "-") skips a field (using its default value), and $tag(omitempty) allows
missing or null values when the field has a default. A value that does not fit throws
.type, and its message names the key, such as 'server.port' is missing. A
time.DateTime field takes a date, or a date and time, converted to UTC.

class Settings {
    name: String
    port: uint (8080)
}

let settings = yaml.decode_to[Settings](text) ! panic("%{E.message}")

encode

Writes a json.Value as a YAML document in block style.

A string is quoted only when it has to be: when it would read back as something else, such
as "true", "1.0" or "null", or holds characters that mean something in YAML. A string of
several lines is written as a literal block (|). An empty mapping or sequence is written as
{} or [].

fs.write("config.yaml", yaml.encode(document)) ! panic("%{E.message}")

encode_all

Writes several YAML documents, with --- between them.

encode_of

Writes a class or struct of your own as YAML, the way json.from would read it; a
time.DateTime field is written as a date and time in UTC.

Field tags control encoding: $tag(yaml: "name") (or `yaml: "name"` ) renames a key,
$tag(yaml: "-") skips a field, and $tag(omitempty) omits zero, empty, or null values.

Classes for 'main'

// Thrown when text is not YAML, or does not fit what it is read into.
+ union Error {
    syntax(String, uint, uint)
    duplicate(String, uint, uint)
    type(String, uint, uint)
    limit(String, uint, uint)

    + get column: uint
    + get line: uint
    + get message: String
    + fn to_string() String
}

Error

Thrown when text is not YAML, or does not fit what it is read into.

Each variant carries (message, line, column). line and column say where the reader
stopped, counting from one, so the message can be shown against the file. They are 0 for
type errors from decode_to, whose message names the key instead.

  • syntax(message, line, column): the text does not read as YAML.
  • duplicate(message, line, column): a mapping has the same key twice.
  • type(message, line, column): a value is not what its tag or the type it is read into needs.
  • limit(message, line, column): aliases expand to too many values, or values nest too deep.