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 AliasesNotEnabledError ¶
type AliasesNotEnabledError struct{}
AliasesNotEnabledError is returned by SafeLoad for a document that dereferences an alias without WithAliases(true), matching Psych::AliasesNotEnabled.
func (*AliasesNotEnabledError) Error ¶
func (e *AliasesNotEnabledError) Error() string
type AnchorNotDefinedError ¶
type AnchorNotDefinedError struct {
// Anchor is the name the alias referenced.
Anchor string
}
AnchorNotDefinedError is returned for an alias naming an anchor the document never defines, matching Psych::AnchorNotDefined. The loader resolves such an alias to nil, which silently turns a broken reference into a null value.
func (*AnchorNotDefinedError) Error ¶
func (e *AnchorNotDefinedError) Error() string
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 DisallowedClassError ¶
type DisallowedClassError struct {
// Name is the class the document asked for.
Name string
}
DisallowedClassError is returned by SafeLoad for a class the document names and the options do not permit. Its message is Psych::DisallowedClass's (psych/exception.rb:23-27), so a host binding can surface it verbatim.
SafeLoad used to REWRITE an unpermitted object into its bare ivar mapping instead of reporting it. A caller that asked for a refusal got a Map, which is type confusion in place of an error -- and silent, because the document had loaded "successfully".
func (*DisallowedClassError) Error ¶
func (e *DisallowedClassError) Error() 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, mirroring the keyword options of Psych.dump / Psych.safe_load.
func WithAliases ¶
WithAliases mirrors Psych.safe_load(aliases: true), permitting a document to dereference `*alias` references. SafeLoad refuses them by default.
func WithPermittedClasses ¶
WithPermittedClasses adds names to SafeLoad's allow-list (Psych's permitted_classes:). Calling it with no names is meaningful and is NOT the same as not calling it: SafeLoad restricts either way, so both deny every class. Repeated calls accumulate.
func WithPermittedSymbols ¶
WithPermittedSymbols narrows which Symbol names SafeLoad may intern (Psych's permitted_symbols:). With no names it narrows nothing, as Psych's empty permitted_symbols does. It is additional to the class check: a document's symbols load only when "Symbol" is also permitted.
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.
Load applies NO allow-list: it is Psych.unsafe_load's counterpart and will materialise any class a document names. Use SafeLoad for a document you did not write.
func SafeLoad ¶
SafeLoad parses like Load and then enforces Psych.safe_load's restriction, returning a *DisallowedClassError for the first class the document names that opts do not permit.
SafeLoad ALWAYS restricts. With no options it permits no class at all, which is Psych.safe_load's own default (its signature is permitted_classes: [], and Psych::ClassLoader::Restricted seeds its allow-list from that list alone), and it is why this function has no "no policy" state to get wrong.
The restriction is wider than `!ruby/object:` tags, because Psych's class loader is: Symbol (in values and in keys), time.Time, Range, Regexp, and the class NAMED by a `!ruby/class` / `!ruby/module` tag all have to be permitted too. Plain scalars and the Map / sequence containers never consult it, which is precisely why psych.rb can document String / Integer / Array / Hash and friends as permitted by default while Restricted holds none of their names.
