Search in sources :

Example 1 with BTableType

use of org.ballerinalang.model.types.BTableType in project ballerina by ballerina-lang.

the class ProgramFileReader method createBTypeFromSig.

private int createBTypeFromSig(char[] chars, int index, Stack<BType> typeStack, PackageInfo packageInfo) {
    int nameIndex;
    char ch = chars[index];
    switch(ch) {
        case 'I':
            typeStack.push(BTypes.typeInt);
            return index + 1;
        case 'F':
            typeStack.push(BTypes.typeFloat);
            return index + 1;
        case 'S':
            typeStack.push(BTypes.typeString);
            return index + 1;
        case 'B':
            typeStack.push(BTypes.typeBoolean);
            return index + 1;
        case 'L':
            typeStack.push(BTypes.typeBlob);
            return index + 1;
        case 'Y':
            typeStack.push(BTypes.typeDesc);
            return index + 1;
        case 'A':
            typeStack.push(BTypes.typeAny);
            return index + 1;
        case 'R':
            // TODO Improve this logic
            index++;
            nameIndex = index;
            while (chars[nameIndex] != ';') {
                nameIndex++;
            }
            String typeName = new String(Arrays.copyOfRange(chars, index, nameIndex));
            typeStack.push(BTypes.getTypeFromName(typeName));
            return nameIndex + 1;
        case 'C':
        case 'J':
        case 'T':
        case 'E':
        case 'D':
        case 'H':
        case 'Z':
            char typeChar = chars[index];
            // TODO Improve this logic
            index++;
            nameIndex = index;
            int colonIndex = -1;
            while (chars[nameIndex] != ';') {
                if (chars[nameIndex] == ':') {
                    colonIndex = nameIndex;
                }
                nameIndex++;
            }
            String pkgPath;
            String name;
            PackageInfo packageInfoOfType;
            if (colonIndex != -1) {
                pkgPath = new String(Arrays.copyOfRange(chars, index, colonIndex));
                name = new String(Arrays.copyOfRange(chars, colonIndex + 1, nameIndex));
                packageInfoOfType = programFile.getPackageInfo(pkgPath);
            } else {
                name = new String(Arrays.copyOfRange(chars, index, nameIndex));
                // Setting the current package;
                packageInfoOfType = packageInfo;
            }
            if (typeChar == 'C') {
                typeStack.push(packageInfoOfType.getConnectorInfo(name).getType());
            } else if (typeChar == 'J') {
                if (name.isEmpty()) {
                    typeStack.push(BTypes.typeJSON);
                } else {
                    typeStack.push(new BJSONType(packageInfoOfType.getStructInfo(name).getType()));
                }
            } else if (typeChar == 'D') {
                if (name.isEmpty()) {
                    typeStack.push(BTypes.typeTable);
                } else {
                    typeStack.push(new BTableType(packageInfoOfType.getStructInfo(name).getType()));
                }
            } else if (typeChar == 'H') {
                if (name.isEmpty()) {
                    typeStack.push(BTypes.typeStream);
                } else {
                    typeStack.push(new BStreamType(packageInfoOfType.getStructInfo(name).getType()));
                }
            } else if (typeChar == 'E') {
                typeStack.push(packageInfoOfType.getEnumInfo(name).getType());
            } else {
                // This is a struct type
                typeStack.push(packageInfoOfType.getStructInfo(name).getType());
            }
            return nameIndex + 1;
        case '[':
            index = createBTypeFromSig(chars, index + 1, typeStack, packageInfo);
            BType elemType = typeStack.pop();
            BArrayType arrayType = new BArrayType(elemType);
            typeStack.push(arrayType);
            return index;
        case 'M':
            index = createBTypeFromSig(chars, index + 1, typeStack, packageInfo);
            BType constrainedType = typeStack.pop();
            BType mapType;
            if (constrainedType == BTypes.typeAny) {
                mapType = BTypes.typeMap;
            } else {
                mapType = new BMapType(constrainedType);
            }
            typeStack.push(mapType);
            return index;
        case 'U':
            // TODO : Fix this for type casting.
            typeStack.push(new BFunctionType());
            return index + 1;
        case 'O':
        case 'P':
            typeChar = chars[index];
            index++;
            nameIndex = index;
            while (chars[nameIndex] != ';') {
                nameIndex++;
            }
            List<BType> memberTypes = new ArrayList<>();
            int memberCount = Integer.parseInt(new String(Arrays.copyOfRange(chars, index, nameIndex)));
            index = nameIndex;
            for (int i = 0; i < memberCount; i++) {
                index = createBTypeFromSig(chars, index + 1, typeStack, packageInfo) - 1;
                memberTypes.add(typeStack.pop());
            }
            if (typeChar == 'O') {
                typeStack.push(new BUnionType(memberTypes));
            } else if (typeChar == 'P') {
                typeStack.push(new BTupleType(memberTypes));
            }
            return index + 1;
        case 'N':
            typeStack.push(BTypes.typeNull);
            return index + 1;
        default:
            throw new ProgramFileFormatException("unsupported base type char: " + ch);
    }
}
Also used : BArrayType(org.ballerinalang.model.types.BArrayType) BJSONType(org.ballerinalang.model.types.BJSONType) BFunctionType(org.ballerinalang.model.types.BFunctionType) ArrayList(java.util.ArrayList) BTupleType(org.ballerinalang.model.types.BTupleType) ProgramFileFormatException(org.ballerinalang.util.exceptions.ProgramFileFormatException) BStreamType(org.ballerinalang.model.types.BStreamType) BUnionType(org.ballerinalang.model.types.BUnionType) BMapType(org.ballerinalang.model.types.BMapType) BType(org.ballerinalang.model.types.BType) BTableType(org.ballerinalang.model.types.BTableType)

Example 2 with BTableType

use of org.ballerinalang.model.types.BTableType in project ballerina by ballerina-lang.

the class LoadToTable method getbTable.

private static BTable getbTable(Context context, List records) throws BallerinaIOException {
    BTypeDescValue type = (BTypeDescValue) context.getRefArgument(0);
    BTable table = new BTable(new BTableType(type.value()));
    BStructType structType = (BStructType) type.value();
    boolean skipHeaderLine = context.getBooleanArgument(0);
    if (skipHeaderLine && !records.isEmpty()) {
        records.remove(0);
    }
    for (Object obj : records) {
        String[] fields = (String[]) obj;
        final BStruct struct = getStruct(fields, structType);
        if (struct != null) {
            table.addData(struct);
        }
    }
    return table;
}
Also used : BStructType(org.ballerinalang.model.types.BStructType) BTypeDescValue(org.ballerinalang.model.values.BTypeDescValue) BTable(org.ballerinalang.model.values.BTable) BStruct(org.ballerinalang.model.values.BStruct) BTableType(org.ballerinalang.model.types.BTableType)

Example 3 with BTableType

use of org.ballerinalang.model.types.BTableType in project ballerina by ballerina-lang.

the class ProgramFileReader method getBTypeFromDescriptor.

private BType getBTypeFromDescriptor(String desc) {
    char ch = desc.charAt(0);
    switch(ch) {
        case 'I':
            return BTypes.typeInt;
        case 'F':
            return BTypes.typeFloat;
        case 'S':
            return BTypes.typeString;
        case 'B':
            return BTypes.typeBoolean;
        case 'Y':
            return BTypes.typeDesc;
        case 'L':
            return BTypes.typeBlob;
        case 'A':
            return BTypes.typeAny;
        case 'R':
            return BTypes.getTypeFromName(desc.substring(1, desc.length() - 1));
        case 'M':
            BType constrainedType = getBTypeFromDescriptor(desc.substring(1));
            if (constrainedType == BTypes.typeAny) {
                return BTypes.typeMap;
            } else {
                return new BMapType(constrainedType);
            }
        case 'C':
        case 'X':
        case 'J':
        case 'T':
        case 'E':
        case 'H':
        case 'Z':
        case 'D':
            String typeName = desc.substring(1, desc.length() - 1);
            String[] parts = typeName.split(":");
            if (parts.length == 1) {
                if (ch == 'J') {
                    return BTypes.typeJSON;
                } else if (ch == 'D') {
                    return BTypes.typeTable;
                } else if (ch == 'H') {
                    // TODO:CHECK
                    return BTypes.typeStream;
                }
            }
            String pkgPath = parts[0];
            String name = parts[1];
            PackageInfo packageInfoOfType = programFile.getPackageInfo(pkgPath);
            if (ch == 'J') {
                return new BJSONType(packageInfoOfType.getStructInfo(name).getType());
            } else if (ch == 'C') {
                return packageInfoOfType.getConnectorInfo(name).getType();
            } else if (ch == 'X') {
                return packageInfoOfType.getServiceInfo(name).getType();
            } else if (ch == 'D') {
                return new BTableType(packageInfoOfType.getStructInfo(name).getType());
            } else if (ch == 'H') {
                return new BStreamType(packageInfoOfType.getStructInfo(name).getType());
            } else if (ch == 'E') {
                return packageInfoOfType.getEnumInfo(name).getType();
            } else {
                return packageInfoOfType.getStructInfo(name).getType();
            }
        case '[':
            BType elemType = getBTypeFromDescriptor(desc.substring(1));
            return new BArrayType(elemType);
        case 'U':
            // TODO : Fix this for type casting.
            return new BFunctionType();
        case 'O':
        case 'P':
            Stack<BType> typeStack = new Stack<BType>();
            createBTypeFromSig(desc.toCharArray(), 0, typeStack, null);
            return typeStack.pop();
        case 'N':
            return BTypes.typeNull;
        default:
            throw new ProgramFileFormatException("unsupported base type char: " + ch);
    }
}
Also used : BArrayType(org.ballerinalang.model.types.BArrayType) BJSONType(org.ballerinalang.model.types.BJSONType) BFunctionType(org.ballerinalang.model.types.BFunctionType) ProgramFileFormatException(org.ballerinalang.util.exceptions.ProgramFileFormatException) BStreamType(org.ballerinalang.model.types.BStreamType) Stack(java.util.Stack) BMapType(org.ballerinalang.model.types.BMapType) BType(org.ballerinalang.model.types.BType) BTableType(org.ballerinalang.model.types.BTableType)

Aggregations

BTableType (org.ballerinalang.model.types.BTableType)3 BArrayType (org.ballerinalang.model.types.BArrayType)2 BFunctionType (org.ballerinalang.model.types.BFunctionType)2 BJSONType (org.ballerinalang.model.types.BJSONType)2 BMapType (org.ballerinalang.model.types.BMapType)2 BStreamType (org.ballerinalang.model.types.BStreamType)2 BType (org.ballerinalang.model.types.BType)2 ProgramFileFormatException (org.ballerinalang.util.exceptions.ProgramFileFormatException)2 ArrayList (java.util.ArrayList)1 Stack (java.util.Stack)1 BStructType (org.ballerinalang.model.types.BStructType)1 BTupleType (org.ballerinalang.model.types.BTupleType)1 BUnionType (org.ballerinalang.model.types.BUnionType)1 BStruct (org.ballerinalang.model.values.BStruct)1 BTable (org.ballerinalang.model.values.BTable)1 BTypeDescValue (org.ballerinalang.model.values.BTypeDescValue)1