Search in sources :

Example 6 with Type

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

the class ProtoTypeRegistry method register.

@Override
public void register(Object t) {
    if (t instanceof Message) {
        Set<FileDescriptor> fds = collectFileDescriptorSet((Message) t);
        for (FileDescriptor fd : fds) {
            registerDescriptor(fd);
        }
        registerMessage((Message) t);
    } else if (t instanceof org.projectnessie.cel.common.types.ref.Type) {
        registerType((org.projectnessie.cel.common.types.ref.Type) t);
    } else {
        throw new RuntimeException(String.format("unsupported type: %s", t.getClass().getName()));
    }
}
Also used : DoubleType(org.projectnessie.cel.common.types.DoubleT.DoubleType) IntType(org.projectnessie.cel.common.types.IntT.IntType) MapType(org.projectnessie.cel.common.types.MapT.MapType) FieldType(org.projectnessie.cel.common.types.ref.FieldType) Err.anyWithEmptyType(org.projectnessie.cel.common.types.Err.anyWithEmptyType) DurationType(org.projectnessie.cel.common.types.DurationT.DurationType) UintType(org.projectnessie.cel.common.types.UintT.UintType) TypeType(org.projectnessie.cel.common.types.TypeT.TypeType) JavaType(com.google.protobuf.Descriptors.FieldDescriptor.JavaType) StringType(org.projectnessie.cel.common.types.StringT.StringType) NullType(org.projectnessie.cel.common.types.NullT.NullType) Type(com.google.api.expr.v1alpha1.Type) BytesType(org.projectnessie.cel.common.types.BytesT.BytesType) ListType(org.projectnessie.cel.common.types.ListT.ListType) BoolType(org.projectnessie.cel.common.types.BoolT.BoolType) Err.unknownType(org.projectnessie.cel.common.types.Err.unknownType) TimestampType(org.projectnessie.cel.common.types.TimestampT.TimestampType) PbTypeDescription.typeNameFromMessage(org.projectnessie.cel.common.types.pb.PbTypeDescription.typeNameFromMessage) Message(com.google.protobuf.Message) FileDescriptor(com.google.protobuf.Descriptors.FileDescriptor)

Example 7 with Type

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

the class CELTest method Customtypes.

@Test
void Customtypes() {
    Type exprType = Decls.newObjectType("google.api.expr.v1alpha1.Expr");
    TypeRegistry reg = newEmptyRegistry();
    Env e = newEnv(customTypeAdapter(reg), customTypeProvider(reg), container("google.api.expr.v1alpha1"), types(Expr.getDefaultInstance(), BoolT.BoolType, IntT.IntType, StringT.StringType), declarations(Decls.newVar("expr", exprType)));
    AstIssuesTuple astIss = e.compile("expr == Expr{id: 2,\n" + "\t\t\tcall_expr: Expr.Call{\n" + "\t\t\t\tfunction: \"_==_\",\n" + "\t\t\t\targs: [\n" + "\t\t\t\t\tExpr{id: 1, ident_expr: Expr.Ident{ name: \"a\" }},\n" + "\t\t\t\t\tExpr{id: 3, ident_expr: Expr.Ident{ name: \"b\" }}]\n" + "\t\t\t}}");
    assertThat(astIss.getAst().getResultType()).isEqualTo(Decls.Bool);
    Program prg = e.program(astIss.getAst());
    Object vars = mapOf("expr", Expr.newBuilder().setId(2).setCallExpr(Call.newBuilder().setFunction("_==_").addAllArgs(asList(Expr.newBuilder().setId(1).setIdentExpr(Ident.newBuilder().setName("a")).build(), Expr.newBuilder().setId(3).setIdentExpr(Ident.newBuilder().setName("b")).build()))).build());
    EvalResult out = prg.eval(vars);
    assertThat(out.getVal()).isSameAs(True);
}
Also used : Type(com.google.api.expr.v1alpha1.Type) EvalResult(org.projectnessie.cel.Program.EvalResult) Env.newEnv(org.projectnessie.cel.Env.newEnv) Env.newCustomEnv(org.projectnessie.cel.Env.newCustomEnv) AstIssuesTuple(org.projectnessie.cel.Env.AstIssuesTuple) TypeRegistry(org.projectnessie.cel.common.types.ref.TypeRegistry) Test(org.junit.jupiter.api.Test)

Example 8 with Type

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

the class CheckerEnvTest method overlappingOverload.

@Test
void overlappingOverload() {
    CheckerEnv env = newStandardCheckerEnv(Container.defaultContainer, newRegistry());
    Type paramA = Decls.newTypeParamType("A");
    List<String> typeParamAList = singletonList("A");
    Assertions.assertThatThrownBy(() -> env.add(Decls.newFunction(Overloads.TypeConvertDyn, Decls.newParameterizedOverload(Overloads.ToDyn, singletonList(paramA), Decls.Dyn, typeParamAList)))).hasMessage("overlapping overload for name 'dyn' (type '(type_param: \"A\") -> dyn' with overloadId: 'to_dyn' cannot be distinguished from '(type_param: \"A\") -> dyn' with overloadId: 'to_dyn')");
}
Also used : Type(com.google.api.expr.v1alpha1.Type) CheckerEnv.newStandardCheckerEnv(org.projectnessie.cel.checker.CheckerEnv.newStandardCheckerEnv) Test(org.junit.jupiter.api.Test)

Example 9 with Type

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

the class ConformanceServiceImpl method refValueToValue.

// TODO(jimlarson): The following conversion code should be moved to
// common/types/provider.go and consolidated/refactored as appropriate.
// In particular, make judicious use of types.NativeToValue().
/**
 * RefValueToValue converts between ref.Val and Value. The ref.Val must not be error or unknown.
 */
static Value refValueToValue(Val res) {
    switch(res.type().typeEnum()) {
        case Bool:
            return Value.newBuilder().setBoolValue(res.booleanValue()).build();
        case Bytes:
            return Value.newBuilder().setBytesValue(res.convertToNative(ByteString.class)).build();
        case Double:
            return Value.newBuilder().setDoubleValue(res.convertToNative(Double.class)).build();
        case Int:
            return Value.newBuilder().setInt64Value(res.intValue()).build();
        case Null:
            return Value.newBuilder().setNullValueValue(0).build();
        case String:
            return Value.newBuilder().setStringValue(res.value().toString()).build();
        case Type:
            return Value.newBuilder().setTypeValue(((TypeT) res).typeName()).build();
        case Uint:
            return Value.newBuilder().setUint64Value(res.intValue()).build();
        case Duration:
            Duration d = res.convertToNative(Duration.class);
            return Value.newBuilder().setObjectValue(Any.pack(d)).build();
        case Timestamp:
            Timestamp t = res.convertToNative(Timestamp.class);
            return Value.newBuilder().setObjectValue(Any.pack(t)).build();
        case List:
            Lister l = (Lister) res;
            ListValue.Builder elts = ListValue.newBuilder();
            for (IteratorT i = l.iterator(); i.hasNext() == True; ) {
                Val v = i.next();
                elts.addValues(refValueToValue(v));
            }
            return Value.newBuilder().setListValue(elts).build();
        case Map:
            Mapper m = (Mapper) res;
            MapValue.Builder elems = MapValue.newBuilder();
            for (IteratorT i = m.iterator(); i.hasNext() == True; ) {
                Val k = i.next();
                Val v = m.get(k);
                Value kv = refValueToValue(k);
                Value vv = refValueToValue(v);
                elems.addEntriesBuilder().setKey(kv).setValue(vv);
            }
            return Value.newBuilder().setMapValue(elems).build();
        case Object:
            // Object type
            Message pb = (Message) res.value();
            Value.Builder v = Value.newBuilder();
            // Somehow the conformance tests
            if (pb instanceof ListValue) {
                v.setListValue((ListValue) pb);
            } else if (pb instanceof MapValue) {
                v.setMapValue((MapValue) pb);
            } else {
                v.setObjectValue(Any.pack(pb));
            }
            return v.build();
        default:
            throw new IllegalStateException(String.format("Unknown %s", res.type().typeEnum()));
    }
}
Also used : Val(org.projectnessie.cel.common.types.ref.Val) TypeT(org.projectnessie.cel.common.types.TypeT) Message(com.google.protobuf.Message) Lister(org.projectnessie.cel.common.types.traits.Lister) ListValue(com.google.api.expr.v1alpha1.ListValue) Duration(com.google.protobuf.Duration) MapValue(com.google.api.expr.v1alpha1.MapValue) Timestamp(com.google.protobuf.Timestamp) IteratorT(org.projectnessie.cel.common.types.IteratorT) Mapper(org.projectnessie.cel.common.types.traits.Mapper) MapValue(com.google.api.expr.v1alpha1.MapValue) Value(com.google.api.expr.v1alpha1.Value) ExprValue(com.google.api.expr.v1alpha1.ExprValue) ListValue(com.google.api.expr.v1alpha1.ListValue) TypeT.newObjectTypeValue(org.projectnessie.cel.common.types.TypeT.newObjectTypeValue)

Example 10 with Type

use of com.google.api.expr.v1alpha1.Type 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)

Aggregations

Type (com.google.api.expr.v1alpha1.Type)30 Test (org.junit.Test)16 MapType (com.google.api.expr.v1alpha1.Type.MapType)15 Type (edu.stanford.CVC4.Type)14 Type (com.google.spanner.v1.Type)12 Test (org.junit.jupiter.api.Test)12 CheckedExpr (com.google.api.expr.v1alpha1.CheckedExpr)11 ByteString (com.google.protobuf.ByteString)11 ArrayType (edu.stanford.CVC4.ArrayType)11 BitVectorType (edu.stanford.CVC4.BitVectorType)11 Expr (edu.stanford.CVC4.Expr)11 ArrayList (java.util.ArrayList)11 Type (org.apache.xbean.asm9.Type)10 Expr (com.google.api.expr.v1alpha1.Expr)9 CVC4.vectorExpr (edu.stanford.CVC4.vectorExpr)9 FieldType (org.projectnessie.cel.common.types.ref.FieldType)8 ListValue (com.google.protobuf.ListValue)7 ExecuteSqlRequest (com.google.spanner.v1.ExecuteSqlRequest)7 CheckerEnv.dynElementType (org.projectnessie.cel.checker.CheckerEnv.dynElementType)7 CheckerEnv.getObjectWellKnownType (org.projectnessie.cel.checker.CheckerEnv.getObjectWellKnownType)7