eval

package
v0.0.0-...-4704126 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2026 License: BSD-3-Clause Imports: 38 Imported by: 0

Documentation

Overview

Package eval evaluates a parsed Puppet manifest (ast.Program) into a catalog.Catalog. It implements Puppet scoping, expression semantics, conditionals, iteration, class/defined-type instantiation, resource declaration and relationship chaining, and a built-in function set. The type system is delegated to github.com/go-pcore/pcore, data binding to github.com/go-hiera/hiera, and facts to an injectable provider (backed by github.com/go-facter/facter by default).

A function/type registry seam (Evaluator.RegisterFunction) lets a host — notably go-ruby-puppet — contribute Ruby-defined custom functions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EvalPlanString

func EvalPlanString(src, plan string, params map[string]any, opts ...Option) (Value, []LogEntry, error)

EvalPlanString parses src, registers its definitions, and runs the named plan.

Types

type Block

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

Block is a Puppet lambda captured with its defining scope, invocable by a Function during iteration.

func (*Block) Arity

func (b *Block) Arity() int

Arity returns the number of declared block parameters.

func (*Block) Call

func (b *Block) Call(args ...Value) (Value, error)

Call invokes the block with positional arguments. A `next()` inside the block unwinds to here and yields its value as the block's result; a `break()` propagates so the surrounding iterator can stop.

type Context

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

Context is handed to every Function. It exposes the evaluator services a function needs without widening the public surface.

func (*Context) Catalog

func (c *Context) Catalog() *catalog.Catalog

Catalog returns the catalog being built.

func (*Context) Log

func (c *Context) Log(level, msg string)

Log records a message at the given level.

func (*Context) Lookup

func (c *Context) Lookup(key string) (Value, bool, error)

Lookup resolves key through the configured Hiera, if any.

type ERBRenderer

type ERBRenderer interface {
	RenderERB(src string, vars map[string]any) (string, error)
}

ERBRenderer renders an ERB template (the legacy Ruby-based template format used by template()/inline_template()). ERB templates may contain arbitrary Ruby, which a pure-Go evaluator cannot execute; a host that embeds a Ruby runtime (e.g. go-ruby-puppet/rbgo, which can reuse github.com/go-ruby-erb to compile the template to Ruby and then run it) wires an implementation via WithERBRenderer.

src is the raw template text. vars is the set of top-scope variables and facts the template may reference (as @instance variables / scope lookups in Ruby ERB). The result is the rendered string.

type Error

type Error struct {
	Pos ast.Position
	Msg string
}

Error is an evaluation error, optionally carrying a source position.

func (*Error) Error

func (e *Error) Error() string

Error implements error.

type Evaluator

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

Evaluator holds the state of one compilation.

func New

func New(opts ...Option) *Evaluator

New builds an Evaluator with the built-in functions registered.

func (*Evaluator) EvalPlan

func (e *Evaluator) EvalPlan(name string, params map[string]any) (Value, error)

EvalPlan runs a previously-parsed `plan` by name with the given parameters and returns its result value. Definitions must have been registered first (via EvalProgram or by parsing a program that contains the plan). It is the entry point a Bolt host uses to run a `.pp` plan.

func (*Evaluator) EvalProgram

func (e *Evaluator) EvalProgram(prog *ast.Program) (*catalog.Catalog, error)

EvalProgram evaluates a whole program: definitions are registered first (hoisted) so forward references resolve, then top-level statements run in order.

func (*Evaluator) Logs

func (e *Evaluator) Logs() []LogEntry

Logs returns the messages emitted during evaluation.

func (*Evaluator) RegisterFunction

func (e *Evaluator) RegisterFunction(name string, fn Function)

RegisterFunction adds or replaces a named function. This is the seam a host (e.g. go-ruby-puppet) uses to contribute custom functions.

type ExportedStore

type ExportedStore interface {
	StoreExported(node string, r *catalog.Resource) error
	CollectExported(capType, excludeNode string) ([]*catalog.Resource, error)
}

ExportedStore is the storeback seam for exported resources (`@@`). A host wires a real backend (e.g. PuppetDB via github.com/go-puppetdb/puppetdb) so that resources exported by one node's compilation can be collected by another node with `Type <<| query |>>`.

StoreExported is called for every `@@` resource declared during compilation. CollectExported returns the exported resources of the given capitalized type (e.g. "Sshkey"), excluding those exported by excludeNode (a node never collects its own exports). Query filtering is applied by the evaluator, so an implementation may return a superset.

type FactsProvider

type FactsProvider interface {
	Fact(name string) (Value, bool)
	Facts() map[string]any
}

FactsProvider supplies top-scope facts. A bare fact name ("os") or a dotted path ("os.family") resolves through Fact; Facts returns the whole tree for `$facts`.

func FacterFacts

func FacterFacts() FactsProvider

FacterFacts returns a FactsProvider backed by go-facter's live host inventory. Pass it via WithFacts.

type Function

type Function func(c *Context, args []Value, block *Block) (Value, error)

Function is a callable contributed to the evaluator. args are the already evaluated positional arguments; block is the optional trailing lambda (nil when absent).

type LogEntry

type LogEntry struct {
	Level   string
	Message string
}

LogEntry is one message emitted by a logging function (notice/info/…).

func EvalString

func EvalString(src string, opts ...Option) (*catalog.Catalog, []LogEntry, error)

EvalString parses and evaluates src, returning the resulting catalog.

type MapFacts

type MapFacts map[string]any

MapFacts is a deterministic FactsProvider backed by a nested map. A dotted fact name digs through nested map[string]any / []any values, so {"os": {"family": "Debian"}} resolves "os.family".

func (MapFacts) Fact

func (m MapFacts) Fact(name string) (Value, bool)

Fact resolves a (possibly dotted) fact name.

func (MapFacts) Facts

func (m MapFacts) Facts() map[string]any

Facts returns the whole fact tree.

type MemoryExportedStore

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

MemoryExportedStore is an in-memory ExportedStore. It is safe for concurrent use and is the default backend used in tests and single-process runs.

func NewMemoryExportedStore

func NewMemoryExportedStore() *MemoryExportedStore

NewMemoryExportedStore returns an empty in-memory exported-resource store.

func (*MemoryExportedStore) CollectExported

func (m *MemoryExportedStore) CollectExported(capType, excludeNode string) ([]*catalog.Resource, error)

CollectExported returns the exported resources of capType not exported by excludeNode.

func (*MemoryExportedStore) StoreExported

func (m *MemoryExportedStore) StoreExported(node string, r *catalog.Resource) error

StoreExported records r as exported by node.

type Option

type Option func(*Evaluator)

Option configures a new Evaluator.

func WithERBRenderer

func WithERBRenderer(r ERBRenderer) Option

WithERBRenderer wires an ERB renderer for template()/inline_template().

func WithExportedStore

func WithExportedStore(store ExportedStore) Option

WithExportedStore wires a backing store for exported resources (`@@`), so they can be collected on other nodes with `<<| |>>`. Without one, exported resources are only collectable within the same compilation.

func WithFacts

func WithFacts(f FactsProvider) Option

WithFacts sets the facts provider (default: none — `$facts` is undef).

func WithHiera

func WithHiera(h *hiera.Hiera) Option

WithHiera wires a Hiera engine to back lookup().

func WithNodeName

func WithNodeName(name string) Option

WithNodeName sets the compiling node's name (default "default").

func WithPlanExecutor

func WithPlanExecutor(x PlanExecutor) Option

WithPlanExecutor wires a PlanExecutor for the plan functions.

func WithTemplateLoader

func WithTemplateLoader(l TemplateLoader) Option

WithTemplateLoader wires a template loader for epp()/template().

type PlanExecutor

type PlanExecutor interface {
	RunTask(task string, targets []string, params map[string]any) (Value, error)
	RunCommand(command string, targets []string) (Value, error)
	RunScript(script string, targets []string, args []any) (Value, error)
	GetTargets(spec Value) (Value, error)
	ApplyCatalog(targets []string, cat *catalog.Catalog) (Value, error)
}

PlanExecutor is the orchestration seam a Bolt-like host (notably github.com/go-puppet-bolt/bolt) wires so that a `plan` written in the Puppet language can run tasks/commands/scripts and apply catalogs against targets.

Values use the evaluator's generic model (strings, []any, map[string]any) so the interface has no dependency on a specific transport package. Targets are passed as their string specs; an executor resolves them through its own inventory. The returned Value becomes the plan function's result (typically a ResultSet rendered as an Array of per-target result Hashes).

type ResourceRef

type ResourceRef struct {
	Type  string
	Title string
}

ResourceRef is a Puppet resource reference value, e.g. `File['/tmp/x']`.

func (*ResourceRef) String

func (r *ResourceRef) String() string

String renders the canonical `Type[Title]` form.

type Scope

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

Scope is a Puppet variable scope: a set of `$name => value` bindings with a lexical parent chain up to the top scope. Puppet variables are immutable once set within a scope.

type Sensitive

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

Sensitive is the value-model wrapper for Puppet's Sensitive data type. It carries an arbitrary value while redacting it in every rendered form (to_s, inspect, interpolation) so secrets do not leak into logs or catalogs. The wrapped value is retrievable only through Sensitive.Unwrap (or the `unwrap` function). It is used by resource-api sensitive-parameter handling and by stdlib::rewrap_sensitive_data.

func NewSensitive

func NewSensitive(v Value) *Sensitive

NewSensitive wraps v as a Sensitive value. Wrapping mirrors Puppet's Sensitive.new: any value (including an already-Sensitive one) may be wrapped.

func (*Sensitive) String

func (s *Sensitive) String() string

String renders the redacted form. Because [stringify] and [inspect] fall through to fmt "%v" for unknown types, this is what appears wherever a Sensitive is stringified.

func (*Sensitive) Unwrap

func (s *Sensitive) Unwrap() Value

Unwrap returns the wrapped value.

type TemplateLoader

type TemplateLoader interface {
	Load(name string) (string, error)
}

TemplateLoader resolves an EPP/ERB template reference (e.g. "apache/vhost.epp") to its raw source text. Wire one via WithTemplateLoader so epp()/template() can load files. Without a loader, only the inline_* forms work.

type Value

type Value = any

Value is a Puppet value. It uses the same representation as github.com/go-pcore/pcore: bool, int64, float64 and string for scalars, []any for arrays, map[string]any for hashes, pcore.Undef for undef, the pcore wrapper types for the rich values, and pcore.Type for data types.

Jump to

Keyboard shortcuts

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