use of org.ballerinalang.model.values.BFloat in project ballerina by ballerina-lang.
the class MessageUtils method generateRequestStruct.
public static BValue generateRequestStruct(Message request, String fieldName, BType structType, Context context) {
BValue bValue = null;
int stringIndex = 0;
int intIndex = 0;
int floatIndex = 0;
int boolIndex = 0;
int refIndex = 0;
if (structType instanceof BStructType) {
BStruct requestStruct = createStruct(context, fieldName);
for (BStructType.StructField structField : ((BStructType) structType).getStructFields()) {
String structFieldName = structField.getFieldName();
if (structField.getFieldType() instanceof BRefType) {
BType bType = structField.getFieldType();
if (MessageRegistry.getInstance().getMessageDescriptorMap().containsKey(bType.getName())) {
Message message = (Message) request.getFields().get(structFieldName);
requestStruct.setRefField(refIndex++, (BRefType) generateRequestStruct(message, structFieldName, structField.getFieldType(), context));
}
} else {
if (request.getFields().containsKey(structFieldName)) {
String fieldType = structField.getFieldType().getName();
switch(fieldType) {
case STRING:
{
requestStruct.setStringField(stringIndex++, (String) request.getFields().get(structFieldName));
break;
}
case INT:
{
requestStruct.setIntField(intIndex++, (Long) request.getFields().get(structFieldName));
break;
}
case FLOAT:
{
Float value = (Float) request.getFields().get(structFieldName);
if (value != null) {
requestStruct.setFloatField(floatIndex++, Double.parseDouble(value.toString()));
}
break;
}
case DOUBLE:
{
Double value = (Double) request.getFields().get(structFieldName);
if (value != null) {
requestStruct.setFloatField(floatIndex++, Double.parseDouble(value.toString()));
}
break;
}
case BOOLEAN:
{
requestStruct.setBooleanField(boolIndex++, (Integer) request.getFields().get(structFieldName));
break;
}
default:
{
throw new UnsupportedFieldTypeException("Error while generating request struct. Field" + " type is not supported : " + fieldType);
}
}
}
}
}
bValue = requestStruct;
} else {
Map<String, Object> fields = request.getFields();
if (fields.size() == 1 && fields.containsKey("value")) {
fieldName = "value";
}
if (request.getFields().containsKey(fieldName)) {
String fieldType = structType.getName();
switch(fieldType) {
case STRING:
{
bValue = new BString((String) request.getFields().get(fieldName));
break;
}
case INT:
{
bValue = new BInteger((Long) request.getFields().get(fieldName));
break;
}
case FLOAT:
{
Float value = (Float) request.getFields().get(fieldName);
if (value != null) {
bValue = new BFloat(Double.parseDouble(value.toString()));
}
break;
}
case DOUBLE:
{
Double value = (Double) request.getFields().get(fieldName);
if (value != null) {
bValue = new BFloat(Double.parseDouble(value.toString()));
}
break;
}
case BOOLEAN:
{
bValue = new BBoolean((Boolean) request.getFields().get(fieldName));
break;
}
default:
{
throw new UnsupportedFieldTypeException("Error while generating request struct. Field " + "type is not supported : " + fieldType);
}
}
}
}
return bValue;
}
use of org.ballerinalang.model.values.BFloat in project ballerina by ballerina-lang.
the class ForkJoinInFunctionTest method testForkJoinWithoutTimeoutExpression.
@Test(description = "Test Fork Join Without Timeout Expression")
public void testForkJoinWithoutTimeoutExpression() {
CompileResult result = BCompileUtil.compile("test-src/workers/fork-join-without-timeout.bal");
BValue[] returns = BRunUtil.invoke(result, "testForkJoinWithoutTimeoutExpression");
Assert.assertEquals(returns.length, 2);
Assert.assertEquals(((BInteger) returns[0]).intValue(), 100);
Assert.assertTrue(returns[1] instanceof BFloat);
Assert.assertEquals(((BFloat) returns[1]).floatValue(), 1.23);
}
use of org.ballerinalang.model.values.BFloat in project ballerina by ballerina-lang.
the class JSONUtils method convertMapToJSON.
/**
* Convert {@link BMap} to {@link BJSON}.
*
* @param map {@link BMap} to be converted to {@link BJSON}
* @return JSON representation of the provided map
*/
@SuppressWarnings("unchecked")
public static BJSON convertMapToJSON(BMap<String, BValue> map) {
Set<String> keys = map.keySet();
BJSON bjson = new BJSON(new JsonNode(Type.OBJECT));
JsonNode jsonNode = bjson.value();
for (String key : keys) {
try {
BValue bvalue = map.get(key);
if (bvalue == null) {
jsonNode.set(key, new BJSON(NULL).value());
} else if (bvalue.getType() == BTypes.typeString) {
jsonNode.set(key, bvalue.stringValue());
} else if (bvalue.getType() == BTypes.typeInt) {
jsonNode.set(key, ((BInteger) bvalue).intValue());
} else if (bvalue.getType() == BTypes.typeFloat) {
jsonNode.set(key, ((BFloat) bvalue).floatValue());
} else if (bvalue.getType() == BTypes.typeBoolean) {
jsonNode.set(key, ((BBoolean) bvalue).booleanValue());
} else if (bvalue.getType().getTag() == TypeTags.MAP_TAG) {
jsonNode.set(key, convertMapToJSON((BMap<String, BValue>) bvalue).value());
} else if (bvalue.getType() == BTypes.typeJSON) {
jsonNode.set(key, ((BJSON) bvalue).value());
} else if (bvalue instanceof BNewArray) {
jsonNode.set(key, convertArrayToJSON((BNewArray) bvalue).value());
} else if (bvalue instanceof BStruct) {
jsonNode.set(key, convertStructToJSON((BStruct) bvalue).value());
} else {
throw BLangExceptionHelper.getRuntimeException(RuntimeErrors.INCOMPATIBLE_TYPE_FOR_CASTING, BTypes.typeJSON, bvalue.getType());
}
} catch (Exception e) {
handleError(e, key);
}
}
return bjson;
}
use of org.ballerinalang.model.values.BFloat in project ballerina by ballerina-lang.
the class Debugger method generateDebugHitMessage.
/**
* Generate debug hit message.
*
* @param ctx Current context.
* @param currentExecLine Current execution line.
* @param workerId Current thread id.
* @return message To be sent.
*/
private MessageDTO generateDebugHitMessage(WorkerExecutionContext ctx, LineNumberInfo currentExecLine, String workerId) {
MessageDTO message = new MessageDTO(DebugConstants.CODE_HIT, DebugConstants.MSG_HIT);
message.setThreadId(workerId);
BreakPointDTO breakPointDTO = new BreakPointDTO(currentExecLine.getPackageInfo().getPkgPath(), currentExecLine.getFileName(), currentExecLine.getLineNumber());
message.setLocation(breakPointDTO);
int callingIp = currentExecLine.getIp();
String pck = ctx.callableUnitInfo.getPackageInfo().getPkgPath();
String functionName = ctx.callableUnitInfo.getName();
LineNumberInfo callingLine = getLineNumber(ctx.callableUnitInfo.getPackageInfo().getPkgPath(), callingIp);
FrameDTO frameDTO = new FrameDTO(pck, functionName, callingLine.getFileName(), callingLine.getLineNumber());
message.addFrame(frameDTO);
LocalVariableAttributeInfo localVarAttrInfo = (LocalVariableAttributeInfo) ctx.workerInfo.getAttributeInfo(AttributeInfo.Kind.LOCAL_VARIABLES_ATTRIBUTE);
localVarAttrInfo.getLocalVariables().forEach(l -> {
VariableDTO variableDTO = new VariableDTO(l.getVariableName(), "Local");
switch(l.getVariableType().getTag()) {
case TypeTags.INT_TAG:
variableDTO.setBValue(new BInteger(ctx.workerLocal.longRegs[l.getVariableIndex()]));
break;
case TypeTags.FLOAT_TAG:
variableDTO.setBValue(new BFloat(ctx.workerLocal.doubleRegs[l.getVariableIndex()]));
break;
case TypeTags.STRING_TAG:
variableDTO.setBValue(new BString(ctx.workerLocal.stringRegs[l.getVariableIndex()]));
break;
case TypeTags.BOOLEAN_TAG:
variableDTO.setBValue(new BBoolean(ctx.workerLocal.intRegs[l.getVariableIndex()] == 1));
break;
case TypeTags.BLOB_TAG:
variableDTO.setBValue(new BBlob(ctx.workerLocal.byteRegs[l.getVariableIndex()]));
break;
default:
variableDTO.setBValue(ctx.workerLocal.refRegs[l.getVariableIndex()]);
break;
}
frameDTO.addVariable(variableDTO);
});
return message;
}
use of org.ballerinalang.model.values.BFloat in project ballerina by ballerina-lang.
the class AssertTest method testAssertFloatEqualsFail.
@Test(expectedExceptions = BLangRuntimeException.class, expectedExceptionsMessageRegExp = ".*expected 30.05 but found 21.05.*")
public void testAssertFloatEqualsFail() {
BValue[] args = { new BFloat(1), new BFloat(20.050) };
BTestUtils.invoke(compileResult, "testAssertFloatEquals", args);
}
Aggregations