Search in sources :

Example 1 with EvalResult

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

the class CELTest method HomogeneousAggregateLiterals.

@Test
void HomogeneousAggregateLiterals() {
    Env e = newCustomEnv(declarations(Decls.newVar("name", Decls.String), Decls.newFunction(Operator.In.id, Decls.newOverload(Overloads.InList, asList(Decls.String, Decls.newListType(Decls.String)), Decls.Bool), Decls.newOverload(Overloads.InMap, asList(Decls.String, Decls.newMapType(Decls.String, Decls.Bool)), Decls.Bool))), homogeneousAggregateLiterals());
    // t.Run("err_list", func(t *testing.T) {
    AstIssuesTuple xIss = e.compile("name in ['hello', 0]");
    assertThat(xIss.getIssues()).isNotNull();
    assertThat(xIss.hasIssues()).isTrue();
    // })
    // t.Run("err_map_key", func(t *testing.T) {
    xIss = e.compile("name in {'hello':'world', 1:'!'}");
    assertThat(xIss.getIssues()).isNotNull();
    assertThat(xIss.hasIssues()).isTrue();
    // })
    // t.Run("err_map_val", func(t *testing.T) {
    xIss = e.compile("name in {'hello':'world', 'goodbye':true}");
    assertThat(xIss.getIssues()).isNotNull();
    assertThat(xIss.hasIssues()).isTrue();
    // })
    ProgramOption funcs = functions(Overload.binary(Operator.In.id, (lhs, rhs) -> {
        if (rhs.type().hasTrait(Trait.ContainerType)) {
            return ((Container) rhs).contains(lhs);
        }
        return valOrErr(rhs, "no such overload");
    }));
    // t.Run("ok_list", func(t *testing.T) {
    AstIssuesTuple astIss = e.compile("name in ['hello', 'world']");
    assertThat(astIss.hasIssues()).isFalse();
    Program prg = e.program(astIss.getAst(), funcs);
    EvalResult out = prg.eval(mapOf("name", "world"));
    assertThat(out.getVal()).isSameAs(True);
    // })
    // t.Run("ok_map", func(t *testing.T) {
    astIss = e.compile("name in {'hello': false, 'world': true}");
    assertThat(astIss.hasIssues()).isFalse();
    prg = e.program(astIss.getAst(), funcs);
    out = prg.eval(mapOf("name", "world"));
    assertThat(out.getVal()).isSameAs(True);
// })
}
Also used : BoolT(org.projectnessie.cel.common.types.BoolT) Interpretable(org.projectnessie.cel.interpreter.Interpretable) Macro(org.projectnessie.cel.parser.Macro) Call(com.google.api.expr.v1alpha1.Expr.Call) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) PartialActivation(org.projectnessie.cel.interpreter.Activation.PartialActivation) EnvOption.container(org.projectnessie.cel.EnvOption.container) Macro.newReceiverMacro(org.projectnessie.cel.parser.Macro.newReceiverMacro) Activation.emptyActivation(org.projectnessie.cel.interpreter.Activation.emptyActivation) Disabled(org.junit.jupiter.api.Disabled) InterpretableDecorator(org.projectnessie.cel.interpreter.InterpretableDecorator) Collections.singletonList(java.util.Collections.singletonList) True(org.projectnessie.cel.common.types.BoolT.True) Err.isError(org.projectnessie.cel.common.types.Err.isError) Container(org.projectnessie.cel.common.types.traits.Container) Arrays.asList(java.util.Arrays.asList) CEL.estimateCost(org.projectnessie.cel.CEL.estimateCost) OptPartialEval(org.projectnessie.cel.EvalOption.OptPartialEval) ProtoTypeRegistry.newEmptyRegistry(org.projectnessie.cel.common.types.pb.ProtoTypeRegistry.newEmptyRegistry) ParsedExpr(com.google.api.expr.v1alpha1.ParsedExpr) EnvOption.customTypeProvider(org.projectnessie.cel.EnvOption.customTypeProvider) Val(org.projectnessie.cel.common.types.ref.Val) TypeRegistry(org.projectnessie.cel.common.types.ref.TypeRegistry) InterpretableAttribute(org.projectnessie.cel.interpreter.Interpretable.InterpretableAttribute) Collections.emptyList(java.util.Collections.emptyList) Expr(com.google.api.expr.v1alpha1.Expr) Ident(com.google.api.expr.v1alpha1.Expr.Ident) InterpretableCall(org.projectnessie.cel.interpreter.Interpretable.InterpretableCall) OptExhaustiveEval(org.projectnessie.cel.EvalOption.OptExhaustiveEval) OptTrackState(org.projectnessie.cel.EvalOption.OptTrackState) StringT.stringOf(org.projectnessie.cel.common.types.StringT.stringOf) Decls(org.projectnessie.cel.checker.Decls) CEL.attributePattern(org.projectnessie.cel.CEL.attributePattern) Executors(java.util.concurrent.Executors) Test(org.junit.jupiter.api.Test) ProgramOption.functions(org.projectnessie.cel.ProgramOption.functions) DefaultTypeAdapter(org.projectnessie.cel.common.types.pb.DefaultTypeAdapter) Overload(org.projectnessie.cel.interpreter.functions.Overload) EvalResult(org.projectnessie.cel.Program.EvalResult) Err.newErr(org.projectnessie.cel.common.types.Err.newErr) IntOne(org.projectnessie.cel.common.types.IntT.IntOne) IntStream(java.util.stream.IntStream) Overloads(org.projectnessie.cel.common.types.Overloads) Env.newEnv(org.projectnessie.cel.Env.newEnv) StdLib(org.projectnessie.cel.Library.StdLib) IntT(org.projectnessie.cel.common.types.IntT) Trait(org.projectnessie.cel.common.types.traits.Trait) EnvOption.declarations(org.projectnessie.cel.EnvOption.declarations) CompletableFuture(java.util.concurrent.CompletableFuture) Err.valOrErr(org.projectnessie.cel.common.types.Err.valOrErr) CEL.partialVars(org.projectnessie.cel.CEL.partialVars) AtomicReference(java.util.concurrent.atomic.AtomicReference) EnvOption.customTypeAdapter(org.projectnessie.cel.EnvOption.customTypeAdapter) AstIssuesTuple(org.projectnessie.cel.Env.AstIssuesTuple) Mapper(org.projectnessie.cel.common.types.traits.Mapper) ProgramOption.evalOptions(org.projectnessie.cel.ProgramOption.evalOptions) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Type(com.google.api.expr.v1alpha1.Type) CEL.astToCheckedExpr(org.projectnessie.cel.CEL.astToCheckedExpr) CEL.noVars(org.projectnessie.cel.CEL.noVars) ExecutorService(java.util.concurrent.ExecutorService) Collections.emptyMap(java.util.Collections.emptyMap) CEL.astToString(org.projectnessie.cel.CEL.astToString) ProgramOption.globals(org.projectnessie.cel.ProgramOption.globals) Operator(org.projectnessie.cel.common.operators.Operator) NamespacedAttribute(org.projectnessie.cel.interpreter.AttributeFactory.NamespacedAttribute) UnknownT(org.projectnessie.cel.common.types.UnknownT) Cost(org.projectnessie.cel.interpreter.Coster.Cost) EnvOption.types(org.projectnessie.cel.EnvOption.types) ProgramOption.customDecorator(org.projectnessie.cel.ProgramOption.customDecorator) EnvOption.macros(org.projectnessie.cel.EnvOption.macros) Interpretable.newConstValue(org.projectnessie.cel.interpreter.Interpretable.newConstValue) CheckedExpr(com.google.api.expr.v1alpha1.CheckedExpr) CEL.parsedExprToAst(org.projectnessie.cel.CEL.parsedExprToAst) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) InterpretableConst(org.projectnessie.cel.interpreter.Interpretable.InterpretableConst) CEL.astToParsedExpr(org.projectnessie.cel.CEL.astToParsedExpr) CEL.checkedExprToAst(org.projectnessie.cel.CEL.checkedExprToAst) EvalState(org.projectnessie.cel.interpreter.EvalState) Util.mapOf(org.projectnessie.cel.Util.mapOf) StringT(org.projectnessie.cel.common.types.StringT) EnvOption.homogeneousAggregateLiterals(org.projectnessie.cel.EnvOption.homogeneousAggregateLiterals) Env.newCustomEnv(org.projectnessie.cel.Env.newCustomEnv) EnvOption.abbrevs(org.projectnessie.cel.EnvOption.abbrevs) 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 2 with EvalResult

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

the class CELTest method GlobalVars.

@Test
void GlobalVars() {
    Type mapStrDyn = Decls.newMapType(Decls.String, Decls.Dyn);
    Env e = newEnv(declarations(Decls.newVar("attrs", mapStrDyn), Decls.newVar("default", Decls.Dyn), Decls.newFunction("get", Decls.newInstanceOverload("get_map", asList(mapStrDyn, Decls.String, Decls.Dyn), Decls.Dyn))));
    AstIssuesTuple astIss = e.compile("attrs.get(\"first\", attrs.get(\"second\", default))");
    // Create the program.
    ProgramOption funcs = functions(Overload.function("get", args -> {
        if (args.length != 3) {
            return newErr("invalid arguments to 'get'");
        }
        if (!(args[0] instanceof Mapper)) {
            return newErr("invalid operand of type '%s' to obj.get(key, def)", args[0].type());
        }
        Mapper attrs = (Mapper) args[0];
        if (!(args[1] instanceof StringT)) {
            return newErr("invalid key of type '%s' to obj.get(key, def)", args[1].type());
        }
        StringT key = (StringT) args[1];
        Val defVal = args[2];
        if (attrs.contains(key) == True) {
            return attrs.get(key);
        }
        return defVal;
    }));
    // Global variables can be configured as a ProgramOption and optionally overridden on Eval.
    Program prg = e.program(astIss.getAst(), funcs, globals(mapOf("default", "third")));
    // t.Run("global_default", func(t *testing.T) {
    Object vars = mapOf("attrs", mapOf());
    EvalResult out = prg.eval(vars);
    assertThat(out.getVal().equal(stringOf("third"))).isSameAs(True);
    // })
    // t.Run("attrs_alt", func(t *testing.T) {
    vars = mapOf("attrs", mapOf("second", "yep"));
    out = prg.eval(vars);
    assertThat(out.getVal().equal(stringOf("yep"))).isSameAs(True);
    // })
    // t.Run("local_default", func(t *testing.T) {
    vars = mapOf("attrs", mapOf(), "default", "fourth");
    out = prg.eval(vars);
    assertThat(out.getVal().equal(stringOf("fourth"))).isSameAs(True);
// })
}
Also used : BoolT(org.projectnessie.cel.common.types.BoolT) Interpretable(org.projectnessie.cel.interpreter.Interpretable) Macro(org.projectnessie.cel.parser.Macro) Call(com.google.api.expr.v1alpha1.Expr.Call) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) PartialActivation(org.projectnessie.cel.interpreter.Activation.PartialActivation) EnvOption.container(org.projectnessie.cel.EnvOption.container) Macro.newReceiverMacro(org.projectnessie.cel.parser.Macro.newReceiverMacro) Activation.emptyActivation(org.projectnessie.cel.interpreter.Activation.emptyActivation) Disabled(org.junit.jupiter.api.Disabled) InterpretableDecorator(org.projectnessie.cel.interpreter.InterpretableDecorator) Collections.singletonList(java.util.Collections.singletonList) True(org.projectnessie.cel.common.types.BoolT.True) Err.isError(org.projectnessie.cel.common.types.Err.isError) Container(org.projectnessie.cel.common.types.traits.Container) Arrays.asList(java.util.Arrays.asList) CEL.estimateCost(org.projectnessie.cel.CEL.estimateCost) OptPartialEval(org.projectnessie.cel.EvalOption.OptPartialEval) ProtoTypeRegistry.newEmptyRegistry(org.projectnessie.cel.common.types.pb.ProtoTypeRegistry.newEmptyRegistry) ParsedExpr(com.google.api.expr.v1alpha1.ParsedExpr) EnvOption.customTypeProvider(org.projectnessie.cel.EnvOption.customTypeProvider) Val(org.projectnessie.cel.common.types.ref.Val) TypeRegistry(org.projectnessie.cel.common.types.ref.TypeRegistry) InterpretableAttribute(org.projectnessie.cel.interpreter.Interpretable.InterpretableAttribute) Collections.emptyList(java.util.Collections.emptyList) Expr(com.google.api.expr.v1alpha1.Expr) Ident(com.google.api.expr.v1alpha1.Expr.Ident) InterpretableCall(org.projectnessie.cel.interpreter.Interpretable.InterpretableCall) OptExhaustiveEval(org.projectnessie.cel.EvalOption.OptExhaustiveEval) OptTrackState(org.projectnessie.cel.EvalOption.OptTrackState) StringT.stringOf(org.projectnessie.cel.common.types.StringT.stringOf) Decls(org.projectnessie.cel.checker.Decls) CEL.attributePattern(org.projectnessie.cel.CEL.attributePattern) Executors(java.util.concurrent.Executors) Test(org.junit.jupiter.api.Test) ProgramOption.functions(org.projectnessie.cel.ProgramOption.functions) DefaultTypeAdapter(org.projectnessie.cel.common.types.pb.DefaultTypeAdapter) Overload(org.projectnessie.cel.interpreter.functions.Overload) EvalResult(org.projectnessie.cel.Program.EvalResult) Err.newErr(org.projectnessie.cel.common.types.Err.newErr) IntOne(org.projectnessie.cel.common.types.IntT.IntOne) IntStream(java.util.stream.IntStream) Overloads(org.projectnessie.cel.common.types.Overloads) Env.newEnv(org.projectnessie.cel.Env.newEnv) StdLib(org.projectnessie.cel.Library.StdLib) IntT(org.projectnessie.cel.common.types.IntT) Trait(org.projectnessie.cel.common.types.traits.Trait) EnvOption.declarations(org.projectnessie.cel.EnvOption.declarations) CompletableFuture(java.util.concurrent.CompletableFuture) Err.valOrErr(org.projectnessie.cel.common.types.Err.valOrErr) CEL.partialVars(org.projectnessie.cel.CEL.partialVars) AtomicReference(java.util.concurrent.atomic.AtomicReference) EnvOption.customTypeAdapter(org.projectnessie.cel.EnvOption.customTypeAdapter) AstIssuesTuple(org.projectnessie.cel.Env.AstIssuesTuple) Mapper(org.projectnessie.cel.common.types.traits.Mapper) ProgramOption.evalOptions(org.projectnessie.cel.ProgramOption.evalOptions) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Type(com.google.api.expr.v1alpha1.Type) CEL.astToCheckedExpr(org.projectnessie.cel.CEL.astToCheckedExpr) CEL.noVars(org.projectnessie.cel.CEL.noVars) ExecutorService(java.util.concurrent.ExecutorService) Collections.emptyMap(java.util.Collections.emptyMap) CEL.astToString(org.projectnessie.cel.CEL.astToString) ProgramOption.globals(org.projectnessie.cel.ProgramOption.globals) Operator(org.projectnessie.cel.common.operators.Operator) NamespacedAttribute(org.projectnessie.cel.interpreter.AttributeFactory.NamespacedAttribute) UnknownT(org.projectnessie.cel.common.types.UnknownT) Cost(org.projectnessie.cel.interpreter.Coster.Cost) EnvOption.types(org.projectnessie.cel.EnvOption.types) ProgramOption.customDecorator(org.projectnessie.cel.ProgramOption.customDecorator) EnvOption.macros(org.projectnessie.cel.EnvOption.macros) Interpretable.newConstValue(org.projectnessie.cel.interpreter.Interpretable.newConstValue) CheckedExpr(com.google.api.expr.v1alpha1.CheckedExpr) CEL.parsedExprToAst(org.projectnessie.cel.CEL.parsedExprToAst) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) InterpretableConst(org.projectnessie.cel.interpreter.Interpretable.InterpretableConst) CEL.astToParsedExpr(org.projectnessie.cel.CEL.astToParsedExpr) CEL.checkedExprToAst(org.projectnessie.cel.CEL.checkedExprToAst) EvalState(org.projectnessie.cel.interpreter.EvalState) Util.mapOf(org.projectnessie.cel.Util.mapOf) StringT(org.projectnessie.cel.common.types.StringT) EnvOption.homogeneousAggregateLiterals(org.projectnessie.cel.EnvOption.homogeneousAggregateLiterals) Env.newCustomEnv(org.projectnessie.cel.Env.newCustomEnv) EnvOption.abbrevs(org.projectnessie.cel.EnvOption.abbrevs) Val(org.projectnessie.cel.common.types.ref.Val) Mapper(org.projectnessie.cel.common.types.traits.Mapper) Type(com.google.api.expr.v1alpha1.Type) 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) StringT(org.projectnessie.cel.common.types.StringT) Test(org.junit.jupiter.api.Test)

Example 3 with EvalResult

use of org.projectnessie.cel.Program.EvalResult 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 4 with EvalResult

use of org.projectnessie.cel.Program.EvalResult 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 5 with EvalResult

use of org.projectnessie.cel.Program.EvalResult 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)

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