yaml

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 18, 2026 License: MIT Imports: 9 Imported by: 0

README

📄 yaml — YAML for Starlark

Go Reference license codecov binary footprint

Decode and encode YAML from Starlark, built on gopkg.in/yaml.v3.

Decoding is hardened: input size, nesting depth, and total node count are bounded; parse panics become errors; and YAML's bare-timestamp footgun is tamed.

Where this fits. starpkg is support for necessary local operations plus simple abstractions over common online services, for ease of use. yaml is a local capability — a pure in-process text↔value codec with no network, filesystem, or external service involved.

For the complete per-builtin reference — signatures, parameters, returns, errors, examples — and the configuration accessors, see docs/API.md.

Installation

go get github.com/starpkg/yaml

Quickstart

The host constructs a Module and hands its LoadModule() loader to a Starlet machine; the script reaches it through load("yaml", …):

package main

import (
    "github.com/1set/starlet"
    "github.com/starpkg/yaml"
)

func main() {
    m := yaml.NewModule()
    interp := starlet.NewWithLoaders(nil, nil, starlet.ModuleLoaderMap{
        yaml.ModuleName: m.LoadModule(),
    })
    _, _ = interp.RunScript([]byte(`
load("yaml", "decode", "encode")
print(decode("a: 1")["a"])     # => 1
print(encode({"b": [1, 2]}))   # => "b:\n    - 1\n    - 2\n"
`), nil)
}

NewModule() reads the decode caps (max_depth / max_nodes / max_input_bytes) from defaults or the YAML_* environment variables. ModuleName is the constant "yaml".

Starlark API at a glance

Top-level builtins (load("yaml", …)):

  • decode(text) — parse a YAML document (string or bytes) into Starlark values.
  • encode(value) — serialize a Starlark value to a YAML string.

There are no object methods: decode returns plain Starlark containers/scalars (dict / list / str / int / float / bool / None), and encode returns a str. Bare timestamps decode to RFC 3339 strings; anchors, aliases, and merge keys (<<) resolve through decode.

See docs/API.md for the full signatures, return values, errors, hardening notes, and examples of both builtins.

Configuration

The module's three decode caps (max_depth, max_nodes, max_input_bytes) are configured via environment variables (YAML_*) or per-option get_<key> / set_<key> accessor builtins. See the Configuration section of docs/API.md for the full option table, defaults, and accessors.

License

MIT

Documentation

Overview

Package yaml provides a Starlark module for decoding and encoding YAML.

Decoding is hardened against malicious documents: the input size, nesting depth, and total node count are all bounded (capwalk), and parse panics are recovered into errors. YAML's bare-timestamp footgun is tamed — values that the parser would turn into a Go time.Time (e.g. `2020-01-01`) are surfaced as RFC 3339 strings, never as a surprise opaque value.

Index

Constants

View Source
const ModuleName = "yaml"

ModuleName is the name used in Starlark's load() for this module.

Variables

This section is empty.

Functions

This section is empty.

Types

type Module

type Module struct {
	// contains filtered or unexported fields
}

Module wraps a ConfigurableModule with YAML functions.

func NewModule

func NewModule() *Module

NewModule creates a new Module with default configuration.

func (*Module) LoadModule

func (m *Module) LoadModule() starlet.ModuleLoader

LoadModule returns the Starlark module loader.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL