expr

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2026 License: BSD-3-Clause Imports: 15 Imported by: ? modules, 0 packages

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.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Canonical added in v0.2.0

func Canonical(n Node) string

Canonical prints n in one form: every operation parenthesised, a run of the same logic operator flattened, strings in single quotes. Two expressions that parse to the same tree print the same text, whatever spacing and parentheses their sources used; Parse(Canonical(n)) prints the same text again.

func DecodeJSON

func DecodeJSON(b []byte, lim Limits) (any, error)

DecodeJSON decodes untrusted JSON under the size, depth and node bounds of lim. Its errors never contain the input.

func Depth added in v0.2.0

func Depth(n Node) int

Depth is the nesting depth of n, the bound Parse puts on an expression.

func Equal

func Equal(a, b any) bool

Equal implements ==: same-kind operands compare natively (strings case-insensitively), others as numbers; NaN is never equal; arrays and objects are never equal (reference equality cannot hold across contexts).

func Eval

func Eval(n Node, env *Env) (any, error)

Eval evaluates a parsed expression. The result is a JSON-shaped value.

func Evaluate

func Evaluate(src string, env *Env) (any, error)

Evaluate parses and evaluates src.

func FormatHCL added in v0.2.0

func FormatHCL(n Node, quote func(string) string) (string, error)

FormatHCL prints n as an HCL expression that reads back as the same tree once HCL's own front end has lowered it. quote writes a string as an HCL quoted string, escapes included; the caller owns HCL's string rules. A value HCL cannot spell is an error: NaN and the infinities (in HCL they are variables, or a unary minus on one).

func IsLiteral added in v0.2.0

func IsLiteral(n Node) bool

IsLiteral reports whether n is a literal (a string, number, bool or null): its HCL text, written bare, would be that literal and not an expression.

func Render added in v0.2.0

func Render(n Node, lim Limits) (string, error)

Render prints n as expression text and proves the text: it parses the text back under lim and requires the tree it gets to be n. A tree whose text reads as another (a variable named NaN is the literal NaN, once printed) or not at all is refused, so the text is never a second reading of the tree.

func ToNumber

func ToNumber(v any) float64

ToNumber coerces to a number: null 0, true 1, false 0, "" 0, a numeric string its value, anything else NaN.

func ToString

func ToString(v any) string

ToString coerces to a string: null "", numbers in a JavaScript-like form, arrays "Array", objects "Object".

func Truthy

func Truthy(v any) bool

Truthy implements the GitHub truthiness rules: false, 0, -0, NaN, "" and null are falsy; arrays and objects are truthy.

Types

type Env

type Env struct {
	Contexts map[string]any
	Status   Status    // nil: success() is true, failure() and cancelled() false
	Files    HashFiler // nil: hashFiles is an error
	Limits   Limits
	// Tenant names who is evaluating (a comparable value: an account, a
	// repository). The limits on abandoned hashFiles calls are counted per
	// Tenant, so one tenant's stalled filer cannot refuse another's calls.
	Tenant any
	// Work, when set, receives the work each evaluation spent (also one that
	// was refused), ADDED to it. With Limits.MaxWork set to what is left, a
	// caller makes one budget out of many evaluations.
	Work *int
}

Env is what an evaluation reads. Contexts maps a context name (matched case-insensitively) to its value.

type Error

type Error struct {
	Pos int // byte offset in the expression, -1 when not positional
	Msg string
}

Error is a syntax, limit or evaluation error. Its text names positions, kinds and source tokens, never an evaluated value, so a secret that flowed through an expression cannot appear in it.

func (*Error) Error

func (e *Error) Error() string

type HashFiler

type HashFiler interface {
	HashFiles(ctx context.Context, patterns []string) (string, error)
}

HashFiler computes hashFiles(patterns...). The library has no file system; the caller supplies one (or none, and the call is an error).

HashFiles MUST honour ctx: it is cancelled when the evaluation's time is spent, and a call that ignores it keeps running after the evaluation has given up on it (it is then "abandoned" until it returns).

The limits are counted per Env.Tenant (Envs with no Tenant share one): once a tenant has maxAbandoned (16) abandoned calls, its next hashFiles is refused as saturated until some return; a tenant never has more than maxTenantInflight (64) calls in flight, so never more than 64 abandoned (calls admitted before any of them timed out can all be abandoned). Another tenant is not affected by either cap, but a hard backstop of maxInflight (256) calls in flight process-wide, over all tenants, bounds goroutines and memory, and is shared: 4 tenants with 64 calls each exhaust it for every other one. A Tenant that does not equal itself (NaN) or is uncomparable is the shared tenant. A slot is reserved before the call starts and released when it returns.

type Limits

type Limits struct {
	MaxLength     int // bytes of one expression (default 21000, GitHub's own)
	MaxDepth      int // AST depth, also bounds evaluation recursion (default 100)
	MaxResult     int // bytes of any string an evaluation builds (default 1 MiB)
	MaxJSONBytes  int // input of fromJSON (default 64 KiB)
	MaxJSONDepth  int // nesting inside fromJSON (default 32)
	MaxJSONNodes  int // values inside fromJSON (default 10000)
	MaxTemplate   int // bytes of a ${{ }} template string (default 256 KiB)
	MaxTemplateEx int // expressions in one template string (default 256)
	MaxWork       int // cumulative work of one evaluation, one unit per byte touched or node visited (default 16 MiB)
	// MaxTime is the wall-clock deadline of one evaluation (default 2 s), read
	// at the charge points: a second line of defence behind MaxWork.
	MaxTime time.Duration
}

Limits bounds every resource an expression can consume. A zero field takes its default; there is no "unlimited".

func DefaultLimits

func DefaultLimits() Limits

DefaultLimits are applied to zero fields.

type Node

type Node interface {
	// contains filtered or unexported methods
}

Node is a parsed expression.

func Call added in v0.2.0

func Call(name string, args ...Node) (Node, error)

Call is name(args...), name one of the functions of the language.

func Cmp added in v0.2.0

func Cmp(op string, l, r Node) (Node, error)

Cmp is l op r, op one of == != < <= > >=.

func Index added in v0.2.0

func Index(recv, idx Node) Node

Index is recv[idx].

func Lit added in v0.2.0

func Lit(v any) (Node, error)

Lit is a literal: nil, a bool, a float64 or a string.

func Logic added in v0.2.0

func Logic(and bool, ops ...Node) (Node, error)

Logic is the && (and) or || of its operands. An operand of the same operator is folded in, as a && (b && c) and (a && b) && c are the one expression a && b && c.

func Not added in v0.2.0

func Not(x Node) Node

Not is !x.

func Parse

func Parse(src string, lim Limits) (Node, error)

Parse parses one expression (the text between ${{ and }}).

func Prop added in v0.2.0

func Prop(recv Node, name string) (Node, error)

Prop is recv.name.

func Star added in v0.2.0

func Star(recv Node) Node

Star is recv.*.

func Var added in v0.2.0

func Var(name string) (Node, error)

Var is a context name such as github or matrix.

type StaticStatus

type StaticStatus struct{ Succeeded, Failed, Canceled bool }

StaticStatus is a Status with fixed answers.

func (StaticStatus) Cancelled

func (s StaticStatus) Cancelled() bool

func (StaticStatus) Failure

func (s StaticStatus) Failure() bool

func (StaticStatus) Success

func (s StaticStatus) Success() bool

type Status

type Status interface {
	Success() bool
	Failure() bool
	Cancelled() bool
}

Status answers the status-check functions. always() needs no answer.

type Template

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

Template is a string with embedded ${{ }} expressions.

func ParseCondition

func ParseCondition(s string, lim Limits) (*Template, error)

ParseCondition parses an `if:` value: either a template, or (as GitHub allows) a bare expression. An empty condition is the constant true.

func ParseTemplate

func ParseTemplate(s string, lim Limits) (*Template, error)

ParseTemplate splits s into literal text and parsed expressions. A "}}" inside a single-quoted string does not close an expression.

func (*Template) Canonical added in v0.2.0

func (t *Template) Canonical() string

Canonical prints the template with every expression in canonical form.

func (*Template) Each added in v0.2.0

func (t *Template) Each(fn func(lit string, n Node))

Each calls fn for every part of the template in order: the text of a literal part with a nil node, or the parsed node of an expression part.

func (*Template) Eval

func (t *Template) Eval(env *Env) (any, error)

Eval evaluates the template: the typed value for a single expression, the concatenation (each value coerced to a string) otherwise.

func (*Template) EvalCondition

func (t *Template) EvalCondition(env *Env) (bool, error)

EvalCondition evaluates a condition to a boolean. When it calls no status function it is implicitly wrapped in success() &&, as GitHub does.

func (*Template) EvalString

func (t *Template) EvalString(env *Env) (string, error)

EvalString is Eval coerced to a string.

func (*Template) HasExpr

func (t *Template) HasExpr() bool

HasExpr reports whether the template contains any expression.

func (*Template) HasStatusCheck

func (t *Template) HasStatusCheck() bool

HasStatusCheck reports whether a parsed condition calls success(), failure(), always() or cancelled(); without one GitHub wraps the condition in success() &&.

func (*Template) Literal added in v0.1.1

func (t *Template) Literal() (string, bool)

Literal returns the text of a template that holds no expression, which is what Eval returns for it, without the cost of an evaluation.

func (*Template) Reads

func (t *Template) Reads(context string) bool

Reads reports whether the template names the context (matched like an expression does, ASCII case-insensitively): a name inside a string literal is not a read.

func (*Template) Single

func (t *Template) Single() bool

Single reports whether the template is exactly one expression, whose typed value Eval then returns.

Jump to

Keyboard shortcuts

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