Search in sources :

Example 1 with AbstractType

use of com.google.api.expr.v1alpha1.Type.AbstractType 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;
    }
}
Also used : AbstractType(com.google.api.expr.v1alpha1.Type.AbstractType) PrimitiveType(com.google.api.expr.v1alpha1.Type.PrimitiveType) Type(com.google.api.expr.v1alpha1.Type) WellKnownType(com.google.api.expr.v1alpha1.Type.WellKnownType) FunctionType(com.google.api.expr.v1alpha1.Type.FunctionType) MapType(com.google.api.expr.v1alpha1.Type.MapType) FunctionType(com.google.api.expr.v1alpha1.Type.FunctionType) AbstractType(com.google.api.expr.v1alpha1.Type.AbstractType) ArrayList(java.util.ArrayList) MapType(com.google.api.expr.v1alpha1.Type.MapType)

Aggregations

Type (com.google.api.expr.v1alpha1.Type)1 AbstractType (com.google.api.expr.v1alpha1.Type.AbstractType)1 FunctionType (com.google.api.expr.v1alpha1.Type.FunctionType)1 MapType (com.google.api.expr.v1alpha1.Type.MapType)1 PrimitiveType (com.google.api.expr.v1alpha1.Type.PrimitiveType)1 WellKnownType (com.google.api.expr.v1alpha1.Type.WellKnownType)1 ArrayList (java.util.ArrayList)1