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;
}
}
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);
}
}
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);
});
}
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);
}
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;
}
Aggregations