use of org.ballerinalang.model.types.BType in project ballerina by ballerina-lang.
the class BLangVMUtils method populateWorkerResultWithValues.
@SuppressWarnings("rawtypes")
public static void populateWorkerResultWithValues(WorkerData result, BValue[] vals, BType[] types) {
if (vals == null) {
return;
}
int longRegCount = 0;
int doubleRegCount = 0;
int stringRegCount = 0;
int intRegCount = 0;
int refRegCount = 0;
int byteRegCount = 0;
for (int i = 0; i < vals.length; i++) {
BType retType = types[i];
switch(retType.getTag()) {
case TypeTags.INT_TAG:
if (vals[i] == null) {
result.longRegs[longRegCount++] = 0;
break;
}
result.longRegs[longRegCount++] = ((BInteger) vals[i]).intValue();
break;
case TypeTags.FLOAT_TAG:
if (vals[i] == null) {
result.doubleRegs[doubleRegCount++] = 0;
break;
}
result.doubleRegs[doubleRegCount++] = ((BFloat) vals[i]).floatValue();
break;
case TypeTags.STRING_TAG:
if (vals[i] == null) {
result.stringRegs[stringRegCount++] = BLangConstants.STRING_NULL_VALUE;
break;
}
result.stringRegs[stringRegCount++] = vals[i].stringValue();
break;
case TypeTags.BOOLEAN_TAG:
if (vals[i] == null) {
result.intRegs[intRegCount++] = 0;
break;
}
result.intRegs[intRegCount++] = ((BBoolean) vals[i]).booleanValue() ? 1 : 0;
break;
case TypeTags.BLOB_TAG:
if (vals[i] == null) {
result.byteRegs[byteRegCount++] = new byte[0];
break;
}
result.byteRegs[byteRegCount++] = ((BBlob) vals[i]).blobValue();
break;
default:
result.refRegs[refRegCount++] = (BRefType) vals[i];
}
}
}
use of org.ballerinalang.model.types.BType in project ballerina by ballerina-lang.
the class GetCRC32 method execute.
@Override
public void execute(Context context) {
BValue entityBody = context.getRefArgument(0);
Checksum checksum = new CRC32();
byte[] bytes;
long checksumVal;
BType argType = entityBody.getType();
if (argType == BTypes.typeJSON || argType == BTypes.typeXML || argType == BTypes.typeString) {
// TODO: Look at the possibility of making the encoding configurable
bytes = entityBody.stringValue().getBytes(StandardCharsets.UTF_8);
} else if (argType == BTypes.typeBlob) {
bytes = ((BBlob) entityBody).blobValue();
} else {
throw new BallerinaException("failed to generate hash: unsupported data type: " + entityBody.getType().getName());
}
checksum.update(bytes, 0, bytes.length);
checksumVal = checksum.getValue();
context.setReturnValues(new BString(Long.toHexString(checksumVal)));
}
use of org.ballerinalang.model.types.BType in project ballerina by ballerina-lang.
the class CPU method handleVariableUnlock.
private static void handleVariableUnlock(WorkerExecutionContext ctx, BType[] types, int[] varRegs) {
for (int i = varRegs.length - 1; i > -1; i--) {
BType paramType = types[i];
int regIndex = varRegs[i];
switch(paramType.getTag()) {
case TypeTags.INT_TAG:
ctx.programFile.getGlobalMemoryBlock().unlockIntField(regIndex);
break;
case TypeTags.FLOAT_TAG:
ctx.programFile.getGlobalMemoryBlock().unlockFloatField(regIndex);
break;
case TypeTags.STRING_TAG:
ctx.programFile.getGlobalMemoryBlock().unlockStringField(regIndex);
break;
case TypeTags.BOOLEAN_TAG:
ctx.programFile.getGlobalMemoryBlock().unlockBooleanField(regIndex);
break;
case TypeTags.BLOB_TAG:
ctx.programFile.getGlobalMemoryBlock().unlockBlobField(regIndex);
break;
default:
ctx.programFile.getGlobalMemoryBlock().unlockRefField(regIndex);
}
}
}
use of org.ballerinalang.model.types.BType in project ballerina by ballerina-lang.
the class StreamingRuntimeManager method addCallback.
public void addCallback(String streamId, BFunctionPointer functionPointer, SiddhiAppRuntime siddhiAppRuntime) {
BType[] parameters = functionPointer.value().getFunctionInfo().getParamTypes();
BStructType structType = (BStructType) ((BArrayType) parameters[0]).getElementType();
if (!(parameters[0] instanceof BArrayType)) {
throw new BallerinaException("incompatible function: inline function needs to be a function accepting" + " a struct array");
}
siddhiAppRuntime.addCallback(streamId, new StreamCallback() {
@Override
public void receive(Event[] events) {
for (Event event : events) {
AtomicInteger intVarIndex = new AtomicInteger(-1);
AtomicInteger floatVarIndex = new AtomicInteger(-1);
AtomicInteger boolVarIndex = new AtomicInteger(-1);
AtomicInteger stringVarIndex = new AtomicInteger(-1);
BStruct output = new BStruct(structType);
for (Object field : event.getData()) {
if (field instanceof Long) {
output.setIntField(intVarIndex.incrementAndGet(), (Long) field);
} else if (field instanceof Double) {
output.setFloatField(floatVarIndex.incrementAndGet(), (Double) field);
} else if (field instanceof Boolean) {
output.setBooleanField(boolVarIndex.incrementAndGet(), (Integer) field);
} else if (field instanceof String) {
output.setStringField(stringVarIndex.incrementAndGet(), (String) field);
}
}
BValue[] args = { output };
BLangFunctions.invokeCallable(functionPointer.value().getFunctionInfo(), args);
}
}
});
}
use of org.ballerinalang.model.types.BType in project ballerina by ballerina-lang.
the class TableJSONDataSource method getStructData.
private static JsonNode getStructData(Object[] data, BStructType.StructField[] structFields, int index) {
JsonNode jsonData = null;
try {
if (structFields == null) {
jsonData = new JsonNode(Type.ARRAY);
if (data != null) {
for (Object value : data) {
if (value instanceof String) {
jsonData.add((String) value);
} else if (value instanceof Boolean) {
jsonData.add((Boolean) value);
} else if (value instanceof Long) {
jsonData.add((long) value);
} else if (value instanceof Double) {
jsonData.add((double) value);
} else if (value instanceof Integer) {
jsonData.add((int) value);
} else if (value instanceof Float) {
jsonData.add((float) value);
} else if (value instanceof BigDecimal) {
jsonData.add(((BigDecimal) value).doubleValue());
}
}
}
} else {
jsonData = new JsonNode(Type.OBJECT);
boolean structError = true;
if (data != null) {
int i = 0;
for (Object value : data) {
BType internaltType = structFields[index - 1].fieldType;
if (internaltType.getTag() == TypeTags.STRUCT_TAG) {
BStructType.StructField[] interanlStructFields = ((BStructType) internaltType).getStructFields();
if (interanlStructFields != null) {
if (value instanceof String) {
jsonData.set(interanlStructFields[i].fieldName, (String) value);
} else if (value instanceof Boolean) {
jsonData.set(interanlStructFields[i].fieldName, (Boolean) value);
} else if (value instanceof Long) {
jsonData.set(interanlStructFields[i].fieldName, (long) value);
} else if (value instanceof Double) {
jsonData.set(interanlStructFields[i].fieldName, (double) value);
} else if (value instanceof Integer) {
jsonData.set(interanlStructFields[i].fieldName, (int) value);
} else if (value instanceof Float) {
jsonData.set(interanlStructFields[i].fieldName, (float) value);
} else if (value instanceof BigDecimal) {
jsonData.set(interanlStructFields[i].fieldName, ((BigDecimal) value).doubleValue());
} else if (value instanceof Struct) {
jsonData.set(interanlStructFields[i].fieldName, getStructData(((Struct) value).getAttributes(), interanlStructFields, i + 1));
}
structError = false;
}
}
++i;
}
}
if (structError) {
throw new BallerinaException("error in constructing the json object from struct type data");
}
}
} catch (SQLException e) {
throw new BallerinaException("error in retrieving struct data to construct the inner json object:" + e.getMessage());
}
return jsonData;
}
Aggregations