yaml

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2026 License: MIT Imports: 14 Imported by: 1 module, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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 caps (max_depth / max_nodes / max_input_bytes / max_time / max_encode_depth) 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 decode caps (max_depth, max_nodes, max_input_bytes) plus two host-only DoS levers — max_time (a per-decode wall-clock bound; max_input_bytes bounds size but not yaml.v3's super-linear parse time, and a merge-key chain under the byte cap can be O(n²)) and max_encode_depth (the encode stack-safety fence — a value nested too deep would overflow the Go stack in dataconv/yaml.v3, so it's rejected first) — are configured via environment variables (YAML_*) or the accessor builtins. The host-only levers have no set_* (a script cannot disable them). See the Configuration section of docs/API.md for the full option table, defaults, and accessors.

License

MIT

Integer decoding and encoding preserve arbitrary precision, including aliases, merge keys, nested containers, and round trips. Decimal/hex/binary/octal integer scalars become Starlark integers; quoted values and explicit floats retain their types. Colliding stringified keys are rejected. See the API reference.

Documentation

Overview

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

Decoding is hardened against malicious documents: the input size, nesting depth, total node count, and parse wall-clock time are all bounded (capwalk + a decode deadline for yaml.v3's super-linear parse), and parse panics are recovered into errors. Encoding is fenced by max_depth so a deeply-nested value can't drive marshalling into a fatal stack overflow. 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
⌂ 换网址