use of org.ballerinalang.model.values.BStruct in project ballerina by ballerina-lang.
the class ConnectorSPIModelHelper method processAnnotations.
private static void processAnnotations(String pkgPath, ProgramFile programFile, AnnotatableNode annotatableNode) {
final BMap bMap = getAnnotationVariable(pkgPath, programFile);
final BValue map = bMap.get(annotatableNode.getAnnotationEntryKey());
if (map == null || map.getType().getTag() != BTypes.typeMap.getTag()) {
return;
}
BMap<String, BValue> annotationMap = (BMap<String, BValue>) map;
for (String key : annotationMap.keySet()) {
final BStruct annotationData = (BStruct) annotationMap.get(key);
StructImpl struct = null;
if (annotationData != null) {
struct = new StructImpl(annotationData);
}
final String annotaionQName = key.split("\\$")[0];
final String[] qNameParts = annotaionQName.split(":");
final AnnotationImpl annotation = new AnnotationImpl(qNameParts[1], qNameParts[0], struct);
annotatableNode.addAnnotation(annotaionQName, annotation);
}
}
use of org.ballerinalang.model.values.BStruct in project ballerina by ballerina-lang.
the class BLangConnectorSPIUtil method getPackageEndpoint.
public static BStruct getPackageEndpoint(ProgramFile programFile, String pkgName, String endpointName) {
final PackageInfo packageInfo = programFile.getPackageInfo(pkgName);
if (packageInfo == null) {
throw new BallerinaConnectorException("Incorrect package name");
}
final PackageVarInfo packageVarInfo = packageInfo.getPackageVarInfo(endpointName);
if (packageVarInfo == null) {
throw new BallerinaConnectorException("Can't locate " + endpointName + " endpoint variable");
}
return (BStruct) programFile.getGlobalMemoryBlock().getRefField(packageVarInfo.getGlobalMemIndex());
}
use of org.ballerinalang.model.values.BStruct in project ballerina by ballerina-lang.
the class BCompileUtil method createAndGetStruct.
public static BStruct createAndGetStruct(ProgramFile programFile, String packagePath, String structName) {
PackageInfo structPackageInfo = programFile.getPackageInfo(packagePath);
StructInfo structInfo = structPackageInfo.getStructInfo(structName);
BStructType structType = structInfo.getType();
return new BStruct(structType);
}
use of org.ballerinalang.model.values.BStruct 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.values.BStruct 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