use of org.ballerinalang.model.types.BType in project ballerina by ballerina-lang.
the class BLangVMStructs method createBStruct.
/**
* Create BStruct for given StructInfo and BValues.
*
* @param structInfo {@link StructInfo} of the BStruct
* @param values field values of the BStruct.
* @return BStruct instance.
*/
public static BStruct createBStruct(StructInfo structInfo, Object... values) {
BStructType structType = structInfo.getType();
BStruct bStruct = new BStruct(structType);
int[] indexes = new int[] { -1, -1, -1, -1, -1, -1 };
BStructType.StructField[] structFields = structType.getStructFields();
for (int i = 0; i < structFields.length; i++) {
if (values.length < i + 1) {
break;
}
BType paramType = structFields[i].getFieldType();
setValue(bStruct, indexes, paramType.getTag(), values[i]);
}
return bStruct;
}
use of org.ballerinalang.model.types.BType in project ballerina by ballerina-lang.
the class BLangProgramRunner method getMainFunction.
public static FunctionInfo getMainFunction(PackageInfo mainPkgInfo) {
String errorMsg = "main function not found in '" + mainPkgInfo.getProgramFile().getProgramFilePath() + "'";
FunctionInfo mainFuncInfo = mainPkgInfo.getFunctionInfo("main");
if (mainFuncInfo == null) {
throw new BallerinaException(errorMsg);
}
BType[] paramTypes = mainFuncInfo.getParamTypes();
BType[] retParamTypes = mainFuncInfo.getRetParamTypes();
BArrayType argsType = new BArrayType(BTypes.typeString);
if (paramTypes.length != 1 || !paramTypes[0].equals(argsType) || retParamTypes.length != 0) {
throw new BallerinaException(errorMsg);
}
return mainFuncInfo;
}
use of org.ballerinalang.model.types.BType 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));
}
use of org.ballerinalang.model.types.BType in project ballerina by ballerina-lang.
the class JSONUtils method convertJSONNodeToStruct.
/**
* Convert a BJSON to a user defined struct.
*
* @param jsonNode JSON to convert
* @param structType Type (definition) of the target struct
* @return If the provided JSON is of object-type, this method will return a {@link BStruct} containing the values
* of the JSON object. Otherwise the method will throw a {@link BallerinaException}.
*/
public static BStruct convertJSONNodeToStruct(JsonNode jsonNode, BStructType structType) {
if (!jsonNode.isObject()) {
throw BLangExceptionHelper.getRuntimeException(RuntimeErrors.INCOMPATIBLE_TYPE_FOR_CASTING, getComplexObjectTypeName(Type.OBJECT), getTypeName(jsonNode));
}
int longRegIndex = -1;
int doubleRegIndex = -1;
int stringRegIndex = -1;
int booleanRegIndex = -1;
int refRegIndex = -1;
int blobRegIndex = -1;
BStruct bStruct = new BStruct(structType);
StructInfo structInfo = structType.structInfo;
boolean fieldExists;
for (StructFieldInfo fieldInfo : structInfo.getFieldInfoEntries()) {
BType fieldType = fieldInfo.getFieldType();
String fieldName = fieldInfo.getName();
try {
fieldExists = jsonNode.has(fieldName);
JsonNode jsonValue = jsonNode.get(fieldName);
switch(fieldType.getTag()) {
case TypeTags.INT_TAG:
longRegIndex++;
if (fieldExists) {
bStruct.setIntField(longRegIndex, jsonNodeToInt(jsonValue));
}
break;
case TypeTags.FLOAT_TAG:
doubleRegIndex++;
if (fieldExists) {
bStruct.setFloatField(doubleRegIndex, jsonNodeToFloat(jsonValue));
}
break;
case TypeTags.STRING_TAG:
stringRegIndex++;
String stringVal;
if (!fieldExists) {
stringVal = "";
} else if (jsonValue.isString()) {
stringVal = jsonValue.stringValue();
} else {
stringVal = jsonValue.toString();
}
bStruct.setStringField(stringRegIndex, stringVal);
break;
case TypeTags.BOOLEAN_TAG:
booleanRegIndex++;
if (fieldExists) {
bStruct.setBooleanField(booleanRegIndex, jsonNodeToBool(jsonValue) ? 1 : 0);
}
break;
case TypeTags.UNION_TAG:
case TypeTags.STRUCT_TAG:
case TypeTags.ANY_TAG:
case TypeTags.JSON_TAG:
case TypeTags.ARRAY_TAG:
case TypeTags.MAP_TAG:
case TypeTags.NULL_TAG:
refRegIndex++;
if (fieldExists) {
bStruct.setRefField(refRegIndex, (BRefType<?>) convertJSON(jsonValue, fieldType));
}
break;
case TypeTags.FUNCTION_POINTER_TAG:
if (fieldExists) {
throw BLangExceptionHelper.getRuntimeException(RuntimeErrors.INCOMPATIBLE_TYPE_FOR_CASTING, fieldName, getTypeName(jsonValue));
}
// TODO: set the default value
bStruct.setRefField(++refRegIndex, null);
break;
case TypeTags.BLOB_TAG:
bStruct.setBlobField(++blobRegIndex, new byte[0]);
break;
default:
throw BLangExceptionHelper.getRuntimeException(RuntimeErrors.INCOMPATIBLE_TYPE_FOR_CASTING, fieldName, getTypeName(jsonValue));
}
} catch (Exception e) {
handleError(e, fieldName);
}
}
return bStruct;
}
use of org.ballerinalang.model.types.BType in project ballerina by ballerina-lang.
the class TableIterator method generateNext.
@Override
public BStruct generateNext() {
BStruct bStruct = new BStruct(type);
int longRegIndex = -1;
int doubleRegIndex = -1;
int stringRegIndex = -1;
int booleanRegIndex = -1;
int refRegIndex = -1;
int blobRegIndex = -1;
int index = 0;
try {
BStructType.StructField[] structFields = type.getStructFields();
for (BStructType.StructField sf : structFields) {
BType type = sf.getFieldType();
++index;
switch(type.getTag()) {
case TypeTags.INT_TAG:
long iValue = rs.getInt(index);
bStruct.setIntField(++longRegIndex, iValue);
break;
case TypeTags.STRING_TAG:
String sValue = rs.getString(index);
bStruct.setStringField(++stringRegIndex, sValue);
break;
case TypeTags.FLOAT_TAG:
double dalue = rs.getDouble(index);
bStruct.setFloatField(++doubleRegIndex, dalue);
break;
case TypeTags.BOOLEAN_TAG:
boolean boolValue = rs.getBoolean(index);
bStruct.setBooleanField(++booleanRegIndex, boolValue ? 1 : 0);
break;
case TypeTags.JSON_TAG:
String jsonValue = rs.getString(index);
bStruct.setRefField(++refRegIndex, new BJSON(jsonValue));
break;
case TypeTags.XML_TAG:
String xmlValue = rs.getString(index);
bStruct.setRefField(++refRegIndex, new BXMLItem(xmlValue));
break;
case TypeTags.BLOB_TAG:
Blob blobValue = rs.getBlob(index);
bStruct.setBlobField(++blobRegIndex, blobValue.getBytes(1L, (int) blobValue.length()));
break;
case TypeTags.ARRAY_TAG:
Array arrayValue = rs.getArray(index);
bStruct.setRefField(++refRegIndex, getDataArray(arrayValue));
break;
}
}
} catch (SQLException e) {
throw new BallerinaException("error in generating next row of data :" + e.getMessage());
}
return bStruct;
}
Aggregations