use of org.ballerinalang.util.codegen.StructFieldInfo in project ballerina by ballerina-lang.
the class CPU method convertMapToStruct.
@SuppressWarnings({ "unchecked", "rawtypes" })
private static void convertMapToStruct(WorkerExecutionContext ctx, int[] operands, WorkerData sf) {
int i = operands[0];
int cpIndex = operands[1];
int j = operands[2];
TypeRefCPEntry typeRefCPEntry = (TypeRefCPEntry) ctx.constPool[cpIndex];
BMap<String, BValue> bMap = (BMap<String, BValue>) sf.refRegs[i];
if (bMap == null) {
handleNullRefError(ctx);
return;
}
int longRegIndex = -1;
int doubleRegIndex = -1;
int stringRegIndex = -1;
int booleanRegIndex = -1;
int blobRegIndex = -1;
int refRegIndex = -1;
BStructType structType = (BStructType) typeRefCPEntry.getType();
BStruct bStruct = new BStruct(structType);
StructInfo structInfo = ctx.callableUnitInfo.getPackageInfo().getStructInfo(structType.getName());
Set<String> keys = bMap.keySet();
for (StructFieldInfo fieldInfo : structInfo.getFieldInfoEntries()) {
String key = fieldInfo.getName();
BType fieldType = fieldInfo.getFieldType();
BValue mapVal = null;
try {
boolean containsField = keys.contains(key);
DefaultValueAttributeInfo defaultValAttrInfo = null;
if (containsField) {
mapVal = bMap.get(key);
if (mapVal == null && BTypes.isValueType(fieldType)) {
throw BLangExceptionHelper.getRuntimeException(RuntimeErrors.INCOMPATIBLE_FIELD_TYPE_FOR_CASTING, key, fieldType, null);
}
if (mapVal != null && !checkCast(mapVal, fieldType)) {
throw BLangExceptionHelper.getRuntimeException(RuntimeErrors.INCOMPATIBLE_FIELD_TYPE_FOR_CASTING, key, fieldType, mapVal.getType());
}
} else {
defaultValAttrInfo = (DefaultValueAttributeInfo) getAttributeInfo(fieldInfo, AttributeInfo.Kind.DEFAULT_VALUE_ATTRIBUTE);
}
switch(fieldType.getTag()) {
case TypeTags.INT_TAG:
longRegIndex++;
if (containsField) {
bStruct.setIntField(longRegIndex, ((BInteger) mapVal).intValue());
} else if (defaultValAttrInfo != null) {
bStruct.setIntField(longRegIndex, defaultValAttrInfo.getDefaultValue().getIntValue());
}
break;
case TypeTags.FLOAT_TAG:
doubleRegIndex++;
if (containsField) {
bStruct.setFloatField(doubleRegIndex, ((BFloat) mapVal).floatValue());
} else if (defaultValAttrInfo != null) {
bStruct.setFloatField(doubleRegIndex, defaultValAttrInfo.getDefaultValue().getFloatValue());
}
break;
case TypeTags.STRING_TAG:
stringRegIndex++;
if (containsField) {
bStruct.setStringField(stringRegIndex, ((BString) mapVal).stringValue());
} else if (defaultValAttrInfo != null) {
bStruct.setStringField(stringRegIndex, defaultValAttrInfo.getDefaultValue().getStringValue());
}
break;
case TypeTags.BOOLEAN_TAG:
booleanRegIndex++;
if (containsField) {
bStruct.setBooleanField(booleanRegIndex, ((BBoolean) mapVal).booleanValue() ? 1 : 0);
} else if (defaultValAttrInfo != null) {
bStruct.setBooleanField(booleanRegIndex, defaultValAttrInfo.getDefaultValue().getBooleanValue() ? 1 : 0);
}
break;
case TypeTags.BLOB_TAG:
blobRegIndex++;
if (containsField && mapVal != null) {
bStruct.setBlobField(blobRegIndex, ((BBlob) mapVal).blobValue());
}
break;
default:
bStruct.setRefField(++refRegIndex, (BRefType) mapVal);
}
} catch (BallerinaException e) {
sf.refRegs[j] = null;
String errorMsg = "cannot convert '" + bMap.getType() + "' to type '" + structType + ": " + e.getMessage();
handleTypeConversionError(ctx, sf, j, errorMsg);
return;
}
}
sf.refRegs[j] = bStruct;
}
use of org.ballerinalang.util.codegen.StructFieldInfo 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;
}
Aggregations