Saltar al contenido

YAML (std::yaml)

En esta página

std::yaml convierte entre cadenas YAML y mapas Zolo. Es la opción natural para leer configuraciones de Kubernetes, GitHub Actions, docker-compose y similares.

Parse

yaml.parse(cadena) convierte texto YAML en un mapa Zolo. Las listas con guiones se convierten en arrays:

Escalares y lista de etiquetas a partir de YAML inline.

01-parse.zolo
Playground
// Feature: yaml.parse — converts a YAML string into a map

// When to use: read YAML configurations (Kubernetes, GitHub Actions, etc.).


use std::yaml

let raw = "name: zolo\nversion: 0.1.0\ndebug: true"
let cfg = yaml.parse(raw)

print(cfg["name"])  // expected: zolo

print(cfg["version"])  // expected: 0.1.0

print(cfg["debug"])  // expected: true


// YAML also supports lists (with hyphens).

let raw2 = "tags:\n  - alpha\n  - beta\n  - gamma"
let cfg2 = yaml.parse(raw2)
print(cfg2["tags"][0])  // expected: alpha

print(cfg2["tags"][2])  // expected: gamma

Stringify

yaml.stringify(mapa) serializa un mapa a texto YAML; parse(stringify(x)) es un round-trip sin pérdida para valores escalares:

Serialización y round-trip de una configuración simple.

02-stringify.zolo
Playground
// Feature: yaml.stringify — serializes a map into a YAML string

// When to use: produce YAML configurations programmatically.


use std::yaml

let cfg = #{
  name: "zolo",
  version: "0.1.0",
  active: true,
}

let out = yaml.stringify(cfg)
print(out)

// expected (lines in `key: value` format):

// name: zolo

// version: 0.1.0

// active: true


// Round-trip returns to the same structure.

let back = yaml.parse(out)
print(back["name"])  // expected: zolo

print(back["active"])  // expected: true

Estructuras Anidadas

Las secciones YAML se convierten en sub-mapas; las listas de mapas (pipelines de CI) son totalmente compatibles:

Config jerárquica con server/database y pipeline con lista de pasos.

03-nested.zolo
Playground
// Feature: yaml — nested structures and lists inside maps

// When to use: typical hierarchical configs (CI, Helm charts, docker-compose).


use std::yaml

let raw = "server:\n  host: localhost\n  port: 8080\ndatabase:\n  url: postgres://...\n  pool: 10"

let cfg = yaml.parse(raw)
print(cfg["server"]["host"])  // expected: localhost

print(cfg["server"]["port"])  // expected: 8080

print(cfg["database"]["pool"])  // expected: 10


// List of maps (typical in pipelines).

let raw2 = "steps:\n  - name: build\n  - name: test\n  - name: deploy"
let pipeline = yaml.parse(raw2)
print(pipeline["steps"][0]["name"])  // expected: build

print(pipeline["steps"][2]["name"])  // expected: deploy

Buscar en Zolo

9 resultados

enespt-br