Search in sources :

Example 6 with CheckedExpr

use of com.google.api.expr.v1alpha1.CheckedExpr in project cel-java by projectnessie.

the class ConformanceServerTest method FullUp.

/**
 * TestFullUp tests Parse, Check, and Eval back-to-back.
 */
@Test
void FullUp() {
    ParseRequest preq = ParseRequest.newBuilder().setCelSource("x + y").build();
    ParseResponse pres = stub.parse(preq);
    assertThat(pres.isInitialized()).isTrue();
    ParsedExpr parsedExpr = pres.getParsedExpr();
    assertThat(parsedExpr.isInitialized()).isTrue();
    CheckRequest creq = CheckRequest.newBuilder().setParsedExpr(parsedExpr).addTypeEnv(Decls.newVar("x", Decls.Int)).addTypeEnv(Decls.newVar("y", Decls.Int)).build();
    CheckResponse cres = stub.check(creq);
    assertThat(cres.isInitialized()).isTrue();
    CheckedExpr checkedExpr = cres.getCheckedExpr();
    assertThat(checkedExpr.isInitialized()).isTrue();
    Type tp = checkedExpr.getTypeMapMap().get(1L);
    assertThat(tp).isNotNull();
    assertThat(tp.getTypeKindCase()).isSameAs(TypeKindCase.PRIMITIVE);
    assertThat(tp.getPrimitive()).isSameAs(PrimitiveType.INT64);
    EvalRequest ereq = EvalRequest.newBuilder().setCheckedExpr(checkedExpr).putBindings("x", exprValueInt64(1)).putBindings("y", exprValueInt64(2)).build();
    EvalResponse eres = stub.eval(ereq);
    assertThat(eres.isInitialized()).isTrue();
    assertThat(eres.getResult().isInitialized()).isTrue();
    assertThat(eres.getResult().getKindCase()).isSameAs(KindCase.VALUE);
    assertThat(eres.getResult().getValue().getKindCase()).isSameAs(Value.KindCase.INT64_VALUE);
    assertThat(eres.getResult().getValue().getInt64Value()).isEqualTo(3L);
}
Also used : EvalRequest(com.google.api.expr.v1alpha1.EvalRequest) PrimitiveType(com.google.api.expr.v1alpha1.Type.PrimitiveType) Type(com.google.api.expr.v1alpha1.Type) ParseRequest(com.google.api.expr.v1alpha1.ParseRequest) CheckedExpr(com.google.api.expr.v1alpha1.CheckedExpr) ParsedExpr(com.google.api.expr.v1alpha1.ParsedExpr) CheckResponse(com.google.api.expr.v1alpha1.CheckResponse) CheckRequest(com.google.api.expr.v1alpha1.CheckRequest) ParseResponse(com.google.api.expr.v1alpha1.ParseResponse) EvalResponse(com.google.api.expr.v1alpha1.EvalResponse) Test(org.junit.jupiter.api.Test)

Example 7 with CheckedExpr

use of com.google.api.expr.v1alpha1.CheckedExpr in project cel-java by projectnessie.

the class CEL method initInterpretable.

/**
 * initIterpretable creates a checked or unchecked interpretable depending on whether the Ast has
 * been run through the type-checker.
 */
private static Program initInterpretable(Prog p, Ast ast, List<InterpretableDecorator> decorators) {
    InterpretableDecorator[] decs = decorators.toArray(new InterpretableDecorator[0]);
    // slower to execute than their checked counterparts.
    if (!ast.isChecked()) {
        p.interpretable = p.interpreter.newUncheckedInterpretable(ast.getExpr(), decs);
        return p;
    }
    // When the AST has been checked it contains metadata that can be used to speed up program
    // execution.
    CheckedExpr checked = astToCheckedExpr(ast);
    p.interpretable = p.interpreter.newInterpretable(checked, decs);
    return p;
}
Also used : CheckedExpr(com.google.api.expr.v1alpha1.CheckedExpr) InterpretableDecorator(org.projectnessie.cel.interpreter.InterpretableDecorator)

Example 8 with CheckedExpr

use of com.google.api.expr.v1alpha1.CheckedExpr 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 9 with CheckedExpr

use of com.google.api.expr.v1alpha1.CheckedExpr in project cel-java by projectnessie.

the class ProviderTest method typeRegistryNewValue_OneofFields.

@Test
void typeRegistryNewValue_OneofFields() {
    TypeRegistry reg = newRegistry(CheckedExpr.getDefaultInstance(), ParsedExpr.getDefaultInstance());
    Val exp = reg.newValue("google.api.expr.v1alpha1.CheckedExpr", mapOf("expr", reg.newValue("google.api.expr.v1alpha1.Expr", mapOf("const_expr", reg.newValue("google.api.expr.v1alpha1.Constant", mapOf("string_value", stringOf("oneof")))))));
    assertThat(exp).matches(v -> !Err.isError(v));
    CheckedExpr ce = exp.convertToNative(CheckedExpr.class);
    assertThat(ce).extracting(CheckedExpr::getExpr).extracting(Expr::getConstExpr).extracting(Constant::getStringValue).isEqualTo("oneof");
}
Also used : Val(org.projectnessie.cel.common.types.ref.Val) ParsedExpr(com.google.api.expr.v1alpha1.ParsedExpr) Expr(com.google.api.expr.v1alpha1.Expr) CheckedExpr(com.google.api.expr.v1alpha1.CheckedExpr) CheckedExpr(com.google.api.expr.v1alpha1.CheckedExpr) TypeRegistry(org.projectnessie.cel.common.types.ref.TypeRegistry) ProtoTypeRegistry(org.projectnessie.cel.common.types.pb.ProtoTypeRegistry) Test(org.junit.jupiter.api.Test)

Aggregations

CheckedExpr (com.google.api.expr.v1alpha1.CheckedExpr)9 ParsedExpr (com.google.api.expr.v1alpha1.ParsedExpr)5 Test (org.junit.jupiter.api.Test)5 CEL.parsedExprToAst (org.projectnessie.cel.CEL.parsedExprToAst)4 CEL.astToCheckedExpr (org.projectnessie.cel.CEL.astToCheckedExpr)3 CEL.checkedExprToAst (org.projectnessie.cel.CEL.checkedExprToAst)3 AstIssuesTuple (org.projectnessie.cel.Env.AstIssuesTuple)3 Env.newCustomEnv (org.projectnessie.cel.Env.newCustomEnv)3 Env.newEnv (org.projectnessie.cel.Env.newEnv)3 CheckRequest (com.google.api.expr.v1alpha1.CheckRequest)2 CheckResponse (com.google.api.expr.v1alpha1.CheckResponse)2 EvalRequest (com.google.api.expr.v1alpha1.EvalRequest)2 EvalResponse (com.google.api.expr.v1alpha1.EvalResponse)2 Expr (com.google.api.expr.v1alpha1.Expr)2 ParseRequest (com.google.api.expr.v1alpha1.ParseRequest)2 ParseResponse (com.google.api.expr.v1alpha1.ParseResponse)2 Type (com.google.api.expr.v1alpha1.Type)2 CEL.astToParsedExpr (org.projectnessie.cel.CEL.astToParsedExpr)2 MapType (com.google.api.expr.v1alpha1.Type.MapType)1 PrimitiveType (com.google.api.expr.v1alpha1.Type.PrimitiveType)1