use of com.google.api.expr.v1alpha1.Type in project cel-java by projectnessie.
the class FieldDescription method typeDefToType.
public Type typeDefToType() {
switch(desc.getJavaType()) {
case MESSAGE:
String msgType = desc.getMessageType().getFullName();
Type wk = Checked.CheckedWellKnowns.get(msgType);
if (wk != null) {
return wk;
}
return Checked.checkedMessageType(msgType);
case ENUM:
return Checked.checkedInt;
case BOOLEAN:
return Checked.checkedBool;
case BYTE_STRING:
return Checked.checkedBytes;
case DOUBLE:
case FLOAT:
return Checked.checkedDouble;
case INT:
if (desc.getType() == FieldDescriptor.Type.UINT32 || desc.getType() == FieldDescriptor.Type.FIXED32) {
return Checked.checkedUint;
}
return Checked.checkedInt;
case LONG:
if (desc.getType() == FieldDescriptor.Type.UINT64 || desc.getType() == FieldDescriptor.Type.FIXED64) {
return Checked.checkedUint;
}
return Checked.checkedInt;
case STRING:
return Checked.checkedString;
}
throw new UnsupportedOperationException("Unknown JavaType " + desc.getJavaType());
}
use of com.google.api.expr.v1alpha1.Type in project cel-java by projectnessie.
the class Types method internalIsAssignable.
/**
* internalIsAssignable returns true if t1 is assignable to t2.
*/
static boolean internalIsAssignable(Mapping m, Type t1, Type t2) {
// Early terminate the call to avoid cases of infinite recursion.
if (t1.equals(t2)) {
return true;
}
// Process type parameters.
Kind kind1 = kindOf(t1);
Kind kind2 = kindOf(t2);
if (kind2 == Kind.kindTypeParam) {
Type t2Sub = m.find(t2);
if (t2Sub != null) {
// If the types are compatible, pick the more general type and return true
if (internalIsAssignable(m, t1, t2Sub)) {
m.add(t2, mostGeneral(t1, t2Sub));
return true;
}
return false;
}
if (notReferencedIn(m, t2, t1)) {
m.add(t2, t1);
return true;
}
}
if (kind1 == Kind.kindTypeParam) {
// For the lower type bound, we currently do not perform adjustment. The restricted
// way we use type parameters in lower type bounds, it is not necessary, but may
// become if we generalize type unification.
Type t1Sub = m.find(t1);
if (t1Sub != null) {
// If the types are compatible, pick the more general type and return true
if (internalIsAssignable(m, t1Sub, t2)) {
m.add(t1, mostGeneral(t1Sub, t2));
return true;
}
return false;
}
if (notReferencedIn(m, t1, t2)) {
m.add(t1, t2);
return true;
}
}
// Next check for wildcard types.
if (isDynOrError(t1) || isDynOrError(t2)) {
return true;
}
// Test for when the types do not need to agree, but are more specific than dyn.
switch(kind1) {
case kindNull:
return internalIsAssignableNull(t2);
case kindPrimitive:
return internalIsAssignablePrimitive(t1.getPrimitive(), t2);
case kindWrapper:
return internalIsAssignable(m, Decls.newPrimitiveType(t1.getWrapper()), t2);
default:
if (kind1 != kind2) {
return false;
}
}
// Test for when the types must agree.
switch(kind1) {
// ERROR, TYPE_PARAM, and DYN handled above.
case kindAbstract:
return internalIsAssignableAbstractType(m, t1.getAbstractType(), t2.getAbstractType());
case kindFunction:
return internalIsAssignableFunction(m, t1.getFunction(), t2.getFunction());
case kindList:
return internalIsAssignable(m, t1.getListType().getElemType(), t2.getListType().getElemType());
case kindMap:
return internalIsAssignableMap(m, t1.getMapType(), t2.getMapType());
case kindObject:
return t1.getMessageType().equals(t2.getMessageType());
case kindType:
// type cannot affect method resolution or assignability.
return true;
case kindWellKnown:
return t1.getWellKnown() == t2.getWellKnown();
default:
return false;
}
}
use of com.google.api.expr.v1alpha1.Type in project cel-java by projectnessie.
the class Types method substitute.
/**
* substitute replaces all direct and indirect occurrences of bound type parameters. Unbound type
* parameters are replaced by DYN if typeParamToDyn is true.
*/
static Type substitute(Mapping m, Type t, boolean typeParamToDyn) {
Type tSub = m.find(t);
if (tSub != null) {
return substitute(m, tSub, typeParamToDyn);
}
Kind kind = kindOf(t);
if (typeParamToDyn && kind == Kind.kindTypeParam) {
return Decls.Dyn;
}
switch(kind) {
case kindAbstract:
// TODO: implement!
AbstractType at = t.getAbstractType();
List<Type> params = new ArrayList<>(at.getParameterTypesCount());
for (Type p : at.getParameterTypesList()) {
params.add(substitute(m, p, typeParamToDyn));
}
return Decls.newAbstractType(at.getName(), params);
case kindFunction:
FunctionType fn = t.getFunction();
Type rt = substitute(m, fn.getResultType(), typeParamToDyn);
List<Type> args = new ArrayList<>(fn.getArgTypesCount());
for (Type a : fn.getArgTypesList()) {
args.add(substitute(m, a, typeParamToDyn));
}
return Decls.newFunctionType(rt, args);
case kindList:
return Decls.newListType(substitute(m, t.getListType().getElemType(), typeParamToDyn));
case kindMap:
MapType mt = t.getMapType();
return Decls.newMapType(substitute(m, mt.getKeyType(), typeParamToDyn), substitute(m, mt.getValueType(), typeParamToDyn));
case kindType:
if (t.getType() != Type.getDefaultInstance()) {
return Decls.newTypeType(substitute(m, t.getType(), typeParamToDyn));
}
return t;
default:
return t;
}
}
use of com.google.api.expr.v1alpha1.Type in project cel-java by projectnessie.
the class AttributesTest method benchmarkResolverFieldQualifier.
@Test
void benchmarkResolverFieldQualifier() {
TestAllTypes msg = TestAllTypes.newBuilder().setSingleNestedMessage(NestedMessage.newBuilder().setBb(123)).build();
TypeRegistry reg = newRegistry(msg);
AttributeFactory attrs = newAttributeFactory(Container.defaultContainer, reg, reg);
Activation vars = newActivation(mapOf("msg", msg));
NamespacedAttribute attr = attrs.absoluteAttribute(1, "msg");
Type opType = reg.findType("google.api.expr.test.v1.proto3.TestAllTypes");
assertThat(opType).isNotNull();
Type fieldType = reg.findType("google.api.expr.test.v1.proto3.TestAllTypes.NestedMessage");
assertThat(fieldType).isNotNull();
attr.addQualifier(makeQualifier(attrs, opType.getType(), 2, "single_nested_message"));
attr.addQualifier(makeQualifier(attrs, fieldType.getType(), 3, "bb"));
// Note: migrated to JMH
}
use of com.google.api.expr.v1alpha1.Type in project cel-java by projectnessie.
the class FieldDescriptionTest method fieldDescription.
@Test
void fieldDescription() {
Db pbdb = newDb();
NestedTestAllTypes msg = NestedTestAllTypes.getDefaultInstance();
String msgName = msg.getDescriptorForType().getFullName();
pbdb.registerMessage(msg);
PbTypeDescription td = pbdb.describeType(msgName);
assertThat(td).isNotNull();
FieldDescription fd = td.fieldByName("payload");
assertThat(fd).isNotNull();
assertThat(fd).extracting(FieldDescription::name).isEqualTo("payload");
assertThat(fd).extracting(FieldDescription::isOneof).isEqualTo(false);
assertThat(fd).extracting(FieldDescription::isMap).isEqualTo(false);
assertThat(fd).extracting(FieldDescription::isMessage).isEqualTo(true);
assertThat(fd).extracting(FieldDescription::isEnum).isEqualTo(false);
assertThat(fd).extracting(FieldDescription::isList).isEqualTo(false);
// Access the field by its Go struct name and check to see that it's index
// matches the one determined by the TypeDescription utils.
Type got = fd.checkedType();
Type wanted = Type.newBuilder().setMessageType("google.api.expr.test.v1.proto3.TestAllTypes").build();
assertThat(got).isEqualTo(wanted);
}
Aggregations