Search in sources :

Example 81 with BFloat

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;
}
Also used : BRefType(org.ballerinalang.model.values.BRefType) BStruct(org.ballerinalang.model.values.BStruct) BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) BInteger(org.ballerinalang.model.values.BInteger) BBoolean(org.ballerinalang.model.values.BBoolean) UnsupportedFieldTypeException(org.ballerinalang.net.grpc.exception.UnsupportedFieldTypeException) BString(org.ballerinalang.model.values.BString) BStructType(org.ballerinalang.model.types.BStructType) BInteger(org.ballerinalang.model.values.BInteger) BFloat(org.ballerinalang.model.values.BFloat) BType(org.ballerinalang.model.types.BType) BFloat(org.ballerinalang.model.values.BFloat)

Example 82 with BFloat

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);
}
Also used : BValue(org.ballerinalang.model.values.BValue) BFloat(org.ballerinalang.model.values.BFloat) CompileResult(org.ballerinalang.launcher.util.CompileResult) Test(org.testng.annotations.Test)

Example 83 with BFloat

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;
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) BNewArray(org.ballerinalang.model.values.BNewArray) BMap(org.ballerinalang.model.values.BMap) BValue(org.ballerinalang.model.values.BValue) BFloat(org.ballerinalang.model.values.BFloat) BString(org.ballerinalang.model.values.BString) BJSON(org.ballerinalang.model.values.BJSON) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException)

Example 84 with BFloat

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;
}
Also used : BreakPointDTO(org.ballerinalang.util.debugger.dto.BreakPointDTO) BString(org.ballerinalang.model.values.BString) BInteger(org.ballerinalang.model.values.BInteger) BBoolean(org.ballerinalang.model.values.BBoolean) BString(org.ballerinalang.model.values.BString) MessageDTO(org.ballerinalang.util.debugger.dto.MessageDTO) LocalVariableAttributeInfo(org.ballerinalang.util.codegen.attributes.LocalVariableAttributeInfo) VariableDTO(org.ballerinalang.util.debugger.dto.VariableDTO) BFloat(org.ballerinalang.model.values.BFloat) LineNumberInfo(org.ballerinalang.util.codegen.LineNumberInfo) BBlob(org.ballerinalang.model.values.BBlob) FrameDTO(org.ballerinalang.util.debugger.dto.FrameDTO)

Example 85 with BFloat

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);
}
Also used : BValue(org.ballerinalang.model.values.BValue) BFloat(org.ballerinalang.model.values.BFloat) Test(org.testng.annotations.Test)

Aggregations

BFloat (org.ballerinalang.model.values.BFloat)158 BValue (org.ballerinalang.model.values.BValue)119 Test (org.testng.annotations.Test)107 BInteger (org.ballerinalang.model.values.BInteger)55 BString (org.ballerinalang.model.values.BString)41 BBoolean (org.ballerinalang.model.values.BBoolean)21 BRefType (org.ballerinalang.model.values.BRefType)7 BStruct (org.ballerinalang.model.values.BStruct)7 BType (org.ballerinalang.model.types.BType)6 BBlob (org.ballerinalang.model.values.BBlob)6 BStringArray (org.ballerinalang.model.values.BStringArray)5 BStructType (org.ballerinalang.model.types.BStructType)4 BIntArray (org.ballerinalang.model.values.BIntArray)4 UnsupportedFieldTypeException (org.ballerinalang.net.grpc.exception.UnsupportedFieldTypeException)4 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)4 BJSON (org.ballerinalang.model.values.BJSON)3 CompileResult (org.ballerinalang.launcher.util.CompileResult)2 BMap (org.ballerinalang.model.values.BMap)2 BRefValueArray (org.ballerinalang.model.values.BRefValueArray)2 Message (org.ballerinalang.net.grpc.Message)2