Pular para o conteúdo

YAML (std::yaml)

Nesta página

std::yaml converte entre strings YAML e mapas Zolo. É a escolha natural para ler configurações do Kubernetes, GitHub Actions, docker-compose e similares.

Parse

yaml.parse(string) converte texto YAML em mapa Zolo. Listas com hífens tornam-se arrays:

Scalares e lista de tags 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 um mapa para texto YAML; parse(stringify(x)) é um round-trip sem perda para valores scalares:

Serialização e round-trip de uma configuração simples.

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

Estruturas Aninhadas

Seções YAML viram sub-mapas; listas de mapas (pipelines de CI) são totalmente suportadas:

Config hierárquica com server/database e pipeline com lista de passos.

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 no Zolo

9 resultados

enespt-br