Search in sources :

Example 1 with BUnionType

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

the class JSONUtils method convertJSON.

private static Object convertJSON(JsonNode jsonValue, BType targetType) {
    switch(targetType.getTag()) {
        case TypeTags.INT_TAG:
            return jsonNodeToInt(jsonValue);
        case TypeTags.FLOAT_TAG:
            return jsonNodeToFloat(jsonValue);
        case TypeTags.STRING_TAG:
            if (jsonValue.isString()) {
                return jsonValue.stringValue();
            } else {
                return jsonValue.toString();
            }
        case TypeTags.BOOLEAN_TAG:
            return jsonNodeToBool(jsonValue);
        case TypeTags.UNION_TAG:
            BUnionType type = (BUnionType) targetType;
            if (jsonValue.isNull() && type.isNullable()) {
                return null;
            }
            List<BType> matchingTypes = type.getMemberTypes().stream().filter(memberType -> memberType != BTypes.typeNull).collect(Collectors.toList());
            if (matchingTypes.size() == 1) {
                return convertJSON(jsonValue, matchingTypes.get(0));
            }
            break;
        case TypeTags.STRUCT_TAG:
            return convertJSONNodeToStruct(jsonValue, (BStructType) targetType);
        case TypeTags.ANY_TAG:
        case TypeTags.JSON_TAG:
            if (jsonValue.isNull()) {
                return null;
            }
            return new BJSON(jsonValue);
        case TypeTags.ARRAY_TAG:
            return jsonNodeToBArray(jsonValue, (BArrayType) targetType);
        case TypeTags.MAP_TAG:
            return jsonNodeToBMap(jsonValue, (BMapType) targetType);
        case TypeTags.NULL_TAG:
            if (jsonValue.isNull()) {
                return null;
            }
            break;
        default:
            break;
    }
    throw BLangExceptionHelper.getRuntimeException(RuntimeErrors.INCOMPATIBLE_TYPE_FOR_CASTING, targetType, getTypeName(jsonValue));
}
Also used : BUnionType(org.ballerinalang.model.types.BUnionType) BUnionType(org.ballerinalang.model.types.BUnionType) BFloat(org.ballerinalang.model.values.BFloat) BType(org.ballerinalang.model.types.BType) BBoolean(org.ballerinalang.model.values.BBoolean) OMText(org.apache.axiom.om.OMText) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException) BMapType(org.ballerinalang.model.types.BMapType) RuntimeErrors(org.ballerinalang.util.exceptions.RuntimeErrors) ArrayList(java.util.ArrayList) BJSON(org.ballerinalang.model.values.BJSON) BNewArray(org.ballerinalang.model.values.BNewArray) BXMLSequence(org.ballerinalang.model.values.BXMLSequence) OMNamespace(org.apache.axiom.om.OMNamespace) BString(org.ballerinalang.model.values.BString) TableJSONDataSource(org.ballerinalang.model.TableJSONDataSource) BInteger(org.ballerinalang.model.values.BInteger) BXML(org.ballerinalang.model.values.BXML) BTypes(org.ballerinalang.model.types.BTypes) BStruct(org.ballerinalang.model.values.BStruct) StructInfo(org.ballerinalang.util.codegen.StructInfo) BMap(org.ballerinalang.model.values.BMap) OMElement(org.apache.axiom.om.OMElement) BFloatArray(org.ballerinalang.model.values.BFloatArray) StructFieldInfo(org.ballerinalang.util.codegen.StructFieldInfo) Iterator(java.util.Iterator) Type(org.ballerinalang.model.util.JsonNode.Type) Set(java.util.Set) BStructType(org.ballerinalang.model.types.BStructType) BXMLItem(org.ballerinalang.model.values.BXMLItem) BLangExceptionHelper(org.ballerinalang.util.exceptions.BLangExceptionHelper) TypeTags(org.ballerinalang.model.types.TypeTags) Collectors(java.util.stream.Collectors) BStringArray(org.ballerinalang.model.values.BStringArray) BIntArray(org.ballerinalang.model.values.BIntArray) BArrayType(org.ballerinalang.model.types.BArrayType) List(java.util.List) OMAbstractFactory(org.apache.axiom.om.OMAbstractFactory) BRefType(org.ballerinalang.model.values.BRefType) Entry(java.util.Map.Entry) BRefValueArray(org.ballerinalang.model.values.BRefValueArray) OMFactory(org.apache.axiom.om.OMFactory) BTable(org.ballerinalang.model.values.BTable) BBooleanArray(org.ballerinalang.model.values.BBooleanArray) BValue(org.ballerinalang.model.values.BValue) BType(org.ballerinalang.model.types.BType) BJSON(org.ballerinalang.model.values.BJSON)

Example 2 with BUnionType

use of org.ballerinalang.model.types.BUnionType 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)

Aggregations

ArrayList (java.util.ArrayList)2 BArrayType (org.ballerinalang.model.types.BArrayType)2 BMapType (org.ballerinalang.model.types.BMapType)2 BType (org.ballerinalang.model.types.BType)2 BUnionType (org.ballerinalang.model.types.BUnionType)2 Iterator (java.util.Iterator)1 List (java.util.List)1 Entry (java.util.Map.Entry)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 OMAbstractFactory (org.apache.axiom.om.OMAbstractFactory)1 OMElement (org.apache.axiom.om.OMElement)1 OMFactory (org.apache.axiom.om.OMFactory)1 OMNamespace (org.apache.axiom.om.OMNamespace)1 OMText (org.apache.axiom.om.OMText)1 TableJSONDataSource (org.ballerinalang.model.TableJSONDataSource)1 BFunctionType (org.ballerinalang.model.types.BFunctionType)1 BJSONType (org.ballerinalang.model.types.BJSONType)1 BStreamType (org.ballerinalang.model.types.BStreamType)1 BStructType (org.ballerinalang.model.types.BStructType)1