Documentation
¶
Index ¶
- func Canonical(n Node) string
- func DecodeJSON(b []byte, lim Limits) (any, error)
- func Depth(n Node) int
- func Equal(a, b any) bool
- func Eval(n Node, env *Env) (any, error)
- func Evaluate(src string, env *Env) (any, error)
- func FormatHCL(n Node, quote func(string) string) (string, error)
- func IsLiteral(n Node) bool
- func Render(n Node, lim Limits) (string, error)
- func ToNumber(v any) float64
- func ToString(v any) string
- func Truthy(v any) bool
- type Env
- type Error
- type HashFiler
- type Limits
- type Node
- func Call(name string, args ...Node) (Node, error)
- func Cmp(op string, l, r Node) (Node, error)
- func Index(recv, idx Node) Node
- func Lit(v any) (Node, error)
- func Logic(and bool, ops ...Node) (Node, error)
- func Not(x Node) Node
- func Parse(src string, lim Limits) (Node, error)
- func Prop(recv Node, name string) (Node, error)
- func Star(recv Node) Node
- func Var(name string) (Node, error)
- type StaticStatus
- type Status
- type Template
- func (t *Template) Canonical() string
- func (t *Template) Each(fn func(lit string, n Node))
- func (t *Template) Eval(env *Env) (any, error)
- func (t *Template) EvalCondition(env *Env) (bool, error)
- func (t *Template) EvalString(env *Env) (string, error)
- func (t *Template) HasExpr() bool
- func (t *Template) HasStatusCheck() bool
- func (t *Template) Literal() (string, bool)
- func (t *Template) Reads(context string) bool
- func (t *Template) Single() bool
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Canonical ¶ added in v0.2.0
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 ¶
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
Depth is the nesting depth of n, the bound Parse puts on an expression.
func Equal ¶
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 FormatHCL ¶ added in v0.2.0
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
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
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 ¶
ToNumber coerces to a number: null 0, true 1, false 0, "" 0, a numeric string its value, anything else NaN.
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 ¶
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.
type HashFiler ¶
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".
type Node ¶
type Node interface {
// contains filtered or unexported methods
}
Node is a parsed expression.
func Logic ¶ added in v0.2.0
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.
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 Template ¶
type Template struct {
// contains filtered or unexported fields
}
Template is a string with embedded ${{ }} expressions.
func ParseCondition ¶
ParseCondition parses an `if:` value: either a template, or (as GitHub allows) a bare expression. An empty condition is the constant true.
func ParseTemplate ¶
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
Canonical prints the template with every expression in canonical form.
func (*Template) Each ¶ added in v0.2.0
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 ¶
Eval evaluates the template: the typed value for a single expression, the concatenation (each value coerced to a string) otherwise.
func (*Template) EvalCondition ¶
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 ¶
EvalString is Eval coerced to a string.
func (*Template) HasStatusCheck ¶
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
Literal returns the text of a template that holds no expression, which is what Eval returns for it, without the cost of an evaluation.