Search in sources :

Example 6 with Constant

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

the class Env method residualAst.

/**
 * ResidualAst takes an Ast and its EvalDetails to produce a new Ast which only contains the
 * attribute references which are unknown.
 *
 * <p>Residual expressions are beneficial in a few scenarios:
 *
 * <ul>
 *   <li>Optimizing constant expression evaluations away.
 *   <li>Indexing and pruning expressions based on known input arguments.
 *   <li>Surfacing additional requirements that are needed in order to complete an evaluation.
 *   <li>Sharing the evaluation of an expression across multiple machines/nodes.
 * </ul>
 *
 * <p>For example, if an expression targets a 'resource' and 'request' attribute and the possible
 * values for the resource are known, a PartialActivation could mark the 'request' as an unknown
 * interpreter.AttributePattern and the resulting ResidualAst would be reduced to only the parts
 * of the expression that reference the 'request'.
 *
 * <p>Note, the expression ids within the residual AST generated through this method have no
 * correlation to the expression ids of the original AST.
 *
 * <p>See the PartialVars helper for how to construct a PartialActivation.
 *
 * <p>TODO: Consider adding an option to generate a Program.Residual to avoid round-tripping to an
 * Ast format and then Program again.
 */
public Ast residualAst(Ast a, EvalDetails details) {
    Expr pruned = pruneAst(a.getExpr(), details.getState());
    String expr = astToString(parsedExprToAst(ParsedExpr.newBuilder().setExpr(pruned).build()));
    AstIssuesTuple parsedIss = parse(expr);
    if (parsedIss.hasIssues()) {
        throw parsedIss.getIssues().err();
    }
    if (!a.isChecked()) {
        return parsedIss.ast;
    }
    AstIssuesTuple checkedIss = check(parsedIss.ast);
    if (checkedIss.hasIssues()) {
        throw checkedIss.getIssues().err();
    }
    return checkedIss.ast;
}
Also used : ParsedExpr(com.google.api.expr.v1alpha1.ParsedExpr) Expr(com.google.api.expr.v1alpha1.Expr) CheckedExpr(com.google.api.expr.v1alpha1.CheckedExpr) CEL.astToParsedExpr(org.projectnessie.cel.CEL.astToParsedExpr) CEL.astToString(org.projectnessie.cel.CEL.astToString)

Example 7 with Constant

use of com.google.api.expr.v1alpha1.Constant 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)5 Expr (com.google.api.expr.v1alpha1.Expr)5 ParsedExpr (com.google.api.expr.v1alpha1.ParsedExpr)5 Test (org.junit.jupiter.api.Test)3 Call (com.google.api.expr.v1alpha1.Expr.Call)2 Type (com.google.api.expr.v1alpha1.Type)2 Constant (io.atlasmap.v2.Constant)2 CEL.astToParsedExpr (org.projectnessie.cel.CEL.astToParsedExpr)2 CEL.astToString (org.projectnessie.cel.CEL.astToString)2 Constant (com.google.api.expr.v1alpha1.Constant)1 Ident (com.google.api.expr.v1alpha1.Expr.Ident)1 ParseRequest (com.google.api.expr.v1alpha1.ParseRequest)1 ParseResponse (com.google.api.expr.v1alpha1.ParseResponse)1 Reference (com.google.api.expr.v1alpha1.Reference)1 SourceInfo (com.google.api.expr.v1alpha1.SourceInfo)1 AtlasException (io.atlasmap.api.AtlasException)1 ConstantModule (io.atlasmap.core.ConstantModule)1 Field (io.atlasmap.v2.Field)1 PropertyField (io.atlasmap.v2.PropertyField)1 ArrayList (java.util.ArrayList)1