use of org.ballerinalang.model.types.BStructType in project ballerina by ballerina-lang.
the class LoadToTable method getStruct.
private static BStruct getStruct(String[] fields, final BStructType structType) {
BStructType.StructField[] internalStructFields = structType.getStructFields();
int fieldLength = internalStructFields.length;
BStruct struct = null;
if (fields.length > 0) {
if (internalStructFields.length != fields.length) {
String msg = "Record row fields count and the give struct's fields count are mismatch";
throw new BallerinaIOException(msg);
}
struct = new BStruct(structType);
int longRegIndex = -1;
int doubleRegIndex = -1;
int stringRegIndex = -1;
int booleanRegIndex = -1;
for (int i = 0; i < fieldLength; i++) {
String value = fields[i];
final BStructType.StructField internalStructField = internalStructFields[i];
final int type = internalStructField.getFieldType().getTag();
switch(type) {
case TypeTags.INT_TAG:
struct.setIntField(++longRegIndex, Long.parseLong(value));
break;
case TypeTags.FLOAT_TAG:
struct.setFloatField(++doubleRegIndex, Double.parseDouble(value));
break;
case TypeTags.STRING_TAG:
struct.setStringField(++stringRegIndex, value);
break;
case TypeTags.BOOLEAN_TAG:
struct.setBooleanField(++booleanRegIndex, (Boolean.parseBoolean(value)) ? 1 : 0);
break;
default:
throw new BallerinaIOException("Type casting support only for int, float, boolean and string. " + "Invalid value for the struct field: " + value);
}
}
}
return struct;
}
use of org.ballerinalang.model.types.BStructType in project ballerina by ballerina-lang.
the class LoadToTable method getbTable.
private static BTable getbTable(Context context, List records) throws BallerinaIOException {
BTypeDescValue type = (BTypeDescValue) context.getRefArgument(0);
BTable table = new BTable(new BTableType(type.value()));
BStructType structType = (BStructType) type.value();
boolean skipHeaderLine = context.getBooleanArgument(0);
if (skipHeaderLine && !records.isEmpty()) {
records.remove(0);
}
for (Object obj : records) {
String[] fields = (String[]) obj;
final BStruct struct = getStruct(fields, structType);
if (struct != null) {
table.addData(struct);
}
}
return table;
}
use of org.ballerinalang.model.types.BStructType in project ballerina by ballerina-lang.
the class CPU method checkJSONEquivalency.
private static boolean checkJSONEquivalency(JsonNode json, BJSONType sourceType, BJSONType targetType) {
BStructType sourceConstrainedType = (BStructType) sourceType.getConstrainedType();
BStructType targetConstrainedType = (BStructType) targetType.getConstrainedType();
// Casting to an unconstrained JSON
if (targetConstrainedType == null) {
// ideally we should't reach here. This is checked from typeChecker
return true;
}
// Casting from constrained JSON to constrained JSON
if (sourceConstrainedType != null) {
if (sourceConstrainedType.equals(targetConstrainedType)) {
return true;
}
return checkStructEquivalency(sourceConstrainedType, targetConstrainedType);
}
// Casting from unconstrained JSON to constrained JSON
BStructType.StructField[] tFields = targetConstrainedType.getStructFields();
for (int i = 0; i < tFields.length; i++) {
String fieldName = tFields[i].getFieldName();
if (!json.has(fieldName)) {
return false;
}
if (!checkJSONCast(json.get(fieldName), sourceType, tFields[i].getFieldType())) {
return false;
}
}
return true;
}
use of org.ballerinalang.model.types.BStructType in project ballerina by ballerina-lang.
the class CPU method convertJSONToStruct.
private static void convertJSONToStruct(WorkerExecutionContext ctx, int[] operands, WorkerData sf) {
int i = operands[0];
int cpIndex = operands[1];
int j = operands[2];
TypeRefCPEntry typeRefCPEntry = (TypeRefCPEntry) ctx.constPool[cpIndex];
BJSON bjson = (BJSON) sf.refRegs[i];
if (bjson == null) {
handleNullRefError(ctx);
return;
}
try {
sf.refRegs[j] = JSONUtils.convertJSONToStruct(bjson, (BStructType) typeRefCPEntry.getType());
} catch (Exception e) {
String errorMsg = "cannot convert '" + TypeConstants.JSON_TNAME + "' to type '" + typeRefCPEntry.getType() + "': " + e.getMessage();
handleTypeConversionError(ctx, sf, j, errorMsg);
}
}
use of org.ballerinalang.model.types.BStructType 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;
}
Aggregations