Help us shape the future of Go! Take the 2026 Go Developer Survey

tree

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2026 License: BSD-3-Clause Imports: 15 Imported by: 0

Documentation

Overview

Package tree is the format-neutral document a Compose file is read into: mappings (ordered, unique keys), sequences and scalars, each carrying the file, line and column it came from. The YAML, JSON and HCL2 readers all produce it, so interpolation, merging and decoding are written once.

Every reader is bounded (Limits): a Compose file is untrusted input.

Index

Constants

This section is empty.

Variables

View Source
var DefaultLimits = Limits{MaxBytes: 4 << 20, MaxDepth: 64, MaxNodes: 1 << 18, MaxString: 1 << 20}

DefaultLimits are the bounds used when a field is zero.

Functions

func Errorf

func Errorf(p Pos, format string, a ...any) error

Errorf returns an *Error at p.

func Walk

func Walk(n *Node, fn func(*Node) bool)

Walk calls fn on n and every node below it, depth first; fn returning false skips a node's children.

Types

type Entry

type Entry struct {
	Key    string
	KeyPos Pos
	Value  *Node
}

Entry is one key of a mapping.

type Error

type Error struct {
	Pos Pos
	Msg string
}

Error is a problem at a place in a file.

func (*Error) Error

func (e *Error) Error() string

type HCLOptions

type HCLOptions struct {
	// Variables are the values of var.NAME: the project's environment
	// (the explicit environment, then .env). A `variable "NAME"` block's
	// default applies when NAME is not among them.
	Variables map[string]string
}

HCLOptions are what an HCL2 Compose file may refer to.

type Kind

type Kind uint8

Kind is a node's type.

const (
	Null Kind = iota
	String
	Int
	Float
	Bool
	Map
	Seq
)

The kinds of node.

func (Kind) String

func (k Kind) String() string

type Limits

type Limits struct {
	// MaxBytes is the largest file (default 4 MiB).
	MaxBytes int
	// MaxDepth is the deepest nesting of mappings and sequences (64).
	MaxDepth int
	// MaxNodes is the most nodes a file may expand to, aliases counted
	// at every use (262144): what stops a YAML "billion laughs".
	MaxNodes int
	// MaxString is the longest scalar, also after interpolation (1 MiB).
	MaxString int
}

Limits bound what a reader accepts.

func (Limits) WithDefaults

func (l Limits) WithDefaults() Limits

WithDefaults fills the zero fields.

type Node

type Node struct {
	Kind Kind
	Pos  Pos
	Tag  Tag
	// Text is a scalar's value: the string itself, a number's literal
	// (base 10 after reading), "true" or "false".
	Text    string
	Entries []Entry // Map
	Items   []*Node // Seq
}

Node is a value of a Compose document.

func NewMap

func NewMap(p Pos) *Node

NewMap returns an empty mapping.

func NewSeq

func NewSeq(p Pos, items ...*Node) *Node

NewSeq returns a sequence of items.

func NewString

func NewString(s string, p Pos) *Node

NewString returns a string scalar.

func Number

func Number(f float64, p Pos) *Node

Number makes a float node, or an integer one when the value is whole: HCL2 has one number type, so 1.0 and 1 must be the same value in all three formats.

func ParseHCL

func ParseHCL(file string, data []byte, lim Limits, opt HCLOptions) (*Node, error)

ParseHCL reads an HCL2 native-syntax Compose file into a tree.

Expressions are evaluated in a static context: the only variable is `var` (HCLOptions.Variables), there are no functions, and `for` expressions and directives are refused (they multiply sizes). A quoted string is a template: a literal "${" is written "$${". Values are final: Compose's own `${VAR}` interpolation does not apply to HCL files.

func ParseJSON

func ParseJSON(file string, data []byte, lim Limits) (*Node, error)

ParseJSON reads a JSON document (RFC 8259) into a tree, keeping key order and positions, refusing duplicate keys and trailing content.

func ParseYAML

func ParseYAML(file string, data []byte, lim Limits) (*Node, error)

ParseYAML reads one YAML 1.2 document into a tree.

Anchors and aliases are resolved here (Compose Specification, fragments: before interpolation), with every use of an alias counted against Limits.MaxNodes. Merge keys (`<<`, the YAML 1.1 merge type that Compose files rely on) take a mapping or a sequence of mappings; keys of the mapping itself win, then earlier sources over later ones. Keys must be unique (YAML 1.2 §3.2.1.3). The tags `!reset` and `!override` are kept for the merge; any other non-standard tag is refused.

func (*Node) AsBool

func (n *Node) AsBool() (bool, error)

AsBool reads a boolean field: a boolean, or a string spelling one (the value of an interpolated variable).

func (*Node) AsInt

func (n *Node) AsInt() (int64, error)

AsInt reads an integer field: an integer, or a string holding one.

func (*Node) AsString

func (n *Node) AsString() (string, error)

AsString reads a string field: a string, or a number or boolean written where a string is expected (as YAML users do: `version: 3`).

func (*Node) Clone

func (n *Node) Clone() *Node

Clone returns a deep copy.

func (*Node) Delete

func (n *Node) Delete(key string)

Delete removes key from a mapping.

func (*Node) Get

func (n *Node) Get(key string) *Node

Get returns a mapping's value for key, or nil.

func (*Node) Has

func (n *Node) Has(key string) bool

Has says whether a mapping has key (even with a null value).

func (*Node) Index

func (n *Node) Index(key string) int

Index returns the position of key in a mapping, or -1.

func (*Node) IsScalar

func (n *Node) IsScalar() bool

IsScalar says whether the node is a string, number, boolean or null.

func (*Node) Scalar

func (n *Node) Scalar() (string, error)

Scalar returns a scalar's text as a string value: what a string field reads (numbers and booleans as written, null as "").

func (*Node) Set

func (n *Node) Set(key string, v *Node)

Set replaces key's value, or appends the entry.

type Pos

type Pos struct {
	File      string
	Line, Col int
}

Pos is where a node starts: a file and a 1-based line and column.

func (Pos) String

func (p Pos) String() string

type Tag

type Tag uint8

Tag is a merge directive attached to a value of an override file (Compose Specification, merge rules: !reset and !override).

const (
	NoTag Tag = iota
	Reset
	Override
)

The merge directives.

Jump to

Keyboard shortcuts

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