expr

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 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 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 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 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 Parse

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

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

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) 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) 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