Search in sources :

Example 11 with EvalResult

use of org.projectnessie.cel.Program.EvalResult in project cel-java by projectnessie.

the class CELTest method Abbrevs_Parsed.

@Test
void Abbrevs_Parsed() {
    // Test whether abbreviations are resolved properly at evaluation time.
    Env env = newEnv(abbrevs("qualified.identifier.name"));
    AstIssuesTuple astIss = env.parse("\"hello \" + name.first");
    assertThat(astIss.hasIssues()).isFalse();
    // abbreviation resolved here.
    Program prg = env.program(astIss.getAst());
    EvalResult out = prg.eval(mapOf("qualified.identifier.name", mapOf("first", "Jim")));
    assertThat(out.getVal().value()).isEqualTo("hello Jim");
}
Also used : EvalResult(org.projectnessie.cel.Program.EvalResult) Env.newEnv(org.projectnessie.cel.Env.newEnv) Env.newCustomEnv(org.projectnessie.cel.Env.newCustomEnv) AstIssuesTuple(org.projectnessie.cel.Env.AstIssuesTuple) Test(org.junit.jupiter.api.Test)

Example 12 with EvalResult

use of org.projectnessie.cel.Program.EvalResult in project cel-java by projectnessie.

the class CELTest method ResidualAst_Complex.

@Test
void ResidualAst_Complex() {
    Env e = newEnv(declarations(Decls.newVar("resource.name", Decls.String), Decls.newVar("request.time", Decls.Timestamp), Decls.newVar("request.auth.claims", Decls.newMapType(Decls.String, Decls.String))));
    PartialActivation unkVars = partialVars(mapOf("resource.name", "bucket/my-bucket/objects/private", "request.auth.claims", mapOf("email_verified", "true")), attributePattern("request.auth.claims").qualString("email"));
    AstIssuesTuple astIss = e.compile("resource.name.startsWith(\"bucket/my-bucket\") &&\n" + "\t\t bool(request.auth.claims.email_verified) == true &&\n" + "\t\t request.auth.claims.email == \"wiley@acme.co\"");
    assertThat(astIss.hasIssues()).isFalse();
    Program prg = e.program(astIss.getAst(), evalOptions(OptTrackState, OptPartialEval));
    EvalResult outDet = prg.eval(unkVars);
    assertThat(outDet.getVal()).matches(UnknownT::isUnknown);
    Ast residual = e.residualAst(astIss.getAst(), outDet.getEvalDetails());
    String expr = astToString(residual);
    assertThat(expr).isEqualTo("request.auth.claims.email == \"wiley@acme.co\"");
}
Also used : UnknownT(org.projectnessie.cel.common.types.UnknownT) PartialActivation(org.projectnessie.cel.interpreter.Activation.PartialActivation) CEL.parsedExprToAst(org.projectnessie.cel.CEL.parsedExprToAst) CEL.checkedExprToAst(org.projectnessie.cel.CEL.checkedExprToAst) EvalResult(org.projectnessie.cel.Program.EvalResult) CEL.astToString(org.projectnessie.cel.CEL.astToString) Env.newEnv(org.projectnessie.cel.Env.newEnv) Env.newCustomEnv(org.projectnessie.cel.Env.newCustomEnv) AstIssuesTuple(org.projectnessie.cel.Env.AstIssuesTuple) Test(org.junit.jupiter.api.Test)

Example 13 with EvalResult

use of org.projectnessie.cel.Program.EvalResult in project cel-java by projectnessie.

the class CELTest method ResidualAst_AttributeQualifiers.

@Test
void ResidualAst_AttributeQualifiers() {
    Env e = newEnv(declarations(Decls.newVar("x", Decls.newMapType(Decls.String, Decls.Dyn)), Decls.newVar("y", Decls.newListType(Decls.Int)), Decls.newVar("u", Decls.Int)));
    AstIssuesTuple astIss = e.parse("x.abc == u && x[\"abc\"] == u && x[x.string] == u && y[0] == u && y[x.zero] == u && (true ? x : y).abc == u && (false ? y : x).abc == u");
    Program prg = e.program(astIss.getAst(), evalOptions(OptTrackState, OptPartialEval));
    PartialActivation vars = partialVars(mapOf("x", mapOf("zero", 0, "abc", 123, "string", "abc"), "y", singletonList(123)), attributePattern("u"));
    EvalResult outDet = prg.eval(vars);
    assertThat(outDet.getVal()).matches(UnknownT::isUnknown);
    Ast residual = e.residualAst(astIss.getAst(), outDet.getEvalDetails());
    String expr = astToString(residual);
    assertThat(expr).isEqualTo("123 == u && 123 == u && 123 == u && 123 == u && 123 == u && 123 == u && 123 == u");
}
Also used : UnknownT(org.projectnessie.cel.common.types.UnknownT) PartialActivation(org.projectnessie.cel.interpreter.Activation.PartialActivation) CEL.parsedExprToAst(org.projectnessie.cel.CEL.parsedExprToAst) CEL.checkedExprToAst(org.projectnessie.cel.CEL.checkedExprToAst) EvalResult(org.projectnessie.cel.Program.EvalResult) CEL.astToString(org.projectnessie.cel.CEL.astToString) Env.newEnv(org.projectnessie.cel.Env.newEnv) Env.newCustomEnv(org.projectnessie.cel.Env.newCustomEnv) AstIssuesTuple(org.projectnessie.cel.Env.AstIssuesTuple) Test(org.junit.jupiter.api.Test)

Example 14 with EvalResult

use of org.projectnessie.cel.Program.EvalResult in project cel-java by projectnessie.

the class CELTest method ResidualAst.

@Test
void ResidualAst() {
    Env e = newEnv(declarations(Decls.newVar("x", Decls.Int), Decls.newVar("y", Decls.Int)));
    PartialActivation unkVars = e.getUnknownVars();
    AstIssuesTuple astIss = e.parse("x < 10 && (y == 0 || 'hello' != 'goodbye')");
    Program prg = e.program(astIss.getAst(), evalOptions(OptTrackState, OptPartialEval));
    EvalResult outDet = prg.eval(unkVars);
    assertThat(outDet.getVal()).matches(UnknownT::isUnknown);
    Ast residual = e.residualAst(astIss.getAst(), outDet.getEvalDetails());
    String expr = astToString(residual);
    assertThat(expr).isEqualTo("x < 10");
}
Also used : UnknownT(org.projectnessie.cel.common.types.UnknownT) PartialActivation(org.projectnessie.cel.interpreter.Activation.PartialActivation) CEL.parsedExprToAst(org.projectnessie.cel.CEL.parsedExprToAst) CEL.checkedExprToAst(org.projectnessie.cel.CEL.checkedExprToAst) EvalResult(org.projectnessie.cel.Program.EvalResult) CEL.astToString(org.projectnessie.cel.CEL.astToString) Env.newEnv(org.projectnessie.cel.Env.newEnv) Env.newCustomEnv(org.projectnessie.cel.Env.newCustomEnv) AstIssuesTuple(org.projectnessie.cel.Env.AstIssuesTuple) Test(org.junit.jupiter.api.Test)

Example 15 with EvalResult

use of org.projectnessie.cel.Program.EvalResult in project cel-java by projectnessie.

the class CELTest method CustomEnv.

@Test
void CustomEnv() {
    Env e = newCustomEnv(declarations(Decls.newVar("a.b.c", Decls.Bool)));
    // t.Run("err", func(t *testing.T) {
    AstIssuesTuple xIss = e.compile("a.b.c == true");
    assertThat(xIss.hasIssues()).isTrue();
    // t.Run("ok", func(t *testing.T) {
    AstIssuesTuple astIss = e.compile("a.b.c");
    assertThat(astIss.hasIssues()).isFalse();
    Program prg = e.program(astIss.getAst());
    EvalResult out = prg.eval(mapOf("a.b.c", true));
    assertThat(out.getVal()).isSameAs(True);
}
Also used : EvalResult(org.projectnessie.cel.Program.EvalResult) Env.newEnv(org.projectnessie.cel.Env.newEnv) Env.newCustomEnv(org.projectnessie.cel.Env.newCustomEnv) AstIssuesTuple(org.projectnessie.cel.Env.AstIssuesTuple) Test(org.junit.jupiter.api.Test)

Aggregations

EvalResult (org.projectnessie.cel.Program.EvalResult)16 Env.newCustomEnv (org.projectnessie.cel.Env.newCustomEnv)15 Env.newEnv (org.projectnessie.cel.Env.newEnv)15 Test (org.junit.jupiter.api.Test)14 AstIssuesTuple (org.projectnessie.cel.Env.AstIssuesTuple)14 CEL.checkedExprToAst (org.projectnessie.cel.CEL.checkedExprToAst)7 CEL.parsedExprToAst (org.projectnessie.cel.CEL.parsedExprToAst)7 CEL.astToString (org.projectnessie.cel.CEL.astToString)6 Val (org.projectnessie.cel.common.types.ref.Val)5 CheckedExpr (com.google.api.expr.v1alpha1.CheckedExpr)4 Expr (com.google.api.expr.v1alpha1.Expr)4 ParsedExpr (com.google.api.expr.v1alpha1.ParsedExpr)4 CEL.astToCheckedExpr (org.projectnessie.cel.CEL.astToCheckedExpr)4 CEL.astToParsedExpr (org.projectnessie.cel.CEL.astToParsedExpr)4 UnknownT (org.projectnessie.cel.common.types.UnknownT)4 PartialActivation (org.projectnessie.cel.interpreter.Activation.PartialActivation)4 Type (com.google.api.expr.v1alpha1.Type)3 TypeRegistry (org.projectnessie.cel.common.types.ref.TypeRegistry)3 EvalState (org.projectnessie.cel.interpreter.EvalState)3 Macro (org.projectnessie.cel.parser.Macro)3