Search in sources :

Example 6 with BArrayType

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

the class JSONUtils method jsonNodeToBArray.

/**
 * Convert a JSON node to an array.
 *
 * @param arrayNode JSON to convert
 * @param targetArrayType Type of the target array
 * @return If the provided JSON is of array type, this method will return a {@link BArrayType} containing the values
 *         of the JSON array. Otherwise the method will throw a {@link BallerinaException}.
 */
@SuppressWarnings("rawtypes")
private static BNewArray jsonNodeToBArray(JsonNode arrayNode, BArrayType targetArrayType) {
    if (!arrayNode.isArray()) {
        throw BLangExceptionHelper.getRuntimeException(RuntimeErrors.INCOMPATIBLE_TYPE_FOR_CASTING, getComplexObjectTypeName(Type.ARRAY), getTypeName(arrayNode));
    }
    BType elementType = targetArrayType.getElementType();
    BRefValueArray refValueArray;
    switch(elementType.getTag()) {
        case TypeTags.INT_TAG:
            return jsonNodeToBIntArray(arrayNode);
        case TypeTags.FLOAT_TAG:
            return jsonNodeToBFloatArray(arrayNode);
        case TypeTags.STRING_TAG:
            return jsonNodeToBStringArray(arrayNode);
        case TypeTags.BOOLEAN_TAG:
            return jsonNodeToBBooleanArray(arrayNode);
        case TypeTags.ANY_TAG:
            refValueArray = new BRefValueArray(elementType);
            for (int i = 0; i < arrayNode.size(); i++) {
                JsonNode element = arrayNode.get(i);
                refValueArray.add(i, (BRefType) getBValue(element));
            }
            return refValueArray;
        default:
            refValueArray = new BRefValueArray(elementType);
            for (int i = 0; i < arrayNode.size(); i++) {
                JsonNode element = arrayNode.get(i);
                if (elementType.getTag() == TypeTags.MAP_TAG) {
                    refValueArray.add(i, jsonNodeToBMap(element, (BMapType) elementType));
                } else if (elementType instanceof BStructType) {
                    refValueArray.add(i, convertJSONNodeToStruct(element, (BStructType) elementType));
                } else if (elementType instanceof BArrayType) {
                    refValueArray.add(i, jsonNodeToBArray(element, (BArrayType) elementType));
                } else {
                    throw BLangExceptionHelper.getRuntimeException(RuntimeErrors.INCOMPATIBLE_TYPE_FOR_CASTING, elementType, getTypeName(element));
                }
            }
            return refValueArray;
    }
}
Also used : BStructType(org.ballerinalang.model.types.BStructType) BMapType(org.ballerinalang.model.types.BMapType) BArrayType(org.ballerinalang.model.types.BArrayType) BType(org.ballerinalang.model.types.BType) BRefValueArray(org.ballerinalang.model.values.BRefValueArray)

Example 7 with BArrayType

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

Example 8 with BArrayType

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

the class TestAnnotationProcessor method resolveFunctions.

/**
 * Resolve function names to {@link TesterinaFunction}s.
 *
 * @param suite {@link TestSuite} whose functions to be resolved.
 */
private static void resolveFunctions(TestSuite suite) {
    List<TesterinaFunction> functions = suite.getTestUtilityFunctions();
    List<String> functionNames = functions.stream().map(testerinaFunction -> testerinaFunction.getName()).collect(Collectors.toList());
    for (Test test : suite.getTests()) {
        if (test.getTestName() != null && functionNames.contains(test.getTestName())) {
            test.setTestFunction(functions.stream().filter(e -> e.getName().equals(test.getTestName())).findFirst().get());
        }
        if (test.getBeforeTestFunction() != null && functionNames.contains(test.getBeforeTestFunction())) {
            test.setBeforeTestFunctionObj(functions.stream().filter(e -> e.getName().equals(test.getBeforeTestFunction())).findFirst().get());
        }
        if (test.getAfterTestFunction() != null && functionNames.contains(test.getAfterTestFunction())) {
            test.setAfterTestFunctionObj(functions.stream().filter(e -> e.getName().equals(test.getAfterTestFunction())).findFirst().get());
        }
        if (test.getDataProvider() != null && functionNames.contains(test.getDataProvider())) {
            String dataProvider = test.getDataProvider();
            test.setDataProviderFunction(functions.stream().filter(e -> e.getName().equals(test.getDataProvider())).findFirst().map(func -> {
                // TODO these validations are not working properly with the latest refactoring
                if (func.getbFunction().getRetParamTypes().length == 1) {
                    BType bType = func.getbFunction().getRetParamTypes()[0];
                    if (bType.getTag() == TypeTags.ARRAY_TAG) {
                        BArrayType bArrayType = (BArrayType) bType;
                        if (bArrayType.getElementType().getTag() != TypeTags.ARRAY_TAG) {
                            String message = String.format("Data provider function [%s] should return an array of" + " arrays.", dataProvider);
                            throw new BallerinaException(message);
                        }
                    } else {
                        String message = String.format("Data provider function [%s] should return an array of " + "arrays.", dataProvider);
                        throw new BallerinaException(message);
                    }
                } else {
                    String message = String.format("Data provider function [%s] should have only one return type" + ".", dataProvider);
                    throw new BallerinaException(message);
                }
                return func;
            }).get());
            if (test.getDataProviderFunction() == null) {
                String message = String.format("Data provider function [%s] cannot be found.", dataProvider);
                throw new BallerinaException(message);
            }
        }
        for (String dependsOnFn : test.getDependsOnTestFunctions()) {
            // TODO handle missing func case
            test.addDependsOnTestFunction(functions.stream().filter(e -> e.getName().equals(dependsOnFn)).findFirst().get());
        }
    }
    // resolve mock functions
    suite.getMockFunctionNamesMap().forEach((id, functionName) -> {
        TesterinaFunction function = suite.getTestUtilityFunctions().stream().filter(e -> e.getName().equals(functionName)).findFirst().get();
        suite.addMockFunctionObj(id, function);
    });
    suite.getBeforeSuiteFunctionNames().forEach(functionName -> {
        TesterinaFunction function = suite.getTestUtilityFunctions().stream().filter(e -> e.getName().equals(functionName)).findFirst().get();
        suite.addBeforeSuiteFunctionObj(function);
    });
    suite.getAfterSuiteFunctionNames().forEach(functionName -> {
        TesterinaFunction function = suite.getTestUtilityFunctions().stream().filter(e -> e.getName().equals(functionName)).findFirst().get();
        suite.addAfterSuiteFunctionObj(function);
    });
    suite.getBeforeEachFunctionNames().forEach(functionName -> {
        TesterinaFunction function = suite.getTestUtilityFunctions().stream().filter(e -> e.getName().equals(functionName)).findFirst().get();
        suite.addBeforeEachFunctionObj(function);
    });
    suite.getAfterEachFunctionNames().forEach(functionName -> {
        TesterinaFunction function = suite.getTestUtilityFunctions().stream().filter(e -> e.getName().equals(functionName)).findFirst().get();
        suite.addAfterEachFunctionObj(function);
    });
}
Also used : Arrays(java.util.Arrays) PackageNode(org.ballerinalang.model.tree.PackageNode) BType(org.ballerinalang.model.types.BType) ProgramFile(org.ballerinalang.util.codegen.ProgramFile) TestSuite(org.ballerinalang.testerina.core.entity.TestSuite) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) DiagnosticLog(org.ballerinalang.util.diagnostic.DiagnosticLog) PackageInfo(org.ballerinalang.util.codegen.PackageInfo) ArrayList(java.util.ArrayList) SupportedAnnotationPackages(org.ballerinalang.compiler.plugins.SupportedAnnotationPackages) AbstractCompilerPlugin(org.ballerinalang.compiler.plugins.AbstractCompilerPlugin) Vector(java.util.Vector) Map(java.util.Map) TesterinaFunction(org.ballerinalang.testerina.core.entity.TesterinaFunction) BLangArrayLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangArrayLiteral) LinkedList(java.util.LinkedList) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) FunctionNode(org.ballerinalang.model.tree.FunctionNode) TypeTags(org.ballerinalang.model.types.TypeTags) Collectors(java.util.stream.Collectors) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode) BArrayType(org.ballerinalang.model.types.BArrayType) List(java.util.List) Instruction(org.ballerinalang.util.codegen.Instruction) Test(org.ballerinalang.testerina.core.entity.Test) Queue(java.util.Queue) FunctionInfo(org.ballerinalang.util.codegen.FunctionInfo) BArrayType(org.ballerinalang.model.types.BArrayType) Test(org.ballerinalang.testerina.core.entity.Test) BType(org.ballerinalang.model.types.BType) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException) TesterinaFunction(org.ballerinalang.testerina.core.entity.TesterinaFunction)

Example 9 with BArrayType

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

the class CPU method checkArrayCast.

private static boolean checkArrayCast(BType sourceType, BType targetType) {
    if (targetType.getTag() == TypeTags.ARRAY_TAG && sourceType.getTag() == TypeTags.ARRAY_TAG) {
        BArrayType sourceArrayType = (BArrayType) sourceType;
        BArrayType targetArrayType = (BArrayType) targetType;
        if (targetArrayType.getDimensions() > sourceArrayType.getDimensions()) {
            return false;
        }
        return checkArrayCast(sourceArrayType.getElementType(), targetArrayType.getElementType());
    } else if (sourceType.getTag() == TypeTags.ARRAY_TAG) {
        return targetType.getTag() == TypeTags.ANY_TAG;
    }
    return sourceType.equals(targetType);
}
Also used : BArrayType(org.ballerinalang.model.types.BArrayType)

Example 10 with BArrayType

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

the class ForkJoinWorkerResponseContext method getWorkerResult.

@SuppressWarnings("rawtypes")
private BRefValueArray getWorkerResult(String channelName) {
    if (channelName == null) {
        return null;
    }
    BRefValueArray bRefValueArray = new BRefValueArray(new BArrayType(BTypes.typeAny));
    WorkerDataChannel dataChannel = getWorkerDataChannel(channelName);
    BRefType[] results;
    boolean dataNotAvailable = true;
    while ((results = dataChannel.tryTakeData()) != null) {
        dataNotAvailable = false;
        for (int i = 0; i < results.length; i++) {
            bRefValueArray.add(i, results[i]);
        }
    }
    if (dataNotAvailable) {
        return null;
    }
    return bRefValueArray;
}
Also used : BRefType(org.ballerinalang.model.values.BRefType) BArrayType(org.ballerinalang.model.types.BArrayType) BRefValueArray(org.ballerinalang.model.values.BRefValueArray)

Aggregations

BArrayType (org.ballerinalang.model.types.BArrayType)11 BType (org.ballerinalang.model.types.BType)7 BMapType (org.ballerinalang.model.types.BMapType)4 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)4 ArrayList (java.util.ArrayList)3 BStructType (org.ballerinalang.model.types.BStructType)3 BRefValueArray (org.ballerinalang.model.values.BRefValueArray)3 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 BFunctionType (org.ballerinalang.model.types.BFunctionType)2 BJSONType (org.ballerinalang.model.types.BJSONType)2 BStreamType (org.ballerinalang.model.types.BStreamType)2 BTableType (org.ballerinalang.model.types.BTableType)2 BUnionType (org.ballerinalang.model.types.BUnionType)2 TypeTags (org.ballerinalang.model.types.TypeTags)2 BRefType (org.ballerinalang.model.values.BRefType)2 BStruct (org.ballerinalang.model.values.BStruct)2 Arrays (java.util.Arrays)1 Iterator (java.util.Iterator)1 LinkedList (java.util.LinkedList)1