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

expr

package
v0.7.16 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2026 License: MPL-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package expr evaluates GitHub Actions style `if:` conditions under three-valued logic. A reference that the caller's Facts cannot resolve yields Unknown rather than a guess, so callers can tell "this condition is provably false" from "we don't have enough information to know."

The grammar is the subset of GitHub's expression syntax actually used in job and step conditions: boolean literals, string literals in single quotes, dotted references (including `.*.name` collection references), the operators `==`, `!=`, `&&`, `||`, `!`, parentheses, and the functions contains, startsWith and endsWith. Anything outside that grammar parses as an unknown leaf instead of failing, since a condition this package cannot parse is exactly the kind of condition it should refuse to decide on.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ComparedLiteral

func ComparedLiteral(expression string, refs []string) (string, bool)

ComparedLiteral is the string literal the first of refs (any of them) is compared against in expression, read off that very comparison node: `ref == 'x'`, `'x' == ref`, `ref != 'x'`, or a contains / startsWith / endsWith call whose first argument is the reference and whose second is the literal. ok is false when no reference of refs is compared to a literal (compared to another reference, or not compared at all).

func ComparedLiterals

func ComparedLiterals(expression string, refs []string) []string

ComparedLiterals is every string literal any of refs is compared against in expression, in source order, read off the same comparison shapes as ComparedLiteral. Empty when none is.

func HasStatusCall

func HasStatusCall(expression string) bool

HasStatusCall reports whether expression calls a job status function that makes GitHub run the job even when a job it needs was skipped: always(), failure() or cancelled() anywhere, or success() under a negation (a plain success() is the default and changes nothing). Function names are case-insensitive, as GitHub reads them. A status call the grammar could not place (it sits inside a fragment parsed as unknown) counts too: reading a status call where there is none only keeps a predecessor's gate from closing the job, never the reverse.

func Refs

func Refs(expression string) []string

Refs returns every dotted reference an expression reads, in the order they appear, for callers that want to know what an expression depends on without evaluating it.

Types

type Comparison

type Comparison struct {
	Literal string
	Negated bool
}

Comparison is one comparison of a reference to a string literal: Negated when the condition holds with the two different (`!=`, or an odd number of `!` around it), not when it holds with them equal (`==`, a contains / startsWith / endsWith call).

func Comparisons

func Comparisons(expression string, refs []string) []Comparison

Comparisons is every comparison of any of refs to a string literal in expression, in source order, read off the same shapes as ComparedLiteral, with its polarity.

type Facts

type Facts interface {
	Resolve(ref string) (val string, known bool)
}

Facts resolves a dotted reference such as "github.event_name" or "needs.policy.outputs.run" to its string value. known is false when the reference has no value in the facts the caller has available, which is distinct from the reference resolving to an empty string.

type Value

type Value int

Value is the result of evaluating a condition under three-valued logic.

const (
	// False means the condition is provably false given the facts.
	False Value = iota
	// True means the condition is provably true given the facts.
	True
	// Unknown means the facts do not determine the condition's outcome.
	Unknown
)

func Eval

func Eval(expression string, f Facts) Value

Eval evaluates expression against f and returns its three-valued result. An expression wrapped in "${{ ... }}" has the wrapper stripped first. An empty expression (no "if:" present) evaluates to True, since an absent condition runs unconditionally. Anything the grammar does not cover evaluates to Unknown rather than panicking.

Jump to

Keyboard shortcuts

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