use of com.google.api.expr.v1alpha1.Type.MapType in project cel-java by projectnessie.
the class Checker method checkSelect.
void checkSelect(Expr.Builder e) {
Select.Builder sel = e.getSelectExprBuilder();
// Before traversing down the tree, try to interpret as qualified name.
String qname = Container.toQualifiedName(e.build());
if (qname != null) {
Decl ident = env.lookupIdent(qname);
if (ident != null) {
if (sel.getTestOnly()) {
errors.expressionDoesNotSelectField(location(e));
setType(e, Decls.Bool);
return;
}
// Rewrite the node to be a variable reference to the resolved fully-qualified
// variable name.
setType(e, ident.getIdent().getType());
setReference(e, newIdentReference(ident.getName(), ident.getIdent().getValue()));
String identName = ident.getName();
e.getIdentExprBuilder().setName(identName);
return;
}
}
// Interpret as field selection, first traversing down the operand.
check(sel.getOperandBuilder());
Type targetType = getType(sel.getOperandBuilder());
// Assume error type by default as most types do not support field selection.
Type resultType = Decls.Error;
switch(kindOf(targetType)) {
case kindMap:
// Maps yield their value type as the selection result type.
MapType mapType = targetType.getMapType();
resultType = mapType.getValueType();
break;
case kindObject:
// Objects yield their field type declaration as the selection result type, but only if
// the field is defined.
FieldType fieldType = lookupFieldType(location(e), targetType.getMessageType(), sel.getField());
if (fieldType != null) {
resultType = fieldType.type;
}
break;
case kindTypeParam:
// Set the operand type to DYN to prevent assignment to a potentionally incorrect type
// at a later point in type-checking. The isAssignable call will update the type
// substitutions for the type param under the covers.
isAssignable(Decls.Dyn, targetType);
// Also, set the result type to DYN.
resultType = Decls.Dyn;
break;
default:
// in order to allow forward progress on the check.
if (isDynOrError(targetType)) {
resultType = Decls.Dyn;
} else {
errors.typeDoesNotSupportFieldSelection(location(e), targetType);
}
break;
}
if (sel.getTestOnly()) {
resultType = Decls.Bool;
}
setType(e, resultType);
}
use of com.google.api.expr.v1alpha1.Type.MapType 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.MapType in project cel-java by projectnessie.
the class PbTypeDescriptionTest method checkedType.
@Test
void checkedType() {
Db pbdb = newDb();
TestAllTypes msg = TestAllTypes.getDefaultInstance();
String msgName = msg.getDescriptorForType().getFullName();
pbdb.registerMessage(msg);
PbTypeDescription td = pbdb.describeType(msgName);
assertThat(td).isNotNull();
FieldDescription field = td.fieldByName("map_string_string");
assertThat(field).isNotNull();
Type mapType = Decls.newMapType(Decls.String, Decls.String);
assertThat(field.checkedType()).isEqualTo(mapType);
field = td.fieldByName("repeated_nested_message");
assertThat(field).isNotNull();
Type listType = Decls.newListType(Decls.newObjectType("google.api.expr.test.v1.proto3.TestAllTypes.NestedMessage"));
assertThat(field.checkedType()).isEqualTo(listType);
}
use of com.google.api.expr.v1alpha1.Type.MapType in project cel-java by projectnessie.
the class JacksonTypeDescriptionTest method checkMapType.
private void checkMapType(JacksonRegistry reg, String prop, Class<?> keyClass, com.google.api.expr.v1alpha1.Type keyType, Class<?> valueClass, com.google.api.expr.v1alpha1.Type valueType) {
JacksonFieldType ft = (JacksonFieldType) reg.findFieldType(CollectionsObject.class.getName(), prop);
assertThat(ft).isNotNull();
JavaType javaType = ft.propertyWriter().getType();
assertThat(javaType).extracting(JavaType::isMapLikeType).isEqualTo(true);
assertThat(javaType.getKeyType()).extracting(JavaType::getRawClass).isSameAs(keyClass);
assertThat(javaType.getContentType()).extracting(JavaType::getRawClass).isSameAs(valueClass);
assertThat(ft.type).extracting(com.google.api.expr.v1alpha1.Type::getMapType).extracting(MapType::getKeyType, MapType::getValueType).containsExactly(keyType, valueType);
}
use of com.google.api.expr.v1alpha1.Type.MapType in project cel-java by projectnessie.
the class Types method notReferencedIn.
/**
* notReferencedIn checks whether the type doesn't appear directly or transitively within the
* other type. This is a standard requirement for type unification, commonly referred to as the
* "occurs check".
*/
static boolean notReferencedIn(Mapping m, Type t, Type withinType) {
if (t.equals(withinType)) {
return false;
}
Kind withinKind = kindOf(withinType);
switch(withinKind) {
case kindTypeParam:
Type wtSub = m.find(withinType);
if (wtSub == null) {
return true;
}
return notReferencedIn(m, t, wtSub);
case kindAbstract:
for (Type pt : withinType.getAbstractType().getParameterTypesList()) {
if (!notReferencedIn(m, t, pt)) {
return false;
}
}
return true;
case kindFunction:
FunctionType fn = withinType.getFunction();
List<Type> types = flattenFunctionTypes(fn);
for (Type a : types) {
if (!notReferencedIn(m, t, a)) {
return false;
}
}
return true;
case kindList:
return notReferencedIn(m, t, withinType.getListType().getElemType());
case kindMap:
MapType mt = withinType.getMapType();
return notReferencedIn(m, t, mt.getKeyType()) && notReferencedIn(m, t, mt.getValueType());
case kindWrapper:
return notReferencedIn(m, t, Decls.newPrimitiveType(withinType.getWrapper()));
default:
return true;
}
}
Aggregations