yaml #
Description
yaml provides a pure-V YAML reader and writer for common configuration files. It supports nested mappings, sequences, flow-style collections, block scalars, tree access through yaml.Any, and generic struct encode/decode.
The generic encode/decode path delegates to the main json module so it matches existing JSON field behavior, including @[json: 'name'].
Usage
import yaml
struct Config {
name string
enabled bool
ports []int
}
const config_text = '
name: app
enabled: true
ports:
- 8080
- 9090
'
fn main() {
doc := yaml.parse_text(config_text)!
assert doc.value('ports[1]').int() == 9090
config := yaml.decode[Config](config_text)!
assert config.name == 'app'
assert yaml.encode(config).contains('"name": "app"')
}
File helpers
import yaml
struct Config {
name string
}
fn main() {
config := yaml.decode_file[Config]('config.yml')!
yaml.encode_file('config.out.yml', config)!
}
Constants #
const null = Any(Null{})
null is an instance of Null, to ease comparisons with it.
fn decode #
fn decode[T](yaml_text string) !T
decode decodes YAML text into the target type T. The generic encode/decode path uses the main json module for field parity.
fn decode_file #
fn decode_file[T](path string) !T
decode_file decodes the YAML file at path into the target type T.
fn encode #
fn encode[T](value T) string
encode encodes the value value into a YAML string. The generic encode/decode path uses the main json module for field parity.
fn encode_file #
fn encode_file[T](path string, value T) !
encode_file encodes value as YAML and writes it to path.
fn parse_file #
fn parse_file(path string) !Doc
parse_file parses the YAML file at path.
fn parse_text #
fn parse_text(text string) !Doc
parse_text parses the YAML document provided in text.
type Any #
type Any = []Any | Null | bool | f64 | i64 | int | map[string]Any | string | u64
Any is the tree representation used by the YAML module.
fn (Any) str #
fn (a Any) str() string
str returns a display-friendly string form of Any.
fn (Any) string #
fn (a Any) string() string
string returns Any as a string when possible, or a YAML representation otherwise.
fn (Any) int #
fn (a Any) int() int
int returns Any as an int.
fn (Any) i64 #
fn (a Any) i64() i64
i64 returns Any as an i64.
fn (Any) u64 #
fn (a Any) u64() u64
u64 returns Any as a u64.
fn (Any) f64 #
fn (a Any) f64() f64
f64 returns Any as an f64.
fn (Any) bool #
fn (a Any) bool() bool
bool returns Any as a bool.
fn (Any) array #
fn (a Any) array() []Any
array returns Any as an array.
fn (Any) as_map #
fn (a Any) as_map() map[string]Any
as_map returns Any as a map.
fn (Any) default_to #
fn (a Any) default_to(value Any) Any
default_to returns value when a is Null.
fn (Any) value #
fn (a Any) value(key string) Any
value queries a value from the current node using dotted keys and array indices.
fn (Any) value_opt #
fn (a Any) value_opt(key string) !Any
value_opt queries a value from the current node and returns an error when missing.
fn (Any) to_json #
fn (a Any) to_json() string
to_json converts Any to JSON.
fn (Any) to_yaml #
fn (a Any) to_yaml() string
to_yaml converts Any to YAML.
fn ([]Any) value #
fn (a []Any) value(key string) Any
value queries a value from the array.
fn ([]Any) as_strings #
fn (a []Any) as_strings() []string
as_strings returns the contents of the array as []string.
fn ([]Any) to_yaml #
fn (a []Any) to_yaml() string
to_yaml converts a YAML array to YAML text.
fn (map[string]Any) value #
fn (m map[string]Any) value(key string) Any
value queries a value from the map.
fn (map[string]Any) as_strings #
fn (m map[string]Any) as_strings() map[string]string
as_strings returns the contents of the map as map[string]string.
fn (map[string]Any) to_yaml #
fn (m map[string]Any) to_yaml() string
to_yaml converts a YAML map to YAML text.
struct Doc #
struct Doc {
pub:
root Any
}
Doc is a parsed YAML document.
fn (Doc) decode #
fn (d Doc) decode[T]() !T
decode decodes the YAML document into the target type T.
fn (Doc) to_any #
fn (d Doc) to_any() Any
to_any converts the YAML document to yaml.Any.
fn (Doc) to_json #
fn (d Doc) to_json() string
to_json converts the YAML document to JSON.
fn (Doc) to_yaml #
fn (d Doc) to_yaml() string
to_yaml converts the YAML document back to YAML text.
fn (Doc) value #
fn (d Doc) value(key string) Any
value queries a value from the YAML document. key supports dotted keys and array indexing like servers[0].host.
fn (Doc) value_opt #
fn (d Doc) value_opt(key string) !Any
value_opt queries a value from the YAML document and returns an error when missing.
struct Null #
struct Null {}
Null is a simple representation of the YAML null value.