feat(eda): type per-playbook action results in CEL env
Declare `actions` as a per-playbook object type with one field per declared prior action id, each typed by that kind's Go Result struct (multi-action-playbooks.md D3). A data-driven types.Provider exposes lower-cased Result fields to the checker, so a result-field typo, an undeclared id, or a dynamic actions read is a compile-time rejection. Evaluation still reads an ordinary map value; no custom ref.Val adapter is needed. NewEnv now takes the declared results (an empty set is valid); `event` stays dyn. The playbook loader keeps its current single-action boundary, so its zero-declared-results env is unchanged and playbook.go needed no call-site edit. Two playbook tests are updated to assert the new compile-time rejection semantics.
This commit is contained in:
3 files changed
+376
-144
No files matched your search
@@ -3,11 +3,14 @@
|
||||
// per eda-playbook-engine.md's resolved open question 1 (CEL decision).
|
||||
//
|
||||
// The environment declares two context roots -- `event` (the triggering
|
||||
// event's decoded payload) and `actions` (prior actions' results keyed by the
|
||||
// action's declared id, `actions.<id>.result.<field>`) -- and applies a cost
|
||||
// limit to every program. The same compile path serves the playbook loader
|
||||
// (reject-at-load) and the lint tool, so author-time diagnostics and runtime
|
||||
// evaluation cannot disagree.
|
||||
// event's decoded payload, kept dyn by J4) and `actions` (prior actions'
|
||||
// results keyed by the action's declared id) -- and applies a cost limit to
|
||||
// every program. `actions` is not a global map: its shape depends on the ids
|
||||
// one playbook declares and the kind each id runs, so NewEnv takes the
|
||||
// declared ids and their Go Result struct types and builds a per-playbook
|
||||
// object type (multi-action-playbooks.md, D3). The same compile path serves
|
||||
// the playbook loader (reject-at-load) and the lint tool, so author-time
|
||||
// diagnostics and runtime evaluation cannot disagree.
|
||||
//
|
||||
// CEL is non-Turing-complete, side-effect-free, and panic-free by design (no
|
||||
// recover() wrapper is needed; verified in the t2db.14 acceptance tests
|
||||
@@ -16,16 +19,30 @@
|
||||
package expr
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"cel.dev/cel-go/cel"
|
||||
celast "cel.dev/cel-go/common/ast"
|
||||
"cel.dev/cel-go/common/types"
|
||||
"cel.dev/cel-go/common/types/ref"
|
||||
)
|
||||
|
||||
// DefaultCostLimit bounds evaluation of every playbook expression (J5 in the
|
||||
// resolved doc: tunable, start here; linter and daemon share the same value).
|
||||
const DefaultCostLimit = 100_000
|
||||
|
||||
// DeclaredResult is one prior action's result the environment exposes to a
|
||||
// later expression. ID is the action's declared id, read as
|
||||
// `actions.<ID>.result.<field>`; Type is the reflect.Type of that action
|
||||
// kind's Go Result struct (the value the run marshals into the per-id
|
||||
// `{result: ...}` map before a later expression reads it).
|
||||
type DeclaredResult struct {
|
||||
ID string
|
||||
Type reflect.Type
|
||||
}
|
||||
|
||||
// Env is the CEL environment for playbook expressions: the declared context
|
||||
// roots and the default cost limit applied to every compiled program.
|
||||
type Env struct {
|
||||
@@ -33,7 +50,7 @@ type Env struct {
|
||||
costLimit uint64
|
||||
}
|
||||
|
||||
// Context is what an expression may read at dispatch time. Both maps are
|
||||
// Context is what an expression may read at dispatch time. Both roots are
|
||||
// read-only from the expression's perspective; CEL enforces this.
|
||||
type Context struct {
|
||||
// Event is the triggering event's decoded payload (webhook body, forge
|
||||
@@ -43,38 +60,188 @@ type Context struct {
|
||||
// declared as CEL types).
|
||||
Event map[string]any
|
||||
// Actions holds prior actions' results keyed by the action's id as
|
||||
// declared in the playbook. Empty today (single-action playbooks have
|
||||
// no prior actions); kept dyn for the same reason as Event.
|
||||
// declared in the playbook. Each id maps to the per-id wrapper
|
||||
// `{"result": <KindResult struct>}` the run produces, so a later
|
||||
// expression reads `actions.<id>.result.<field>`.
|
||||
Actions map[string]map[string]any
|
||||
}
|
||||
|
||||
// NewEnv builds the CEL environment with the two context roots and the
|
||||
// default cost limit.
|
||||
func NewEnv() *Env {
|
||||
// NewEnv builds a CEL environment whose `actions` root is an object type with
|
||||
// one field per declared result. An empty declared set is valid: `actions` is
|
||||
// then an object with no fields, and any `actions.<id>` read fails at compile
|
||||
// time. `event` stays dyn (J4).
|
||||
func NewEnv(declared ...DeclaredResult) *Env {
|
||||
reg, err := newResultRegistry(declared)
|
||||
if err != nil {
|
||||
// NewEnv with an unsupported Result type (not a struct) is a
|
||||
// programming error, not a playbook error. Panic is appropriate
|
||||
// (package init-style invariant).
|
||||
panic(err)
|
||||
}
|
||||
provider := newActionResultProvider(declared, reg)
|
||||
celEnv, err := cel.NewEnv(
|
||||
cel.CustomTypeAdapter(reg),
|
||||
cel.CustomTypeProvider(provider),
|
||||
cel.Variable("event", cel.MapType(cel.StringType, cel.DynType)),
|
||||
cel.Variable("actions", cel.MapType(cel.StringType, cel.DynType)),
|
||||
cel.Variable("actions", provider.actionsType()),
|
||||
)
|
||||
if err != nil {
|
||||
// cel.NewEnv with static variable declarations cannot fail in
|
||||
// practice; a failure here is a programming error, not a playbook
|
||||
// error. Panic is appropriate (package init-style invariant).
|
||||
// error. Panic is appropriate.
|
||||
panic(err)
|
||||
}
|
||||
return &Env{celEnv: celEnv, costLimit: DefaultCostLimit}
|
||||
}
|
||||
|
||||
// newResultRegistry registers every declared Result reflect.Type with CEL's
|
||||
// native type system, using lower-cased Go field names so `Written` is read
|
||||
// as `written`. The registry is both the provider (for the Result struct
|
||||
// fields) and the adapter (so a Go Result struct value inside the eval map
|
||||
// adapts to those same fields).
|
||||
func newResultRegistry(declared []DeclaredResult) (*types.Registry, error) {
|
||||
items := make([]any, 0, len(declared)+1)
|
||||
items = append(items, types.ParseStructField(lowerFieldName))
|
||||
for _, d := range declared {
|
||||
items = append(items, d.Type)
|
||||
}
|
||||
return types.NewRegistry(items...)
|
||||
}
|
||||
|
||||
// lowerFieldName maps a Go struct field to its CEL name: the lower-cased
|
||||
// field name (Written -> written, Level -> level).
|
||||
func lowerFieldName(f reflect.StructField) string {
|
||||
return strings.ToLower(f.Name)
|
||||
}
|
||||
|
||||
// nativeResultTypeName is the CEL type name cel-go's native type system
|
||||
// assigns to a reflect.Type (simple package alias + struct name), e.g.
|
||||
// `log.Result`. It must match the name the native registry registers, because
|
||||
// the per-id wrapper's `result` field is typed by that name and its fields
|
||||
// are resolved through the native registry.
|
||||
func nativeResultTypeName(t reflect.Type) string {
|
||||
if t.Kind() == reflect.Pointer {
|
||||
t = t.Elem()
|
||||
}
|
||||
pkg := t.PkgPath()
|
||||
if i := strings.LastIndexByte(pkg, '/'); i >= 0 {
|
||||
pkg = pkg[i+1:]
|
||||
}
|
||||
return pkg + "." + t.Name()
|
||||
}
|
||||
|
||||
// The `actions` root object type name and the per-id wrapper type namespace.
|
||||
// Per-id names must live in a reserved namespace (eda.action.<id>): naming a
|
||||
// wrapper `actions.<id>` makes the checker parse `actions.<id>` as a type
|
||||
// reference rather than a field selection, which then fails with "type ...
|
||||
// does not support field selection".
|
||||
const (
|
||||
actionsTypeName = "actions"
|
||||
actionTypePrefix = "eda.action."
|
||||
resultFieldName = "result"
|
||||
)
|
||||
|
||||
// actionResultProvider is the data-driven types.Provider for the
|
||||
// per-playbook `actions` object. It owns the `actions` root and each
|
||||
// `eda.action.<id>` wrapper; every other type name (including the native
|
||||
// Result structs) is delegated to the native registry.
|
||||
type actionResultProvider struct {
|
||||
ids []string
|
||||
resultOf map[string]string
|
||||
base *types.Registry
|
||||
}
|
||||
|
||||
// newActionResultProvider builds the provider from the declared results,
|
||||
// de-duplicating and sorting ids so field-name reporting is deterministic.
|
||||
func newActionResultProvider(declared []DeclaredResult, base *types.Registry) *actionResultProvider {
|
||||
p := &actionResultProvider{
|
||||
ids: make([]string, 0, len(declared)),
|
||||
resultOf: make(map[string]string, len(declared)),
|
||||
base: base,
|
||||
}
|
||||
for _, d := range declared {
|
||||
if _, seen := p.resultOf[d.ID]; seen {
|
||||
continue
|
||||
}
|
||||
p.resultOf[d.ID] = nativeResultTypeName(d.Type)
|
||||
p.ids = append(p.ids, d.ID)
|
||||
}
|
||||
sort.Strings(p.ids)
|
||||
return p
|
||||
}
|
||||
|
||||
func (p *actionResultProvider) actionsType() *types.Type {
|
||||
return types.NewObjectType(actionsTypeName)
|
||||
}
|
||||
|
||||
func (p *actionResultProvider) wrapperType(id string) string {
|
||||
return actionTypePrefix + id
|
||||
}
|
||||
|
||||
// EnumValue implements types.Provider.
|
||||
func (p *actionResultProvider) EnumValue(enumName string) ref.Val {
|
||||
return p.base.EnumValue(enumName)
|
||||
}
|
||||
|
||||
// FindIdent implements types.Provider.
|
||||
func (p *actionResultProvider) FindIdent(identName string) (ref.Val, bool) {
|
||||
return p.base.FindIdent(identName)
|
||||
}
|
||||
|
||||
// FindStructType implements types.Provider.
|
||||
func (p *actionResultProvider) FindStructType(structType string) (*types.Type, bool) {
|
||||
if structType == actionsTypeName {
|
||||
return types.NewTypeTypeWithParam(types.NewObjectType(structType)), true
|
||||
}
|
||||
for _, id := range p.ids {
|
||||
if structType == p.wrapperType(id) {
|
||||
return types.NewTypeTypeWithParam(types.NewObjectType(structType)), true
|
||||
}
|
||||
}
|
||||
return p.base.FindStructType(structType)
|
||||
}
|
||||
|
||||
// FindStructFieldNames implements types.Provider.
|
||||
func (p *actionResultProvider) FindStructFieldNames(structType string) ([]string, bool) {
|
||||
if structType == actionsTypeName {
|
||||
return append([]string(nil), p.ids...), true
|
||||
}
|
||||
for _, id := range p.ids {
|
||||
if structType == p.wrapperType(id) {
|
||||
return []string{resultFieldName}, true
|
||||
}
|
||||
}
|
||||
return p.base.FindStructFieldNames(structType)
|
||||
}
|
||||
|
||||
// FindStructFieldType implements types.Provider.
|
||||
func (p *actionResultProvider) FindStructFieldType(structType, fieldName string) (*types.FieldType, bool) {
|
||||
if structType == actionsTypeName {
|
||||
for _, id := range p.ids {
|
||||
if fieldName == id {
|
||||
return &types.FieldType{Type: types.NewObjectType(p.wrapperType(id))}, true
|
||||
}
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
for _, id := range p.ids {
|
||||
if structType == p.wrapperType(id) && fieldName == resultFieldName {
|
||||
return &types.FieldType{Type: types.NewObjectType(p.resultOf[id])}, true
|
||||
}
|
||||
}
|
||||
return p.base.FindStructFieldType(structType, fieldName)
|
||||
}
|
||||
|
||||
// NewValue implements types.Provider.
|
||||
func (p *actionResultProvider) NewValue(structType string, fields map[string]ref.Val) ref.Val {
|
||||
return p.base.NewValue(structType, fields)
|
||||
}
|
||||
|
||||
// Compile parses and type-checks a playbook expression string against the
|
||||
// declared context. A syntax error, an unknown root (anything other than
|
||||
// event/actions), or a type error is a returned error -- never a panic. The
|
||||
// returned Program is safe to evaluate concurrently (CEL programs are
|
||||
// stateless once compiled).
|
||||
//
|
||||
// Compile does not decide whether every `actions` read can be pinned to a
|
||||
// prior action id at load; that classification is Program.ActionReferences'
|
||||
// job, and the playbook loader rejects a program whose ActionReferences
|
||||
// reports unresolvable. The split keeps author-time diagnostics and runtime
|
||||
// evaluation on the same compiled artifact.
|
||||
// event/actions), a dynamic `actions` read, or a field typo on a declared
|
||||
// result is a returned error -- never a panic. The returned Program is safe
|
||||
// to evaluate concurrently (CEL programs are stateless once compiled).
|
||||
func (e *Env) Compile(src string) (*Program, error) {
|
||||
ast, issues := e.celEnv.Compile(src)
|
||||
if issues != nil && issues.Err() != nil {
|
||||
@@ -103,6 +270,11 @@ func (e *Env) Compile(src string) (*Program, error) {
|
||||
// slipping through as a runtime miss. The returned ids are the sorted,
|
||||
// de-duplicated ids of the static accesses, for the playbook loader's
|
||||
// unknown-id check.
|
||||
//
|
||||
// With the per-playbook object type the checker already rejects every
|
||||
// non-static `actions` read at compile time, so a program that reaches this
|
||||
// walk is resolvable by construction; the walk is retained until the next
|
||||
// pass removes it together with the loader's duplicate unknown-id check.
|
||||
func actionReferences(ast *cel.Ast) ([]string, bool) {
|
||||
if ast == nil || ast.NativeRep() == nil {
|
||||
return nil, true
|
||||
|
||||
@@ -1,13 +1,139 @@
|
||||
package expr
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"cel.dev/cel-go/cel"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/eda/module/log"
|
||||
)
|
||||
|
||||
// declaredResultEnv builds the per-playbook environment used by the typed
|
||||
// `actions` tests: one declared prior action id "build" whose result type is
|
||||
// the log kind's Go Result struct (fields Written, Level -> written, level).
|
||||
func declaredResultEnv(t *testing.T) *Env {
|
||||
t.Helper()
|
||||
return NewEnv(DeclaredResult{ID: "build", Type: reflect.TypeFor[log.Result]()})
|
||||
}
|
||||
|
||||
// TestCompileDeclaredResultRead: a read of a declared id's result field
|
||||
// compiles against the per-playbook object type.
|
||||
func TestCompileDeclaredResultRead(t *testing.T) {
|
||||
env := declaredResultEnv(t)
|
||||
prg, err := env.Compile(`actions.build.result.written == true`)
|
||||
if err != nil {
|
||||
t.Fatalf("Compile(declared result read): %v", err)
|
||||
}
|
||||
ids, resolvable := prg.ActionReferences()
|
||||
if !resolvable || !slices.Equal(ids, []string{"build"}) {
|
||||
t.Fatalf("ActionReferences = (%v, %v), want (build, true)", ids, resolvable)
|
||||
}
|
||||
}
|
||||
|
||||
// TestCompileMisspelledResultFieldRejected: a field the declared Result does
|
||||
// not define is a compile-time error naming the field, never a runtime miss.
|
||||
func TestCompileMisspelledResultFieldRejected(t *testing.T) {
|
||||
env := declaredResultEnv(t)
|
||||
misspelled := "wri" + "ten" // deliberately misspelled Result field under test
|
||||
_, err := env.Compile(`actions.build.result.` + misspelled + ` == true`)
|
||||
if err == nil {
|
||||
t.Fatal("Compile(misspelled field) = nil, want error")
|
||||
}
|
||||
for _, want := range []string{"undefined field", misspelled} {
|
||||
if !strings.Contains(err.Error(), want) {
|
||||
t.Errorf("Compile(misspelled field) error = %q, want it to contain %q", err.Error(), want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestCompileUndeclaredActionIDRejected: a read of an id not declared for the
|
||||
// playbook is a compile-time error naming the id.
|
||||
func TestCompileUndeclaredActionIDRejected(t *testing.T) {
|
||||
env := declaredResultEnv(t)
|
||||
_, err := env.Compile(`actions.unknownid.result.written == true`)
|
||||
if err == nil {
|
||||
t.Fatal("Compile(unknown id) = nil, want error")
|
||||
}
|
||||
for _, want := range []string{"undefined field", "unknownid"} {
|
||||
if !strings.Contains(err.Error(), want) {
|
||||
t.Errorf("Compile(unknown id) error = %q, want it to contain %q", err.Error(), want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestCompileDynamicActionAccessRejected: a dynamic index, an `in` test, or a
|
||||
// size read of `actions` all fail to compile against the object type (there is
|
||||
// no map to index or iterate). Only the failure is asserted; the message is
|
||||
// cel-go's and not part of this package's contract.
|
||||
func TestCompileDynamicActionAccessRejected(t *testing.T) {
|
||||
env := declaredResultEnv(t)
|
||||
for _, src := range []string{
|
||||
`actions["build"]`,
|
||||
`"build" in actions`,
|
||||
`size(actions) > 0`,
|
||||
} {
|
||||
t.Run(src, func(t *testing.T) {
|
||||
if _, err := env.Compile(src); err == nil {
|
||||
t.Errorf("Compile(%q) = nil, want error", src)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestEvalDeclaredResultRead: a declared id's result read evaluates against a
|
||||
// map whose id entry wraps the Go Result struct in `{result: ...}`.
|
||||
func TestEvalDeclaredResultRead(t *testing.T) {
|
||||
env := declaredResultEnv(t)
|
||||
prg, err := env.Compile(`actions.build.result.written`)
|
||||
if err != nil {
|
||||
t.Fatalf("Compile: %v", err)
|
||||
}
|
||||
got, err := env.Eval(prg, Context{
|
||||
Actions: map[string]map[string]any{
|
||||
"build": {"result": log.Result{Written: true, Level: "info"}},
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Eval: %v", err)
|
||||
}
|
||||
if b, ok := got.(bool); !ok || !b {
|
||||
t.Fatalf("Eval = %#v, want true", got)
|
||||
}
|
||||
|
||||
level, err := env.Eval(mustCompile(t, env, `actions.build.result.level`), Context{
|
||||
Actions: map[string]map[string]any{
|
||||
"build": {"result": log.Result{Written: true, Level: "info"}},
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Eval(level): %v", err)
|
||||
}
|
||||
if s, ok := level.(string); !ok || s != "info" {
|
||||
t.Fatalf("Eval(level) = %#v, want %q", level, "info")
|
||||
}
|
||||
}
|
||||
|
||||
// TestEmptyDeclaredSetIsValid: an environment with no declared results has an
|
||||
// `actions` object with no fields; any `actions.<id>` read fails at compile.
|
||||
func TestEmptyDeclaredSetIsValid(t *testing.T) {
|
||||
env := NewEnv()
|
||||
if _, err := env.Compile(`event.label == "x"`); err != nil {
|
||||
t.Fatalf("Compile(event read) on empty env: %v", err)
|
||||
}
|
||||
_, err := env.Compile(`actions.build.result.written == true`)
|
||||
if err == nil {
|
||||
t.Fatal("Compile(actions read) on empty env = nil, want error")
|
||||
}
|
||||
for _, want := range []string{"undefined field", "build"} {
|
||||
if !strings.Contains(err.Error(), want) {
|
||||
t.Errorf("Compile(actions read) on empty env error = %q, want it to contain %q", err.Error(), want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestCompileEvalValidBooleanAgainstEvent is the happy path: a boolean
|
||||
// condition reading an event field compiles and evaluates correctly.
|
||||
func TestCompileEvalValidBooleanAgainstEvent(t *testing.T) {
|
||||
@@ -80,94 +206,47 @@ func TestEvalHasMacroPresence(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestActionReferences classifies every read of the `actions` context root:
|
||||
// a static access (actions.<id> or actions["<id>"]) resolves to its id, and
|
||||
// any other spelling that mentions actions reports unresolvable.
|
||||
// TestActionReferences classifies every read of the `actions` context root
|
||||
// that still compiles under the per-playbook object type: a static field
|
||||
// selection resolves to its id, and a bare `actions` value (which compiles)
|
||||
// is reported unresolvable so the playbook loader still rejects it.
|
||||
func TestActionReferences(t *testing.T) {
|
||||
env := NewEnv()
|
||||
env := declaredResultEnv(t)
|
||||
tests := []struct {
|
||||
name string
|
||||
src string
|
||||
wantIDs []string
|
||||
resolvable bool
|
||||
name string
|
||||
src string
|
||||
wantIDs []string
|
||||
wantResolvable bool
|
||||
}{
|
||||
{
|
||||
name: "field selection resolves",
|
||||
src: `actions.a.result.x == true`,
|
||||
wantIDs: []string{"a"},
|
||||
resolvable: true,
|
||||
name: "field selection resolves",
|
||||
src: `actions.build.result.written == true`,
|
||||
wantIDs: []string{"build"},
|
||||
wantResolvable: true,
|
||||
},
|
||||
{
|
||||
name: "literal map index resolves",
|
||||
src: `actions["a"].result.x == true`,
|
||||
wantIDs: []string{"a"},
|
||||
resolvable: true,
|
||||
name: "two static accesses of one id consume two idents",
|
||||
src: `actions.build.result.written == true && actions.build.result.level == "info"`,
|
||||
wantIDs: []string{"build"},
|
||||
wantResolvable: true,
|
||||
},
|
||||
{
|
||||
name: "two static accesses of one id consume two idents",
|
||||
src: `actions.a.x == actions.a.y`,
|
||||
wantIDs: []string{"a"},
|
||||
resolvable: true,
|
||||
name: "no actions reference",
|
||||
src: `event.priority == 3`,
|
||||
wantIDs: nil,
|
||||
wantResolvable: true,
|
||||
},
|
||||
{
|
||||
name: "multiple references deduped and sorted",
|
||||
src: `actions.b.result == true || actions.a.result == true || actions.b.result2 == true`,
|
||||
wantIDs: []string{"a", "b"},
|
||||
resolvable: true,
|
||||
name: "has macro over static access",
|
||||
src: `has(actions.build.result.written)`,
|
||||
wantIDs: []string{"build"},
|
||||
wantResolvable: true,
|
||||
},
|
||||
{
|
||||
name: "no actions reference",
|
||||
src: `event.priority == 3`,
|
||||
wantIDs: nil,
|
||||
resolvable: true,
|
||||
},
|
||||
{
|
||||
name: "has macro over static access",
|
||||
src: `has(actions.notify.result.delivered)`,
|
||||
wantIDs: []string{"notify"},
|
||||
resolvable: true,
|
||||
},
|
||||
{
|
||||
name: "event field index key is unresolvable",
|
||||
src: `actions[event.name].result.x == true`,
|
||||
wantIDs: nil,
|
||||
resolvable: false,
|
||||
},
|
||||
{
|
||||
name: "computed string index key is unresolvable",
|
||||
src: `actions["a" + "b"].result.x == true`,
|
||||
wantIDs: nil,
|
||||
resolvable: false,
|
||||
},
|
||||
{
|
||||
name: "in operator on actions is unresolvable",
|
||||
src: `"notify" in actions`,
|
||||
wantIDs: nil,
|
||||
resolvable: false,
|
||||
},
|
||||
{
|
||||
name: "size of actions is unresolvable",
|
||||
src: `size(actions) > 0`,
|
||||
wantIDs: nil,
|
||||
resolvable: false,
|
||||
},
|
||||
{
|
||||
name: "macro receiver actions is unresolvable",
|
||||
src: `actions.all(k, k == "a")`,
|
||||
wantIDs: nil,
|
||||
resolvable: false,
|
||||
},
|
||||
{
|
||||
name: "bare actions is unresolvable",
|
||||
src: `actions`,
|
||||
wantIDs: nil,
|
||||
resolvable: false,
|
||||
},
|
||||
{
|
||||
name: "equality against map literal is unresolvable",
|
||||
src: `actions == {}`,
|
||||
wantIDs: nil,
|
||||
resolvable: false,
|
||||
name: "bare actions is unresolvable",
|
||||
src: `actions`,
|
||||
wantIDs: nil,
|
||||
wantResolvable: false,
|
||||
},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
@@ -177,8 +256,8 @@ func TestActionReferences(t *testing.T) {
|
||||
t.Fatalf("Compile(%q): %v", tc.src, err)
|
||||
}
|
||||
ids, resolvable := prg.ActionReferences()
|
||||
if resolvable != tc.resolvable {
|
||||
t.Fatalf("ActionReferences(%q) resolvable = %v, want %v", tc.src, resolvable, tc.resolvable)
|
||||
if resolvable != tc.wantResolvable {
|
||||
t.Fatalf("ActionReferences(%q) resolvable = %v, want %v", tc.src, resolvable, tc.wantResolvable)
|
||||
}
|
||||
if !slices.Equal(ids, tc.wantIDs) {
|
||||
t.Fatalf("ActionReferences(%q) ids = %v, want %v", tc.src, ids, tc.wantIDs)
|
||||
@@ -187,26 +266,6 @@ func TestActionReferences(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestEvalActionsContext: prior-action results read via actions.<id>.result.
|
||||
func TestEvalActionsContext(t *testing.T) {
|
||||
env := NewEnv()
|
||||
prg, err := env.Compile(`actions.a.result.written == true`)
|
||||
if err != nil {
|
||||
t.Fatalf("Compile: %v", err)
|
||||
}
|
||||
got, err := env.Eval(prg, Context{
|
||||
Actions: map[string]map[string]any{
|
||||
"a": {"result": map[string]any{"written": true}},
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Eval: %v", err)
|
||||
}
|
||||
if b, ok := got.(bool); !ok || !b {
|
||||
t.Fatalf("Eval = %#v, want true", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestEvalDeepNestedNoPanic: hostile deep nested access errors cleanly.
|
||||
func TestEvalDeepNestedNoPanic(t *testing.T) {
|
||||
env := NewEnv()
|
||||
@@ -292,3 +351,13 @@ func TestEvalPanicFreeHostileData(t *testing.T) {
|
||||
t.Fatalf("Eval(hostile deep) = %#v, want false", got)
|
||||
}
|
||||
}
|
||||
|
||||
// mustCompile compiles src against env and fails the test on error.
|
||||
func mustCompile(t *testing.T, env *Env, src string) *Program {
|
||||
t.Helper()
|
||||
prg, err := env.Compile(src)
|
||||
if err != nil {
|
||||
t.Fatalf("Compile(%q): %v", src, err)
|
||||
}
|
||||
return prg
|
||||
}
|
||||
@@ -490,8 +490,10 @@ func TestValidateActionIDs(t *testing.T) {
|
||||
|
||||
// TestLoadWhenActionsReferenceFails: a when that reads the `actions` context
|
||||
// root in a form that cannot be pinned to a prior action id at load fails the
|
||||
// whole load, naming the playbook path (and the id when one statically
|
||||
// resolves) -- never a runtime miss.
|
||||
// whole load, naming the playbook path -- never a runtime miss. Under the
|
||||
// per-playbook object type these forms are rejected at compile time: the
|
||||
// field-selection spelling names the undefined id, while a dynamic index, an
|
||||
// `in` test, or a size read fails with cel-go's overload error (no id name).
|
||||
func TestLoadWhenActionsReferenceFails(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
@@ -499,7 +501,7 @@ func TestLoadWhenActionsReferenceFails(t *testing.T) {
|
||||
wantID string
|
||||
}{
|
||||
{name: "undeclared field selection", when: `actions.a.result.x == true`, wantID: "a"},
|
||||
{name: "undeclared literal map index", when: `actions["a"].result.x == true`, wantID: "a"},
|
||||
{name: "dynamic index on actions", when: `actions["a"].result.x == true`},
|
||||
{name: "non-literal index key", when: `actions[key].result.x == true`},
|
||||
{name: "in operator on actions", when: `"notify" in actions`},
|
||||
{name: "size of actions", when: `size(actions) > 0`},
|
||||
@@ -525,33 +527,22 @@ func TestLoadWhenActionsReferenceFails(t *testing.T) {
|
||||
// TestUnknownActionReferenceGeneralRule: the reference check compares the
|
||||
// statically-resolved ids against the declared ids of earlier actions, so it
|
||||
// already behaves correctly when the one-action boundary later relaxes (the
|
||||
// Load path can only exercise the empty-prior set today).
|
||||
// Load path can only exercise the empty-prior set today). The ids are passed
|
||||
// directly: under the per-playbook object type an undeclared `actions.<id>`
|
||||
// read is rejected at compile time, so there is no longer an env that compiles
|
||||
// one to classify.
|
||||
func TestUnknownActionReferenceGeneralRule(t *testing.T) {
|
||||
env := expr.NewEnv()
|
||||
compile := func(src string) []string {
|
||||
t.Helper()
|
||||
prg, err := env.Compile(src)
|
||||
if err != nil {
|
||||
t.Fatalf("Compile(%q): %v", src, err)
|
||||
}
|
||||
ids, resolvable := prg.ActionReferences()
|
||||
if !resolvable {
|
||||
t.Fatalf("ActionReferences(%q) = resolvable false, want true", src)
|
||||
}
|
||||
return ids
|
||||
}
|
||||
|
||||
// a declared on the prior action: known.
|
||||
if id, unknown := unknownActionReference([]rawAction{{ID: "a"}}, 1, compile(`actions.a.result.delivered == true`)); unknown {
|
||||
if id, unknown := unknownActionReference([]rawAction{{ID: "a"}}, 1, []string{"a"}); unknown {
|
||||
t.Fatalf("unknownActionReference(declared prior) = (%q, true), want known", id)
|
||||
}
|
||||
// Two static reads of a in one expression consume two actions idents and
|
||||
// still resolve to the one declared id: known.
|
||||
if id, unknown := unknownActionReference([]rawAction{{ID: "a"}}, 1, compile(`actions.a.x == actions.a.y`)); unknown {
|
||||
t.Fatalf("unknownActionReference(two static reads of declared id) = (%q, true), want known", id)
|
||||
// Two static reads of a in one expression resolve to the one declared id
|
||||
// (ActionReferences de-duplicates), so the de-duplicated id is known.
|
||||
if id, unknown := unknownActionReference([]rawAction{{ID: "a"}}, 1, []string{"a", "a"}); unknown {
|
||||
t.Fatalf("unknownActionReference(deduplicated declared id) = (%q, true), want known", id)
|
||||
}
|
||||
// A different prior id leaves b unknown.
|
||||
if id, unknown := unknownActionReference([]rawAction{{ID: "a"}}, 1, compile(`actions.b.result.delivered == true`)); !unknown || id != "b" {
|
||||
if id, unknown := unknownActionReference([]rawAction{{ID: "a"}}, 1, []string{"b"}); !unknown || id != "b" {
|
||||
t.Fatalf("unknownActionReference(undeclared) = (%q, %v), want (b, true)", id, unknown)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user