Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add partial eval support to the REPL. #874

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .bazeliskrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
USE_BAZEL_VERSION=6.4.0
65 changes: 65 additions & 0 deletions cel/cel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1688,6 +1688,71 @@ func TestPartialVars(t *testing.T) {
}
}

func TestPartialVarsExtendedEnv(t *testing.T) {
env := testEnv(t,
Variable("x", IntType),
Variable("y", IntType),
)

// Use env to make sure internals are all initialized.
ast, iss := env.Compile("x == y")

if iss.Err() != nil {
t.Fatalf("env.Compile() failed: %v", iss.Err())
}
prg, err := env.Program(ast, EvalOptions(OptPartialEval))
if err != nil {
t.Fatalf("env.Program() failed: %v", err)
}

act, err := env.PartialVars(map[string]any{"x": 1, "y": 1})

if err != nil {
t.Fatalf("env.PartialVars failed: %v", err)
}
val, _, err := prg.Eval(act)
if err != nil {
t.Fatalf("Eval failed: %v", err)
}

if val != types.True {
t.Fatalf("want: %v, got: %v", types.True, val)
}

// Now test that a sub environment is correctly copied.
env2, err := env.Extend(Variable("z", IntType))
if err != nil {
t.Fatalf("env.Extend failed: %v", err)
}

ast, iss = env2.Compile("x == y && y == z")

if iss.Err() != nil {
t.Fatalf("env.Compile() failed: %v", iss.Err())
}
prg, err = env2.Program(ast, EvalOptions(OptPartialEval))
if err != nil {
t.Fatalf("env.Program() failed: %v", err)
}

act, err = env2.PartialVars(map[string]any{"z": 1, "y": 1})

if err != nil {
t.Fatalf("env.PartialVars failed: %v", err)
}
val, _, err = prg.Eval(act)
if err != nil {
t.Fatalf("Eval failed: %v", err)
}
if !types.IsUnknown(val) {
t.Fatalf("Wanted unknown, got %v", val)
}

if !reflect.DeepEqual(val, types.NewUnknown(1, types.NewAttributeTrail("x"))) {
t.Fatalf("Wanted Unknown(x (1)), got: %v", val)
}
}

func TestResidualAstAttributeQualifiers(t *testing.T) {
env := testEnv(t,
Variable("x", MapType(StringType, DynType)),
Expand Down
10 changes: 3 additions & 7 deletions cel/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,18 +308,14 @@ func (e *Env) Extend(opts ...EnvOption) (*Env, error) {
chkOptsCopy := make([]checker.Option, len(e.chkOpts))
copy(chkOptsCopy, e.chkOpts)

// Copy the declarations if needed.
varsCopy := []*decls.VariableDecl{}
if chk != nil {
// If the type-checker has already been instantiated, then the e.declarations have been
// validated within the chk instance.
chkOptsCopy = append(chkOptsCopy, checker.ValidatedDeclarations(chk))
} else {
// If the type-checker has not been instantiated, ensure the unvalidated declarations are
// provided to the extended Env instance.
varsCopy = make([]*decls.VariableDecl, len(e.variables))
copy(varsCopy, e.variables)
}
// vars are used by PartialVars to infer unknown attribute paths.
varsCopy := make([]*decls.VariableDecl, len(e.variables))
copy(varsCopy, e.variables)

// Copy macros and program options
macsCopy := make([]parser.Macro, len(e.macros))
Expand Down
4 changes: 3 additions & 1 deletion common/types/unknown.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,9 @@ func (u *Unknown) Equal(other ref.Val) ref.Val {
// String implements the Stringer interface
func (u *Unknown) String() string {
var str strings.Builder
for id, attrs := range u.attributeTrails {
ids := u.IDs()
for _, id := range ids {
attrs := u.attributeTrails[id]
if str.Len() != 0 {
str.WriteString(", ")
}
Expand Down