Documentation
¶
Overview ¶
Package yaml is a pure-Go (CGO-free) Psych-compatible YAML emitter and loader for the Ruby value model. It is the deterministic, interpreter-independent core of MRI 4.0.5's Psych: Dump renders a tree of Ruby values to a Psych-compatible document and Load parses such a document back, so Load(Dump(x)) round-trips the structures Ruby programs (and Puppet's state/run-summary files) persist — without any Ruby runtime.
Ruby value model ¶
A Ruby value is represented by an [any] drawn from a small, fixed set of Go types so a host (such as go-embedded-ruby) can map its own object graph to and from this package:
Ruby Go (in) Go (out, from Load) ---- ------- ------------------- nil nil nil true / false bool bool Integer int, int64, *big.Int int64 or *big.Int Float float64, float32 float64 String string string Symbol Symbol Symbol Array []any, []Value []any Hash *Map (ordered), map[...]any *Map (insertion order) Time time.Time time.Time Range *Range *Range Object *Object (tag + ivars) *Object Class / Module Class / Module Class / Module Regexp *Regexp *Regexp
Dump also accepts a plain Go map[string]any / map[Symbol]any (sorted by key for determinism); Load always yields an ordered *Map for a mapping so key order is preserved on round-trip.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Dump ¶
Dump serialises a Ruby value to a Psych-compatible YAML document string, matching Psych.dump / Object#to_yaml. v is drawn from the package value model (see the package doc); a value outside that model returns an error rather than panicking. The opts are accepted for Psych parity and do not affect emission.
Types ¶
type Class ¶
type Class string
Class is a Ruby Class reference, emitted/loaded as the `!ruby/class 'Name'` tag. Its value is the fully-qualified class name (e.g. "String").
type Map ¶
type Map struct {
// contains filtered or unexported fields
}
Map is an insertion-ordered Ruby Hash. Load returns mappings as *Map so key order round-trips; Dump accepts *Map, a plain Go map (emitted in sorted key order), or a []Pair via NewMap.
type Module ¶
type Module string
Module is a Ruby Module reference, emitted/loaded as `!ruby/module 'Name'`.
type Object ¶
type Object struct {
Class string
IVars map[string]Value
// Order, when non-nil, fixes the emission order of IVars keys; names absent
// from Order are appended in lexicographic order. When nil, all keys are
// emitted in lexicographic order.
Order []string
}
Object is a generic tagged Ruby object — anything Psych writes as a `!ruby/object:ClassName` mapping of instance variables. Class is the tag's class name ("" or "Object" emits the bare `!ruby/object`). IVars holds the instance variables by bare name (no leading "@"); Dump orders them by Order when set, else lexicographically (documented, deterministic). A host binds its own object instances to and from this shape.
type Option ¶
type Option func(*options)
Option configures Dump / SafeLoad. Options are accepted for parity with Psych.dump / Psych.safe_load (which take keyword options); this loader is safe by construction — it instantiates only the *Object shapes named by the `!ruby/object:` tags actually present and never evaluates anything — so the options are tolerated and currently advisory. They exist so callers (and a host binding) can pass Psych-style configuration without breaking.
func WithAliases ¶
WithAliases mirrors Psych.safe_load(aliases: true). Aliases are resolved regardless; the option is accepted for parity.
func WithPermittedClasses ¶
WithPermittedClasses restricts SafeLoad to materialise `!ruby/object:` tags only for the named classes (Psych's permitted_classes:). Other tagged mappings load as their plain ordered mapping.
type Range ¶
Range is a Ruby Range, emitted/loaded as the `!ruby/range` mapping with begin / end / excl members. A nil Begin or End models a beginless / endless range.
type Regexp ¶
Regexp is a Ruby Regexp, emitted/loaded inline as `!ruby/regexp /source/flags` (Psych's representation). Flags is the trailing option letters (e.g. "mix").
type Symbol ¶
type Symbol string
Symbol is a Ruby Symbol (`:name`). Dump emits the bare `:name` form (or the double-quoted `:"a\nb"` form when the name carries control characters), and Load maps both `:name` scalars and the `!ruby/symbol` tag back to a Symbol.
type SyntaxError ¶
type SyntaxError struct{ Message string }
SyntaxError is the error type Load / SafeLoad return for a malformed document (mirroring Psych::SyntaxError). It carries a human-readable message.
type Value ¶
type Value = any
Value is the interface satisfied by every Ruby value this package handles. It is purely documentary — the public API uses any — but a host may use it to constrain its own adapters.
func Load ¶
Load parses a Psych-compatible YAML document into a Ruby value, matching Psych.load / YAML.load. Mappings load as an ordered *Map (key order preserved), sequences as []any, and the Psych tags into the package's Symbol / Object / Range / Class / Module / Regexp / time.Time shapes. A blank document loads as nil.
