Search in sources :

Example 1 with CheckerEnv

use of org.projectnessie.cel.checker.CheckerEnv in project cel-java by projectnessie.

the class InterpreterTest method setProto2PrimitiveFields.

@Test
void setProto2PrimitiveFields() {
    // Test the use of proto2 primitives within object construction.
    Source src = newTextSource("input == TestAllTypes{\n" + "  single_int32: 1,\n" + "  single_int64: 2,\n" + "  single_uint32: 3u,\n" + "  single_uint64: 4u,\n" + "  single_float: -3.3,\n" + "  single_double: -2.2,\n" + "  single_string: \"hello world\",\n" + "  single_bool: true\n" + "}");
    ParseResult parsed = Parser.parseAllMacros(src);
    assertThat(parsed.hasErrors()).withFailMessage(parsed.getErrors()::toDisplayString).isFalse();
    Container cont = testContainer("google.api.expr.test.v1.proto2");
    TypeRegistry reg = newRegistry(com.google.api.expr.test.v1.proto2.TestAllTypesProto.TestAllTypes.getDefaultInstance());
    CheckerEnv env = newStandardCheckerEnv(cont, reg);
    env.add(singletonList(Decls.newVar("input", Decls.newObjectType("google.api.expr.test.v1.proto2.TestAllTypes"))));
    CheckResult checkResult = Checker.Check(parsed, src, env);
    if (parsed.hasErrors()) {
        throw new IllegalArgumentException(parsed.getErrors().toDisplayString());
    }
    AttributeFactory attrs = newAttributeFactory(cont, reg, reg);
    Interpreter i = newStandardInterpreter(cont, reg, reg, attrs);
    Interpretable eval = i.newInterpretable(checkResult.getCheckedExpr());
    int one = 1;
    long two = 2L;
    int three = 3;
    long four = 4L;
    float five = -3.3f;
    double six = -2.2d;
    String str = "hello world";
    boolean truth = true;
    com.google.api.expr.test.v1.proto2.TestAllTypesProto.TestAllTypes input = com.google.api.expr.test.v1.proto2.TestAllTypesProto.TestAllTypes.newBuilder().setSingleInt32(one).setSingleInt64(two).setSingleUint32(three).setSingleUint64(four).setSingleFloat(five).setSingleDouble(six).setSingleString(str).setSingleBool(truth).build();
    Activation vars = newActivation(mapOf("input", reg.nativeToValue(input)));
    Val result = eval.eval(vars);
    assertThat(result.value()).isInstanceOf(Boolean.class);
    boolean got = (Boolean) result.value();
    assertThat(got).isTrue();
}
Also used : Val(org.projectnessie.cel.common.types.ref.Val) TestAllTypes(com.google.api.expr.test.v1.proto2.TestAllTypesProto.TestAllTypes) Interpreter.newStandardInterpreter(org.projectnessie.cel.interpreter.Interpreter.newStandardInterpreter) Interpreter.newInterpreter(org.projectnessie.cel.interpreter.Interpreter.newInterpreter) ParseResult(org.projectnessie.cel.parser.Parser.ParseResult) AttributePattern.newPartialAttributeFactory(org.projectnessie.cel.interpreter.AttributePattern.newPartialAttributeFactory) AttributeFactory.newAttributeFactory(org.projectnessie.cel.interpreter.AttributeFactory.newAttributeFactory) Activation.newPartialActivation(org.projectnessie.cel.interpreter.Activation.newPartialActivation) Activation.emptyActivation(org.projectnessie.cel.interpreter.Activation.emptyActivation) Activation.newActivation(org.projectnessie.cel.interpreter.Activation.newActivation) ByteString(com.google.protobuf.ByteString) TypeRegistry(org.projectnessie.cel.common.types.ref.TypeRegistry) Source(org.projectnessie.cel.common.Source) MethodSource(org.junit.jupiter.params.provider.MethodSource) Source.newTextSource(org.projectnessie.cel.common.Source.newTextSource) Container.newContainer(org.projectnessie.cel.common.containers.Container.newContainer) Container(org.projectnessie.cel.common.containers.Container) CheckResult(org.projectnessie.cel.checker.Checker.CheckResult) CheckerEnv(org.projectnessie.cel.checker.CheckerEnv) CheckerEnv.newStandardCheckerEnv(org.projectnessie.cel.checker.CheckerEnv.newStandardCheckerEnv) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 2 with CheckerEnv

use of org.projectnessie.cel.checker.CheckerEnv in project cel-java by projectnessie.

the class InterpreterTest method missingIdentInSelect.

@Test
void missingIdentInSelect() {
    Source src = newTextSource("a.b.c");
    ParseResult parsed = Parser.parseAllMacros(src);
    assertThat(parsed.hasErrors()).withFailMessage(parsed.getErrors()::toDisplayString).isFalse();
    Container cont = testContainer("test");
    TypeRegistry reg = newRegistry();
    CheckerEnv env = newStandardCheckerEnv(cont, reg);
    env.add(Decls.newVar("a.b", Decls.Dyn));
    CheckResult checkResult = Checker.Check(parsed, src, env);
    if (parsed.hasErrors()) {
        throw new IllegalArgumentException(parsed.getErrors().toDisplayString());
    }
    AttributeFactory attrs = newPartialAttributeFactory(cont, reg, reg);
    Interpreter interp = newStandardInterpreter(cont, reg, reg, attrs);
    Interpretable i = interp.newInterpretable(checkResult.getCheckedExpr());
    Activation vars = newPartialActivation(mapOf("a.b", mapOf("d", "hello")), newAttributePattern("a.b").qualString("c"));
    Val result = i.eval(vars);
    assertThat(result).isInstanceOf(UnknownT.class);
    result = i.eval(emptyActivation());
    assertThat(result).isInstanceOf(Err.class);
}
Also used : Val(org.projectnessie.cel.common.types.ref.Val) Container.newContainer(org.projectnessie.cel.common.containers.Container.newContainer) Container(org.projectnessie.cel.common.containers.Container) Interpreter.newStandardInterpreter(org.projectnessie.cel.interpreter.Interpreter.newStandardInterpreter) Interpreter.newInterpreter(org.projectnessie.cel.interpreter.Interpreter.newInterpreter) ParseResult(org.projectnessie.cel.parser.Parser.ParseResult) CheckResult(org.projectnessie.cel.checker.Checker.CheckResult) CheckerEnv(org.projectnessie.cel.checker.CheckerEnv) CheckerEnv.newStandardCheckerEnv(org.projectnessie.cel.checker.CheckerEnv.newStandardCheckerEnv) AttributePattern.newPartialAttributeFactory(org.projectnessie.cel.interpreter.AttributePattern.newPartialAttributeFactory) AttributeFactory.newAttributeFactory(org.projectnessie.cel.interpreter.AttributeFactory.newAttributeFactory) Activation.newPartialActivation(org.projectnessie.cel.interpreter.Activation.newPartialActivation) Activation.emptyActivation(org.projectnessie.cel.interpreter.Activation.emptyActivation) Activation.newActivation(org.projectnessie.cel.interpreter.Activation.newActivation) TypeRegistry(org.projectnessie.cel.common.types.ref.TypeRegistry) Source(org.projectnessie.cel.common.Source) MethodSource(org.junit.jupiter.params.provider.MethodSource) Source.newTextSource(org.projectnessie.cel.common.Source.newTextSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 3 with CheckerEnv

use of org.projectnessie.cel.checker.CheckerEnv in project cel-java by projectnessie.

the class Env method check.

/**
 * Check performs type-checking on the input Ast and yields a checked Ast and/or set of Issues.
 *
 * <p>Checking has failed if the returned Issues value and its Issues.Err() value are non-nil.
 * Issues should be inspected if they are non-nil, but may not represent a fatal error.
 *
 * <p>It is possible to have both non-nil Ast and Issues values returned from this call: however,
 * the mere presence of an Ast does not imply that it is valid for use.
 */
public AstIssuesTuple check(Ast ast) {
    // Note, errors aren't currently possible on the Ast to ParsedExpr conversion.
    ParsedExpr pe = astToParsedExpr(ast);
    // Construct the internal checker env, erroring if there is an issue adding the declarations.
    synchronized (once) {
        if (chk == null && chkErr == null) {
            CheckerEnv ce = CheckerEnv.newCheckerEnv(container, provider);
            ce.enableDynamicAggregateLiterals(true);
            if (hasFeature(FeatureDisableDynamicAggregateLiterals)) {
                ce.enableDynamicAggregateLiterals(false);
            }
            try {
                ce.add(declarations);
                chk = ce;
            } catch (RuntimeException e) {
                chkErr = e;
            } catch (Exception e) {
                chkErr = new RuntimeException(e);
            }
        }
    }
    // The once call will ensure that this value is set or nil for all invocations.
    if (chkErr != null) {
        Errors errs = new Errors(ast.getSource());
        errs.reportError(NoLocation, "%s", chkErr.toString());
        return new AstIssuesTuple(null, newIssues(errs));
    }
    ParseResult pr = new ParseResult(pe.getExpr(), new Errors(ast.getSource()), pe.getSourceInfo());
    CheckResult checkRes = Checker.Check(pr, ast.getSource(), chk);
    if (checkRes.hasErrors()) {
        return new AstIssuesTuple(null, newIssues(checkRes.getErrors()));
    }
    // Manually create the Ast to ensure that the Ast source information (which may be more
    // detailed than the information provided by Check), is returned to the caller.
    CheckedExpr ce = checkRes.getCheckedExpr();
    ast = new Ast(ce.getExpr(), ce.getSourceInfo(), ast.getSource(), ce.getReferenceMapMap(), ce.getTypeMapMap());
    return new AstIssuesTuple(ast, Issues.noIssues(ast.getSource()));
}
Also used : Errors(org.projectnessie.cel.common.Errors) AstPruner.pruneAst(org.projectnessie.cel.interpreter.AstPruner.pruneAst) CEL.parsedExprToAst(org.projectnessie.cel.CEL.parsedExprToAst) ParseResult(org.projectnessie.cel.parser.Parser.ParseResult) CheckResult(org.projectnessie.cel.checker.Checker.CheckResult) CheckedExpr(com.google.api.expr.v1alpha1.CheckedExpr) CheckerEnv(org.projectnessie.cel.checker.CheckerEnv) ParsedExpr(com.google.api.expr.v1alpha1.ParsedExpr) CEL.astToParsedExpr(org.projectnessie.cel.CEL.astToParsedExpr)

Example 4 with CheckerEnv

use of org.projectnessie.cel.checker.CheckerEnv in project cel-java by projectnessie.

the class AttributesTest method attributeStateTracking.

@ParameterizedTest
@MethodSource("attributeStateTrackingTests")
void attributeStateTracking(TestDef tc) {
    Source src = Source.newTextSource(tc.expr);
    ParseResult parsed = Parser.parseAllMacros(src);
    assertThat(parsed.hasErrors()).isFalse();
    Container cont = Container.defaultContainer;
    TypeRegistry reg = newRegistry();
    CheckerEnv env = newStandardCheckerEnv(cont, reg);
    if (tc.env != null) {
        env.add(tc.env);
    }
    CheckResult checkResult = Checker.Check(parsed, src, env);
    if (parsed.hasErrors()) {
        throw new IllegalArgumentException(parsed.getErrors().toDisplayString());
    }
    AttributeFactory attrs = newAttributeFactory(cont, reg, reg);
    Interpreter interp = newStandardInterpreter(cont, reg, reg, attrs);
    // Show that program planning will now produce an error.
    EvalState st = newEvalState();
    Interpretable i = interp.newInterpretable(checkResult.getCheckedExpr(), optimize(), trackState(st));
    Activation in = newActivation(tc.in);
    Val out = i.eval(in);
    assertThat(out).extracting(o -> o.equal(tc.out)).isSameAs(True);
    for (Entry<Object, Object> iv : tc.state.entrySet()) {
        long id = ((Number) iv.getKey()).longValue();
        Object val = iv.getValue();
        Val stVal = st.value(id);
        assertThat(stVal).withFailMessage(() -> String.format("id(%d), val=%s, stVal=%s", id, val, stVal)).isNotNull();
        assertThat(stVal).withFailMessage(() -> String.format("id(%d), val=%s, stVal=%s", id, val, stVal)).isEqualTo(DefaultTypeAdapter.Instance.nativeToValue(val));
        deepEquals(String.format("id(%d)", id), stVal.value(), val);
    }
}
Also used : Val(org.projectnessie.cel.common.types.ref.Val) Cost.estimateCost(org.projectnessie.cel.interpreter.Coster.Cost.estimateCost) Arrays(java.util.Arrays) AttributePattern.newPartialAttributeFactory(org.projectnessie.cel.interpreter.AttributePattern.newPartialAttributeFactory) ParseResult(org.projectnessie.cel.parser.Parser.ParseResult) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Interpreter.optimize(org.projectnessie.cel.interpreter.Interpreter.optimize) Source(org.projectnessie.cel.common.Source) AttributePattern.newAttributePattern(org.projectnessie.cel.interpreter.AttributePattern.newAttributePattern) Activation.emptyActivation(org.projectnessie.cel.interpreter.Activation.emptyActivation) IntType(org.projectnessie.cel.common.types.IntT.IntType) True(org.projectnessie.cel.common.types.BoolT.True) Interpreter.trackState(org.projectnessie.cel.interpreter.Interpreter.trackState) Map(java.util.Map) CheckerEnv.newStandardCheckerEnv(org.projectnessie.cel.checker.CheckerEnv.newStandardCheckerEnv) Val(org.projectnessie.cel.common.types.ref.Val) Container.newContainer(org.projectnessie.cel.common.containers.Container.newContainer) TypeRegistry(org.projectnessie.cel.common.types.ref.TypeRegistry) MethodSource(org.junit.jupiter.params.provider.MethodSource) False(org.projectnessie.cel.common.types.BoolT.False) StringT.stringOf(org.projectnessie.cel.common.types.StringT.stringOf) Decls(org.projectnessie.cel.checker.Decls) IntT.intOf(org.projectnessie.cel.common.types.IntT.intOf) Test(org.junit.jupiter.api.Test) CheckerEnv(org.projectnessie.cel.checker.CheckerEnv) List(java.util.List) UnknownT.unknownOf(org.projectnessie.cel.common.types.UnknownT.unknownOf) Entry(java.util.Map.Entry) DefaultTypeAdapter(org.projectnessie.cel.common.types.pb.DefaultTypeAdapter) Activation.newPartialActivation(org.projectnessie.cel.interpreter.Activation.newPartialActivation) Any(com.google.protobuf.Any) Err.newErr(org.projectnessie.cel.common.types.Err.newErr) NestedMessage(com.google.api.expr.test.v1.proto3.TestAllTypesProto.TestAllTypes.NestedMessage) Checker(org.projectnessie.cel.checker.Checker) CheckResult(org.projectnessie.cel.checker.Checker.CheckResult) Parser(org.projectnessie.cel.parser.Parser) EvalState.newEvalState(org.projectnessie.cel.interpreter.EvalState.newEvalState) Util.deepEquals(org.projectnessie.cel.Util.deepEquals) Qualifier(org.projectnessie.cel.interpreter.AttributeFactory.Qualifier) ProtoTypeRegistry.newRegistry(org.projectnessie.cel.common.types.pb.ProtoTypeRegistry.newRegistry) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Type(com.google.api.expr.v1alpha1.Type) AttributeFactory.newAttributeFactory(org.projectnessie.cel.interpreter.AttributeFactory.newAttributeFactory) NamespacedAttribute(org.projectnessie.cel.interpreter.AttributeFactory.NamespacedAttribute) UnknownT(org.projectnessie.cel.common.types.UnknownT) Container(org.projectnessie.cel.common.containers.Container) Interpretable.newConstValue(org.projectnessie.cel.interpreter.Interpretable.newConstValue) Attribute(org.projectnessie.cel.interpreter.AttributeFactory.Attribute) Decl(com.google.api.expr.v1alpha1.Decl) InterpretableConst(org.projectnessie.cel.interpreter.Interpretable.InterpretableConst) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Activation.newActivation(org.projectnessie.cel.interpreter.Activation.newActivation) TestAllTypes(com.google.api.expr.test.v1.proto3.TestAllTypesProto.TestAllTypes) Util.mapOf(org.projectnessie.cel.Util.mapOf) Interpreter.newStandardInterpreter(org.projectnessie.cel.interpreter.Interpreter.newStandardInterpreter) Interpreter.newStandardInterpreter(org.projectnessie.cel.interpreter.Interpreter.newStandardInterpreter) ParseResult(org.projectnessie.cel.parser.Parser.ParseResult) AttributePattern.newPartialAttributeFactory(org.projectnessie.cel.interpreter.AttributePattern.newPartialAttributeFactory) AttributeFactory.newAttributeFactory(org.projectnessie.cel.interpreter.AttributeFactory.newAttributeFactory) Activation.emptyActivation(org.projectnessie.cel.interpreter.Activation.emptyActivation) Activation.newPartialActivation(org.projectnessie.cel.interpreter.Activation.newPartialActivation) Activation.newActivation(org.projectnessie.cel.interpreter.Activation.newActivation) TypeRegistry(org.projectnessie.cel.common.types.ref.TypeRegistry) Source(org.projectnessie.cel.common.Source) MethodSource(org.junit.jupiter.params.provider.MethodSource) Container.newContainer(org.projectnessie.cel.common.containers.Container.newContainer) Container(org.projectnessie.cel.common.containers.Container) CheckResult(org.projectnessie.cel.checker.Checker.CheckResult) CheckerEnv.newStandardCheckerEnv(org.projectnessie.cel.checker.CheckerEnv.newStandardCheckerEnv) CheckerEnv(org.projectnessie.cel.checker.CheckerEnv) EvalState.newEvalState(org.projectnessie.cel.interpreter.EvalState.newEvalState) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 5 with CheckerEnv

use of org.projectnessie.cel.checker.CheckerEnv in project cel-java by projectnessie.

the class InterpreterTest method typeConversionOpt.

@ParameterizedTest
@MethodSource("typeConversionOptTests")
void typeConversionOpt(ConvTestCase tc) {
    Source src = newTextSource(tc.in);
    ParseResult parsed = Parser.parseAllMacros(src);
    assertThat(parsed.hasErrors()).withFailMessage(parsed.getErrors()::toDisplayString).isFalse();
    Container cont = Container.defaultContainer;
    TypeRegistry reg = newRegistry();
    CheckerEnv env = newStandardCheckerEnv(cont, reg);
    CheckResult checkResult = Checker.Check(parsed, src, env);
    if (parsed.hasErrors()) {
        throw new IllegalArgumentException(parsed.getErrors().toDisplayString());
    }
    AttributeFactory attrs = newAttributeFactory(cont, reg, reg);
    Interpreter interp = newStandardInterpreter(cont, reg, reg, attrs);
    if (!tc.fail) {
        typeConversionOptCheck(tc, checkResult, interp);
    } else {
        Throwable err = catchThrowable(() -> typeConversionOptCheck(tc, checkResult, interp));
        assertThat(err).withFailMessage(() -> format("Expected '%s' to fail with '%s'", tc.in, tc.err)).isNotNull();
        // TODO 'err' below comes from "try-catch" of the preceding 'newInterpretable'
        // Show how the error returned during program planning is the same as the runtime
        // error which would be produced normally.
        Interpretable i2 = interp.newInterpretable(checkResult.getCheckedExpr());
        Val errVal = i2.eval(emptyActivation());
        String errValStr = errVal.toString();
        assertThat(errValStr).isEqualTo(err.getMessage());
        if (tc.err != null) {
            assertThat(errValStr).contains(tc.err);
        }
    }
}
Also used : Val(org.projectnessie.cel.common.types.ref.Val) Interpreter.newStandardInterpreter(org.projectnessie.cel.interpreter.Interpreter.newStandardInterpreter) Interpreter.newInterpreter(org.projectnessie.cel.interpreter.Interpreter.newInterpreter) ParseResult(org.projectnessie.cel.parser.Parser.ParseResult) AttributePattern.newPartialAttributeFactory(org.projectnessie.cel.interpreter.AttributePattern.newPartialAttributeFactory) AttributeFactory.newAttributeFactory(org.projectnessie.cel.interpreter.AttributeFactory.newAttributeFactory) ByteString(com.google.protobuf.ByteString) TypeRegistry(org.projectnessie.cel.common.types.ref.TypeRegistry) Source(org.projectnessie.cel.common.Source) MethodSource(org.junit.jupiter.params.provider.MethodSource) Source.newTextSource(org.projectnessie.cel.common.Source.newTextSource) Container.newContainer(org.projectnessie.cel.common.containers.Container.newContainer) Container(org.projectnessie.cel.common.containers.Container) CheckResult(org.projectnessie.cel.checker.Checker.CheckResult) CheckerEnv(org.projectnessie.cel.checker.CheckerEnv) CheckerEnv.newStandardCheckerEnv(org.projectnessie.cel.checker.CheckerEnv.newStandardCheckerEnv) Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

CheckResult (org.projectnessie.cel.checker.Checker.CheckResult)6 CheckerEnv (org.projectnessie.cel.checker.CheckerEnv)6 ParseResult (org.projectnessie.cel.parser.Parser.ParseResult)6 MethodSource (org.junit.jupiter.params.provider.MethodSource)5 CheckerEnv.newStandardCheckerEnv (org.projectnessie.cel.checker.CheckerEnv.newStandardCheckerEnv)5 Source (org.projectnessie.cel.common.Source)5 Container (org.projectnessie.cel.common.containers.Container)5 Container.newContainer (org.projectnessie.cel.common.containers.Container.newContainer)5 TypeRegistry (org.projectnessie.cel.common.types.ref.TypeRegistry)5 AttributeFactory.newAttributeFactory (org.projectnessie.cel.interpreter.AttributeFactory.newAttributeFactory)5 AttributePattern.newPartialAttributeFactory (org.projectnessie.cel.interpreter.AttributePattern.newPartialAttributeFactory)5 Interpreter.newStandardInterpreter (org.projectnessie.cel.interpreter.Interpreter.newStandardInterpreter)5 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 Source.newTextSource (org.projectnessie.cel.common.Source.newTextSource)4 Val (org.projectnessie.cel.common.types.ref.Val)4 Activation.emptyActivation (org.projectnessie.cel.interpreter.Activation.emptyActivation)4 Activation.newActivation (org.projectnessie.cel.interpreter.Activation.newActivation)4 Activation.newPartialActivation (org.projectnessie.cel.interpreter.Activation.newPartialActivation)4 Interpreter.newInterpreter (org.projectnessie.cel.interpreter.Interpreter.newInterpreter)4 Test (org.junit.jupiter.api.Test)3