Search in sources :

Example 6 with AstIssuesTuple

use of org.projectnessie.cel.Env.AstIssuesTuple in project cel-java by projectnessie.

the class CELTest method EvalOptions.

@Test
void EvalOptions() {
    Env e = newEnv(declarations(Decls.newVar("k", Decls.String), Decls.newVar("v", Decls.Bool)));
    AstIssuesTuple astIss = e.compile("{k: true}[k] || v != false");
    Program prg = e.program(astIss.getAst(), evalOptions(OptExhaustiveEval));
    EvalResult outDetails = prg.eval(mapOf("k", "key", "v", true));
    assertThat(outDetails.getVal()).isSameAs(True);
    // Test to see whether 'v != false' was resolved to a value.
    // With short-circuiting it normally wouldn't be.
    EvalState s = outDetails.getEvalDetails().getState();
    Val lhsVal = s.value(astIss.getAst().getExpr().getCallExpr().getArgs(0).getId());
    assertThat(lhsVal).isSameAs(True);
    Val rhsVal = s.value(astIss.getAst().getExpr().getCallExpr().getArgs(1).getId());
    assertThat(rhsVal).isSameAs(True);
}
Also used : Val(org.projectnessie.cel.common.types.ref.Val) EvalResult(org.projectnessie.cel.Program.EvalResult) EvalState(org.projectnessie.cel.interpreter.EvalState) 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 7 with AstIssuesTuple

use of org.projectnessie.cel.Env.AstIssuesTuple in project cel-java by projectnessie.

the class CELTest method CustomMacro.

@Test
void CustomMacro() {
    Macro joinMacro = newReceiverMacro("join", 1, (eh, target, args) -> {
        Expr delim = args.get(0);
        Expr iterIdent = eh.ident("__iter__");
        Expr accuIdent = eh.ident("__result__");
        Expr init = eh.literalString("");
        Expr condition = eh.literalBool(true);
        Expr step = eh.globalCall(Operator.Conditional.id, eh.globalCall(Operator.Greater.id, eh.receiverCall("size", accuIdent, emptyList()), eh.literalInt(0)), eh.globalCall(Operator.Add.id, eh.globalCall(Operator.Add.id, accuIdent, delim), iterIdent), iterIdent);
        return eh.fold("__iter__", target, "__result__", init, condition, step, accuIdent);
    });
    Env e = newEnv(macros(joinMacro));
    AstIssuesTuple astIss = e.compile("['hello', 'cel', 'friend'].join(',')");
    assertThat(astIss.hasIssues()).isFalse();
    Program prg = e.program(astIss.getAst(), evalOptions(OptExhaustiveEval));
    EvalResult out = prg.eval(noVars());
    assertThat(out.getVal().equal(stringOf("hello,cel,friend"))).isSameAs(True);
}
Also used : ParsedExpr(com.google.api.expr.v1alpha1.ParsedExpr) Expr(com.google.api.expr.v1alpha1.Expr) CEL.astToCheckedExpr(org.projectnessie.cel.CEL.astToCheckedExpr) CheckedExpr(com.google.api.expr.v1alpha1.CheckedExpr) CEL.astToParsedExpr(org.projectnessie.cel.CEL.astToParsedExpr) EvalResult(org.projectnessie.cel.Program.EvalResult) Macro(org.projectnessie.cel.parser.Macro) Macro.newReceiverMacro(org.projectnessie.cel.parser.Macro.newReceiverMacro) 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 8 with AstIssuesTuple

use of org.projectnessie.cel.Env.AstIssuesTuple in project cel-java by projectnessie.

the class CELTest method CheckedExprToAst_ConstantExpr.

@Test
void CheckedExprToAst_ConstantExpr() {
    Env stdEnv = newEnv();
    String in = "10";
    AstIssuesTuple astIss = stdEnv.compile(in);
    assertThat(astIss.hasIssues()).isFalse();
    CheckedExpr expr = astToCheckedExpr(astIss.getAst());
    Ast ast2 = checkedExprToAst(expr);
    assertThat(ast2.getExpr()).isEqualTo(astIss.getAst().getExpr());
}
Also used : CEL.parsedExprToAst(org.projectnessie.cel.CEL.parsedExprToAst) CEL.checkedExprToAst(org.projectnessie.cel.CEL.checkedExprToAst) CEL.astToCheckedExpr(org.projectnessie.cel.CEL.astToCheckedExpr) CheckedExpr(com.google.api.expr.v1alpha1.CheckedExpr) 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 9 with AstIssuesTuple

use of org.projectnessie.cel.Env.AstIssuesTuple in project cel-java by projectnessie.

the class CELTest method Abbrevs_Compiled.

@Test
void Abbrevs_Compiled() {
    // Test whether abbreviations successfully resolve at type-check time (compile time).
    Env env = newEnv(abbrevs("qualified.identifier.name"), declarations(Decls.newVar("qualified.identifier.name.first", Decls.String)));
    // abbreviation resolved here.
    AstIssuesTuple astIss = env.compile("\"hello \"+ name.first");
    assertThat(astIss.hasIssues()).isFalse();
    Program prg = env.program(astIss.getAst());
    EvalResult out = prg.eval(mapOf("qualified.identifier.name.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 10 with AstIssuesTuple

use of org.projectnessie.cel.Env.AstIssuesTuple in project cel-java by projectnessie.

the class CELTest method AstToProto.

@Test
void AstToProto() {
    Env stdEnv = newEnv(declarations(Decls.newVar("a", Decls.Dyn), Decls.newVar("b", Decls.Dyn)));
    AstIssuesTuple astIss = stdEnv.parse("a + b");
    assertThat(astIss.hasIssues()).isFalse();
    ParsedExpr parsed = astToParsedExpr(astIss.getAst());
    Ast ast2 = parsedExprToAst(parsed);
    assertThat(ast2.getExpr()).isEqualTo(astIss.getAst().getExpr());
    assertThatThrownBy(() -> astToCheckedExpr(astIss.getAst())).isInstanceOf(IllegalArgumentException.class).hasMessage("cannot convert unchecked ast");
    AstIssuesTuple astIss2 = stdEnv.check(astIss.getAst());
    assertThat(astIss2.hasIssues()).isFalse();
    // assertThat(astIss.hasIssues()).isFalse();
    CheckedExpr checked = astToCheckedExpr(astIss2.getAst());
    Ast ast3 = checkedExprToAst(checked);
    assertThat(ast3.getExpr()).isEqualTo(astIss2.getAst().getExpr());
}
Also used : CEL.parsedExprToAst(org.projectnessie.cel.CEL.parsedExprToAst) CEL.checkedExprToAst(org.projectnessie.cel.CEL.checkedExprToAst) CEL.astToCheckedExpr(org.projectnessie.cel.CEL.astToCheckedExpr) CheckedExpr(com.google.api.expr.v1alpha1.CheckedExpr) ParsedExpr(com.google.api.expr.v1alpha1.ParsedExpr) CEL.astToParsedExpr(org.projectnessie.cel.CEL.astToParsedExpr) 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

AstIssuesTuple (org.projectnessie.cel.Env.AstIssuesTuple)26 Env.newCustomEnv (org.projectnessie.cel.Env.newCustomEnv)26 Env.newEnv (org.projectnessie.cel.Env.newEnv)26 Test (org.junit.jupiter.api.Test)24 EvalResult (org.projectnessie.cel.Program.EvalResult)17 CEL.checkedExprToAst (org.projectnessie.cel.CEL.checkedExprToAst)12 CEL.parsedExprToAst (org.projectnessie.cel.CEL.parsedExprToAst)12 CEL.astToString (org.projectnessie.cel.CEL.astToString)11 CheckedExpr (com.google.api.expr.v1alpha1.CheckedExpr)10 CEL.astToCheckedExpr (org.projectnessie.cel.CEL.astToCheckedExpr)10 ParsedExpr (com.google.api.expr.v1alpha1.ParsedExpr)8 CEL.astToParsedExpr (org.projectnessie.cel.CEL.astToParsedExpr)8 Expr (com.google.api.expr.v1alpha1.Expr)7 Type (com.google.api.expr.v1alpha1.Type)6 CEL.estimateCost (org.projectnessie.cel.CEL.estimateCost)6 Call (com.google.api.expr.v1alpha1.Expr.Call)5 Ident (com.google.api.expr.v1alpha1.Expr.Ident)5 Arrays.asList (java.util.Arrays.asList)5 Collections.emptyList (java.util.Collections.emptyList)5 Collections.emptyMap (java.util.Collections.emptyMap)5