Search in sources :

Example 1 with Entry

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

the class Unparser method visitStructMap.

void visitStructMap(CreateStruct expr) {
    List<Entry> entries = expr.getEntriesList();
    str.append("{");
    for (int i = 0; i < entries.size(); i++) {
        if (i > 0) {
            str.append(", ");
        }
        Entry entry = entries.get(i);
        Expr k = entry.getMapKey();
        visit(k);
        str.append(": ");
        Expr v = entry.getValue();
        visit(v);
    }
    str.append("}");
}
Also used : Entry(com.google.api.expr.v1alpha1.Expr.CreateStruct.Entry) Expr(com.google.api.expr.v1alpha1.Expr)

Example 2 with Entry

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

the class AstPruner method prune.

Expr prune(Expr node) {
    if (node == null) {
        return null;
    }
    Val val = value(node.getId());
    if (val != null && !isUnknownOrError(val)) {
        Expr newNode = maybeCreateLiteral(node.getId(), val);
        if (newNode != null) {
            return newNode;
        }
    }
    switch(node.getExprKindCase()) {
        case SELECT_EXPR:
            Select select = node.getSelectExpr();
            Expr operand = prune(select.getOperand());
            if (operand != null && operand != select.getOperand()) {
                return Expr.newBuilder().setId(node.getId()).setSelectExpr(Select.newBuilder().setOperand(operand).setField(select.getField()).setTestOnly(select.getTestOnly())).build();
            }
            break;
        case CALL_EXPR:
            Call call = node.getCallExpr();
            Expr newExpr = maybePruneFunction(node);
            if (newExpr != null) {
                newExpr = prune(newExpr);
                return newExpr;
            }
            boolean prunedCall = false;
            List<Expr> args = call.getArgsList();
            List<Expr> newArgs = new ArrayList<>(args.size());
            for (int i = 0; i < args.size(); i++) {
                Expr arg = args.get(i);
                newArgs.add(arg);
                Expr newArg = prune(arg);
                if (newArg != null && newArg != arg) {
                    prunedCall = true;
                    newArgs.set(i, newArg);
                }
            }
            Call newCall = Call.newBuilder().setFunction(call.getFunction()).setTarget(call.getTarget()).addAllArgs(newArgs).build();
            Expr newTarget = prune(call.getTarget());
            if (newTarget != null && newTarget != call.getTarget()) {
                prunedCall = true;
                newCall = Call.newBuilder().setFunction(call.getFunction()).setTarget(newTarget).addAllArgs(newArgs).build();
            }
            if (prunedCall) {
                return Expr.newBuilder().setId(node.getId()).setCallExpr(newCall).build();
            }
            break;
        case LIST_EXPR:
            CreateList list = node.getListExpr();
            List<Expr> elems = list.getElementsList();
            List<Expr> newElems = new ArrayList<>(elems.size());
            boolean prunedList = false;
            for (int i = 0; i < elems.size(); i++) {
                Expr elem = elems.get(i);
                newElems.add(elem);
                Expr newElem = prune(elem);
                if (newElem != null && newElem != elem) {
                    newElems.set(i, newElem);
                    prunedList = true;
                }
            }
            if (prunedList) {
                return Expr.newBuilder().setId(node.getId()).setListExpr(CreateList.newBuilder().addAllElements(newElems)).build();
            }
            break;
        case STRUCT_EXPR:
            boolean prunedStruct = false;
            CreateStruct struct = node.getStructExpr();
            List<Entry> entries = struct.getEntriesList();
            String messageType = struct.getMessageName();
            List<Entry> newEntries = new ArrayList<>(entries.size());
            for (int i = 0; i < entries.size(); i++) {
                Entry entry = entries.get(i);
                newEntries.add(entry);
                Expr mapKey = entry.getMapKey();
                Expr newKey = mapKey != Entry.getDefaultInstance().getMapKey() ? prune(mapKey) : null;
                Expr newValue = prune(entry.getValue());
                if ((newKey == null || newKey == mapKey) && (newValue == null || newValue == entry.getValue())) {
                    continue;
                }
                prunedStruct = true;
                Entry newEntry;
                if (!messageType.isEmpty()) {
                    newEntry = Entry.newBuilder().setFieldKey(entry.getFieldKey()).setValue(newValue).build();
                } else {
                    newEntry = Entry.newBuilder().setMapKey(newKey).setValue(newValue).build();
                }
                newEntries.set(i, newEntry);
            }
            if (prunedStruct) {
                return Expr.newBuilder().setId(node.getId()).setStructExpr(CreateStruct.newBuilder().setMessageName(messageType).addAllEntries(entries)).build();
            }
            break;
        case COMPREHENSION_EXPR:
            Comprehension compre = node.getComprehensionExpr();
            // Only the range of the comprehension is pruned since the state tracking only records
            // the last iteration of the comprehension and not each step in the evaluation which
            // means that the any residuals computed in between might be inaccurate.
            Expr newRange = prune(compre.getIterRange());
            if (newRange != null && newRange != compre.getIterRange()) {
                return Expr.newBuilder().setId(node.getId()).setComprehensionExpr(Comprehension.newBuilder().setIterVar(compre.getIterVar()).setIterRange(newRange).setAccuVar(compre.getAccuVar()).setAccuInit(compre.getAccuInit()).setLoopCondition(compre.getLoopCondition()).setLoopStep(compre.getLoopStep()).setResult(compre.getResult())).build();
            }
    }
    // allocation cost at another point. So go with the simple approach - at least for now.
    return node;
}
Also used : Val(org.projectnessie.cel.common.types.ref.Val) Call(com.google.api.expr.v1alpha1.Expr.Call) CreateStruct(com.google.api.expr.v1alpha1.Expr.CreateStruct) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) Comprehension(com.google.api.expr.v1alpha1.Expr.Comprehension) CreateList(com.google.api.expr.v1alpha1.Expr.CreateList) Entry(com.google.api.expr.v1alpha1.Expr.CreateStruct.Entry) Expr(com.google.api.expr.v1alpha1.Expr) Select(com.google.api.expr.v1alpha1.Expr.Select)

Example 3 with Entry

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

the class ConformanceServiceImpl method valueToRefValue.

/**
 * ValueToRefValue converts between exprpb.Value and ref.Val.
 */
static Val valueToRefValue(TypeAdapter adapter, Value v) {
    switch(v.getKindCase()) {
        case NULL_VALUE:
            return NullT.NullValue;
        case BOOL_VALUE:
            return boolOf(v.getBoolValue());
        case INT64_VALUE:
            return intOf(v.getInt64Value());
        case UINT64_VALUE:
            return uintOf(v.getUint64Value());
        case DOUBLE_VALUE:
            return doubleOf(v.getDoubleValue());
        case STRING_VALUE:
            return stringOf(v.getStringValue());
        case BYTES_VALUE:
            return bytesOf(v.getBytesValue().toByteArray());
        case OBJECT_VALUE:
            Any any = v.getObjectValue();
            return adapter.nativeToValue(any);
        case MAP_VALUE:
            MapValue m = v.getMapValue();
            Map<Val, Val> entries = new HashMap<>();
            for (Entry entry : m.getEntriesList()) {
                Val key = valueToRefValue(adapter, entry.getKey());
                Val pb = valueToRefValue(adapter, entry.getValue());
                entries.put(key, pb);
            }
            return adapter.nativeToValue(entries);
        case LIST_VALUE:
            ListValue l = v.getListValue();
            List<Val> elts = l.getValuesList().stream().map(el -> valueToRefValue(adapter, el)).collect(Collectors.toList());
            return adapter.nativeToValue(elts);
        case TYPE_VALUE:
            String typeName = v.getTypeValue();
            Type tv = Types.getTypeByName(typeName);
            if (tv != null) {
                return tv;
            }
            return newObjectTypeValue(typeName);
        default:
            throw new IllegalArgumentException("unknown value " + v.getKindCase());
    }
}
Also used : Val(org.projectnessie.cel.common.types.ref.Val) MapValue(com.google.api.expr.v1alpha1.MapValue) Lister(org.projectnessie.cel.common.types.traits.Lister) EnvOption.container(org.projectnessie.cel.EnvOption.container) CheckRequest(com.google.api.expr.v1alpha1.CheckRequest) Err(org.projectnessie.cel.common.types.Err) True(org.projectnessie.cel.common.types.BoolT.True) Err.isError(org.projectnessie.cel.common.types.Err.isError) Ast(org.projectnessie.cel.Ast) CheckResponse(com.google.api.expr.v1alpha1.CheckResponse) Map(java.util.Map) Value(com.google.api.expr.v1alpha1.Value) UnknownSet(com.google.api.expr.v1alpha1.UnknownSet) Val(org.projectnessie.cel.common.types.ref.Val) PrintWriter(java.io.PrintWriter) IteratorT(org.projectnessie.cel.common.types.IteratorT) DoubleT.doubleOf(org.projectnessie.cel.common.types.DoubleT.doubleOf) Status(com.google.rpc.Status) EnvOption.clearMacros(org.projectnessie.cel.EnvOption.clearMacros) StringT.stringOf(org.projectnessie.cel.common.types.StringT.stringOf) EnvOption(org.projectnessie.cel.EnvOption) ExprValue(com.google.api.expr.v1alpha1.ExprValue) Collectors(java.util.stream.Collectors) IntT.intOf(org.projectnessie.cel.common.types.IntT.intOf) UintT.uintOf(org.projectnessie.cel.common.types.UintT.uintOf) ByteString(com.google.protobuf.ByteString) List(java.util.List) ErrorSet(com.google.api.expr.v1alpha1.ErrorSet) UnknownT.unknownOf(org.projectnessie.cel.common.types.UnknownT.unknownOf) Type(org.projectnessie.cel.common.types.ref.Type) Any(com.google.protobuf.Any) EvalResult(org.projectnessie.cel.Program.EvalResult) ParseRequest(com.google.api.expr.v1alpha1.ParseRequest) Err.newErr(org.projectnessie.cel.common.types.Err.newErr) Env.newEnv(org.projectnessie.cel.Env.newEnv) Entry(com.google.api.expr.v1alpha1.MapValue.Entry) ParseResponse(com.google.api.expr.v1alpha1.ParseResponse) StdLib(org.projectnessie.cel.Library.StdLib) EnvOption.declarations(org.projectnessie.cel.EnvOption.declarations) ConformanceServiceImplBase(com.google.api.expr.v1alpha1.ConformanceServiceGrpc.ConformanceServiceImplBase) HashMap(java.util.HashMap) Timestamp(com.google.protobuf.Timestamp) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) AstIssuesTuple(org.projectnessie.cel.Env.AstIssuesTuple) Mapper(org.projectnessie.cel.common.types.traits.Mapper) Types(org.projectnessie.cel.common.types.Types) IssueDetails(com.google.api.expr.v1alpha1.IssueDetails) CEL.astToCheckedExpr(org.projectnessie.cel.CEL.astToCheckedExpr) EvalRequest(com.google.api.expr.v1alpha1.EvalRequest) Code(com.google.rpc.Code) StringWriter(java.io.StringWriter) EnvOption.types(org.projectnessie.cel.EnvOption.types) NullT(org.projectnessie.cel.common.types.NullT) Types.boolOf(org.projectnessie.cel.common.types.Types.boolOf) TypeAdapter(org.projectnessie.cel.common.types.ref.TypeAdapter) UnknownT.isUnknown(org.projectnessie.cel.common.types.UnknownT.isUnknown) ListValue(com.google.api.expr.v1alpha1.ListValue) CEL.parsedExprToAst(org.projectnessie.cel.CEL.parsedExprToAst) BytesT.bytesOf(org.projectnessie.cel.common.types.BytesT.bytesOf) CELError(org.projectnessie.cel.common.CELError) SourcePosition(com.google.api.expr.v1alpha1.SourcePosition) TypeT(org.projectnessie.cel.common.types.TypeT) Duration(com.google.protobuf.Duration) CEL.astToParsedExpr(org.projectnessie.cel.CEL.astToParsedExpr) CEL.checkedExprToAst(org.projectnessie.cel.CEL.checkedExprToAst) Program(org.projectnessie.cel.Program) EvalResponse(com.google.api.expr.v1alpha1.EvalResponse) TypeT.newObjectTypeValue(org.projectnessie.cel.common.types.TypeT.newObjectTypeValue) Message(com.google.protobuf.Message) Env(org.projectnessie.cel.Env) Env.newCustomEnv(org.projectnessie.cel.Env.newCustomEnv) Entry(com.google.api.expr.v1alpha1.MapValue.Entry) Type(org.projectnessie.cel.common.types.ref.Type) HashMap(java.util.HashMap) ListValue(com.google.api.expr.v1alpha1.ListValue) MapValue(com.google.api.expr.v1alpha1.MapValue) ByteString(com.google.protobuf.ByteString) Any(com.google.protobuf.Any)

Example 4 with Entry

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

the class Unparser method visitStructMsg.

void visitStructMsg(CreateStruct expr) {
    List<Entry> entries = expr.getEntriesList();
    str.append(expr.getMessageName());
    str.append("{");
    for (int i = 0; i < entries.size(); i++) {
        if (i > 0) {
            str.append(", ");
        }
        Entry entry = entries.get(i);
        String f = entry.getFieldKey();
        str.append(f);
        str.append(": ");
        Expr v = entry.getValue();
        visit(v);
    }
    str.append("}");
}
Also used : Entry(com.google.api.expr.v1alpha1.Expr.CreateStruct.Entry) Expr(com.google.api.expr.v1alpha1.Expr)

Example 5 with Entry

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

the class AstPruner method maybeCreateLiteral.

Expr maybeCreateLiteral(long id, Val v) {
    Type t = v.type();
    switch(t.typeEnum()) {
        case Bool:
            return createLiteral(id, Constant.newBuilder().setBoolValue((Boolean) v.value()).build());
        case Int:
            return createLiteral(id, Constant.newBuilder().setInt64Value(((Number) v.value()).longValue()).build());
        case Uint:
            return createLiteral(id, Constant.newBuilder().setUint64Value(((Number) v.value()).longValue()).build());
        case String:
            return createLiteral(id, Constant.newBuilder().setStringValue(v.value().toString()).build());
        case Double:
            return createLiteral(id, Constant.newBuilder().setDoubleValue(((Number) v.value()).doubleValue()).build());
        case Bytes:
            return createLiteral(id, Constant.newBuilder().setBytesValue(ByteString.copyFrom((byte[]) v.value())).build());
        case Null:
            return createLiteral(id, Constant.newBuilder().setNullValue(NullValue.NULL_VALUE).build());
    }
    // Attempt to build a list literal.
    if (v instanceof Lister) {
        Lister list = (Lister) v;
        int sz = (int) list.size().intValue();
        List<Expr> elemExprs = new ArrayList<>(sz);
        for (int i = 0; i < sz; i++) {
            Val elem = list.get(intOf(i));
            if (isUnknownOrError(elem)) {
                return null;
            }
            Expr elemExpr = maybeCreateLiteral(nextID(), elem);
            if (elemExpr == null) {
                return null;
            }
            elemExprs.add(elemExpr);
        }
        return Expr.newBuilder().setId(id).setListExpr(CreateList.newBuilder().addAllElements(elemExprs).build()).build();
    }
    // Create a map literal if possible.
    if (v instanceof Mapper) {
        Mapper mp = (Mapper) v;
        IteratorT it = mp.iterator();
        List<Entry> entries = new ArrayList<>((int) mp.size().intValue());
        while (it.hasNext() == True) {
            Val key = it.next();
            Val val = mp.get(key);
            if (isUnknownOrError(key) || isUnknownOrError(val)) {
                return null;
            }
            Expr keyExpr = maybeCreateLiteral(nextID(), key);
            if (keyExpr == null) {
                return null;
            }
            Expr valExpr = maybeCreateLiteral(nextID(), val);
            if (valExpr == null) {
                return null;
            }
            Entry entry = Entry.newBuilder().setId(nextID()).setMapKey(keyExpr).setValue(valExpr).build();
            entries.add(entry);
        }
        return Expr.newBuilder().setId(id).setStructExpr(CreateStruct.newBuilder().addAllEntries(entries)).build();
    }
    // the enumeration the fields for a given message.
    return null;
}
Also used : Val(org.projectnessie.cel.common.types.ref.Val) Mapper(org.projectnessie.cel.common.types.traits.Mapper) IteratorT(org.projectnessie.cel.common.types.IteratorT) Type(org.projectnessie.cel.common.types.ref.Type) Entry(com.google.api.expr.v1alpha1.Expr.CreateStruct.Entry) Expr(com.google.api.expr.v1alpha1.Expr) Lister(org.projectnessie.cel.common.types.traits.Lister) ArrayList(java.util.ArrayList)

Aggregations

Expr (com.google.api.expr.v1alpha1.Expr)4 Entry (com.google.api.expr.v1alpha1.Expr.CreateStruct.Entry)4 Val (org.projectnessie.cel.common.types.ref.Val)4 ArrayList (java.util.ArrayList)3 Any (com.google.protobuf.Any)2 ByteString (com.google.protobuf.ByteString)2 List (java.util.List)2 Map (java.util.Map)2 TestAllTypes (com.google.api.expr.test.v1.proto3.TestAllTypesProto.TestAllTypes)1 NestedMessage (com.google.api.expr.test.v1.proto3.TestAllTypesProto.TestAllTypes.NestedMessage)1 CheckRequest (com.google.api.expr.v1alpha1.CheckRequest)1 CheckResponse (com.google.api.expr.v1alpha1.CheckResponse)1 ConformanceServiceImplBase (com.google.api.expr.v1alpha1.ConformanceServiceGrpc.ConformanceServiceImplBase)1 Decl (com.google.api.expr.v1alpha1.Decl)1 ErrorSet (com.google.api.expr.v1alpha1.ErrorSet)1 EvalRequest (com.google.api.expr.v1alpha1.EvalRequest)1 EvalResponse (com.google.api.expr.v1alpha1.EvalResponse)1 Call (com.google.api.expr.v1alpha1.Expr.Call)1 Comprehension (com.google.api.expr.v1alpha1.Expr.Comprehension)1 CreateList (com.google.api.expr.v1alpha1.Expr.CreateList)1