Search in sources :

Example 1 with PartialActivation

use of org.projectnessie.cel.interpreter.Activation.PartialActivation 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 2 with PartialActivation

use of org.projectnessie.cel.interpreter.Activation.PartialActivation 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 3 with PartialActivation

use of org.projectnessie.cel.interpreter.Activation.PartialActivation 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 4 with PartialActivation

use of org.projectnessie.cel.interpreter.Activation.PartialActivation in project cel-java by projectnessie.

the class CELTest method residualAst_Modified.

@Test
void residualAst_Modified() {
    Env e = newEnv(declarations(Decls.newVar("x", Decls.newMapType(Decls.String, Decls.Int)), Decls.newVar("y", Decls.Int)));
    AstIssuesTuple astIss = e.parse("x == y");
    Program prg = e.program(astIss.getAst(), evalOptions(OptTrackState, OptPartialEval));
    for (int x = 123; x < 456; x++) {
        PartialActivation vars = partialVars(mapOf("x", x), attributePattern("y"));
        EvalResult outDet = prg.eval(vars);
        assertThat(outDet.getVal()).matches(UnknownT::isUnknown);
        Ast residual = e.residualAst(astIss.getAst(), outDet.getEvalDetails());
        String orig = astToString(astIss.getAst());
        assertThat(orig).isEqualTo("x == y");
        String expr = astToString(residual);
        String want = String.format("%d == y", x);
        assertThat(expr).isEqualTo(want);
    }
}
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 5 with PartialActivation

use of org.projectnessie.cel.interpreter.Activation.PartialActivation in project cel-java by projectnessie.

the class AttributePatternsTest method unknownResolution.

@ParameterizedTest
@MethodSource("attributePatternsTestCases")
void unknownResolution(PatternTest tst) {
    Assumptions.assumeTrue(tst.disabled == null, tst.disabled);
    TypeRegistry reg = newRegistry();
    for (int i = 0; i < tst.matches.length; i++) {
        Attr m = tst.matches[i];
        Container cont = Container.defaultContainer;
        if (m.unchecked) {
            cont = Container.newContainer(Container.name(m.container));
        }
        AttributeFactory fac = newPartialAttributeFactory(cont, reg, reg);
        Attribute attr = genAttr(fac, m);
        PartialActivation partVars = newPartialActivation(emptyActivation(), tst.pattern);
        Object val = attr.resolve(partVars);
        assertThat(val).withFailMessage(() -> String.format("match: '%s', gen: '%s'", m, attr)).isInstanceOf(UnknownT.class);
    }
    for (int i = 0; i < tst.misses.length; i++) {
        Attr m = tst.misses[i];
        Container cont = Container.defaultContainer;
        if (m.unchecked) {
            cont = Container.newContainer(Container.name(m.container));
        }
        AttributeFactory fac = newPartialAttributeFactory(cont, reg, reg);
        Attribute attr = genAttr(fac, m);
        PartialActivation partVars = newPartialActivation(emptyActivation(), tst.pattern);
        assertThatThrownBy(() -> attr.resolve(partVars), "miss: '%s', gen: '%s'", m, attr);
    }
}
Also used : Container(org.projectnessie.cel.common.containers.Container) PartialActivation(org.projectnessie.cel.interpreter.Activation.PartialActivation) Activation.newPartialActivation(org.projectnessie.cel.interpreter.Activation.newPartialActivation) NamespacedAttribute(org.projectnessie.cel.interpreter.AttributeFactory.NamespacedAttribute) Attribute(org.projectnessie.cel.interpreter.AttributeFactory.Attribute) AttributePattern.newPartialAttributeFactory(org.projectnessie.cel.interpreter.AttributePattern.newPartialAttributeFactory) TypeRegistry(org.projectnessie.cel.common.types.ref.TypeRegistry) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

PartialActivation (org.projectnessie.cel.interpreter.Activation.PartialActivation)6 Test (org.junit.jupiter.api.Test)5 CEL.astToString (org.projectnessie.cel.CEL.astToString)4 CEL.checkedExprToAst (org.projectnessie.cel.CEL.checkedExprToAst)4 CEL.parsedExprToAst (org.projectnessie.cel.CEL.parsedExprToAst)4 AstIssuesTuple (org.projectnessie.cel.Env.AstIssuesTuple)4 Env.newCustomEnv (org.projectnessie.cel.Env.newCustomEnv)4 Env.newEnv (org.projectnessie.cel.Env.newEnv)4 EvalResult (org.projectnessie.cel.Program.EvalResult)4 UnknownT (org.projectnessie.cel.common.types.UnknownT)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 TypeRegistry (org.projectnessie.cel.common.types.ref.TypeRegistry)2 Activation.newPartialActivation (org.projectnessie.cel.interpreter.Activation.newPartialActivation)2 NamespacedAttribute (org.projectnessie.cel.interpreter.AttributeFactory.NamespacedAttribute)2 AttributePattern.newPartialAttributeFactory (org.projectnessie.cel.interpreter.AttributePattern.newPartialAttributeFactory)2 MethodSource (org.junit.jupiter.params.provider.MethodSource)1 Container (org.projectnessie.cel.common.containers.Container)1 Attribute (org.projectnessie.cel.interpreter.AttributeFactory.Attribute)1 Qualifier (org.projectnessie.cel.interpreter.AttributeFactory.Qualifier)1