Available

amalgame-yaml

YAML 1.2 subset reader.

amc package add yaml GitHub ↗ ← Back to the ecosystem

Overview

YAML 1.2 reader in pure Amalgame, for simple configuration files. It reads block mappings and sequences, scalars and comments, and returns a YamlValue tree. It cannot write YAML.

Features

  • ✓Block mappings and sequences, nested by indentation, including lists of mappings
  • ✓Scalars: booleans, integers, floats, quoted or plain strings, null and ~
  • ✓Reading: Get(key), At(i), Count(), Has(key), Keys(), then AsString(), AsInt(), AsFloat(), AsBool()
  • ✓Missing key: Get returns a null value, no error
  • ✓Pure Amalgame, no C dependency

Limits

  • –No anchors or aliases (&name, *name).
  • –No flow style ([1, 2], {a: b}): the value stays a string.
  • –No multi-line strings (|, >), no multiple documents (---), no tags (!!str).
  • –No error reporting: a malformed line gives a wrong or incomplete result, with no message.

Example

After amc package add yaml, put this code in main.am and run amc run main.am.

import Amalgame.Formats.Yaml

public class Program {
    public static void Main() {
        let src: string = "server:\n  host: localhost\n  port: 8080\nfeatures:\n  - auth\n  - logs\n"
        let cfg: YamlValue = Yaml.Parse(src)   // or Yaml.Parse(File.ReadAll("config.yml"))

        let server: YamlValue = cfg.Get("server")
        let host: YamlValue = server.Get("host")
        let port: YamlValue = server.Get("port")
        Console.WriteLine(host.AsString())                   // localhost
        Console.WriteLine(String_FromInt(port.AsInt()))      // 8080

        let features: YamlValue = cfg.Get("features")
        let first: YamlValue = features.At(0)
        Console.WriteLine(String_FromInt(features.Count()))  // 2
        Console.WriteLine(first.AsString())                  // auth
    }
}

At a glance

Related packages