Search in sources :

Example 1 with BStructType

use of org.ballerinalang.model.types.BStructType in project ballerina by ballerina-lang.

the class Util method getArrayOfBodyParts.

/**
 * From a given list of body parts get a ballerina value array.
 *
 * @param bodyParts List of body parts
 * @return BRefValueArray representing an array of entities
 */
static BRefValueArray getArrayOfBodyParts(ArrayList<BStruct> bodyParts) {
    BStructType typeOfBodyPart = bodyParts.get(0).getType();
    BStruct[] result = bodyParts.toArray(new BStruct[bodyParts.size()]);
    return new BRefValueArray(result, typeOfBodyPart);
}
Also used : BStructType(org.ballerinalang.model.types.BStructType) BStruct(org.ballerinalang.model.values.BStruct) BRefValueArray(org.ballerinalang.model.values.BRefValueArray)

Example 2 with BStructType

use of org.ballerinalang.model.types.BStructType in project ballerina by ballerina-lang.

the class SQLDataIterator method createUserDefinedType.

private BStruct createUserDefinedType(Struct structValue, BStructType structType) {
    if (structValue == null) {
        return null;
    }
    BStructType.StructField[] internalStructFields = structType.getStructFields();
    BStruct struct = new BStruct(structType);
    try {
        Object[] dataArray = structValue.getAttributes();
        if (dataArray != null) {
            if (dataArray.length != internalStructFields.length) {
                throw new BallerinaException("specified struct and returned struct are not compatible");
            }
            int longRegIndex = -1;
            int doubleRegIndex = -1;
            int stringRegIndex = -1;
            int booleanRegIndex = -1;
            int refRegIndex = -1;
            int index = 0;
            for (BStructType.StructField internalField : internalStructFields) {
                int type = internalField.getFieldType().getTag();
                Object value = dataArray[index];
                switch(type) {
                    case TypeTags.INT_TAG:
                        if (value instanceof BigDecimal) {
                            struct.setIntField(++longRegIndex, ((BigDecimal) value).intValue());
                        } else {
                            struct.setIntField(++longRegIndex, (long) value);
                        }
                        break;
                    case TypeTags.FLOAT_TAG:
                        if (value instanceof BigDecimal) {
                            struct.setFloatField(++doubleRegIndex, ((BigDecimal) value).doubleValue());
                        } else {
                            struct.setFloatField(++doubleRegIndex, (double) value);
                        }
                        break;
                    case TypeTags.STRING_TAG:
                        struct.setStringField(++stringRegIndex, (String) value);
                        break;
                    case TypeTags.BOOLEAN_TAG:
                        struct.setBooleanField(++booleanRegIndex, (int) value);
                        break;
                    case TypeTags.STRUCT_TAG:
                        struct.setRefField(++refRegIndex, createUserDefinedType((Struct) value, (BStructType) internalField.fieldType));
                        break;
                    default:
                        throw new BallerinaException("error in retrieving UDT data for unsupported type:" + type);
                }
                ++index;
            }
        }
    } catch (SQLException e) {
        throw new BallerinaException("error in retrieving UDT data:" + e.getMessage());
    }
    return struct;
}
Also used : BStructType(org.ballerinalang.model.types.BStructType) BStruct(org.ballerinalang.model.values.BStruct) SQLException(java.sql.SQLException) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException) BigDecimal(java.math.BigDecimal) Struct(java.sql.Struct) BStruct(org.ballerinalang.model.values.BStruct)

Example 3 with BStructType

use of org.ballerinalang.model.types.BStructType in project ballerina by ballerina-lang.

the class AbstractSQLAction method getStructType.

private BStructType getStructType(BStruct parameter) {
    BTypeDescValue type = (BTypeDescValue) parameter.getRefField(3);
    BStructType structType = null;
    if (type != null) {
        structType = (BStructType) type.value();
    }
    return structType;
}
Also used : BStructType(org.ballerinalang.model.types.BStructType) BTypeDescValue(org.ballerinalang.model.values.BTypeDescValue)

Example 4 with BStructType

use of org.ballerinalang.model.types.BStructType in project ballerina by ballerina-lang.

the class DefaultStreamObserver method generateRequestStruct.

private BValue generateRequestStruct(Message request, String fieldName, BType structType, Resource resource) {
    BValue bValue = null;
    int stringIndex = 0;
    int intIndex = 0;
    int floatIndex = 0;
    int boolIndex = 0;
    int refIndex = 0;
    if (structType instanceof BStructType) {
        BStruct requestStruct = BLangConnectorSPIUtil.createBStruct(MessageUtils.getProgramFile(resource), structType.getPackagePath(), structType.getName());
        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(), resource));
                }
            } 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 (fields.containsKey(fieldName)) {
            String fieldType = structType.getName();
            switch(fieldType) {
                case STRING:
                    {
                        bValue = new BString((String) fields.get(fieldName));
                        break;
                    }
                case INT:
                    {
                        bValue = new BInteger((Long) fields.get(fieldName));
                        break;
                    }
                case FLOAT:
                    {
                        Float value = (Float) fields.get(fieldName);
                        if (value != null) {
                            bValue = new BFloat(Double.parseDouble(value.toString()));
                        }
                        break;
                    }
                case DOUBLE:
                    {
                        Double value = (Double) fields.get(fieldName);
                        if (value != null) {
                            bValue = new BFloat(Double.parseDouble(value.toString()));
                        }
                        break;
                    }
                case BOOLEAN:
                    {
                        bValue = new BBoolean((Boolean) fields.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) Message(org.ballerinalang.net.grpc.Message) 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 5 with BStructType

use of org.ballerinalang.model.types.BStructType in project ballerina by ballerina-lang.

the class MethodListener method generateRequestStruct.

private BValue generateRequestStruct(Message request, ProgramFile programFile, String fieldName, BType structType) {
    BValue bValue = null;
    int stringIndex = 0;
    int intIndex = 0;
    int floatIndex = 0;
    int boolIndex = 0;
    int refIndex = 0;
    if (structType instanceof BStructType) {
        BStruct requestStruct = BLangConnectorSPIUtil.createBStruct(programFile, structType.getPackagePath(), structType.getName());
        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, programFile, structFieldName, structField.getFieldType()));
                }
            } 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 (fields.containsKey(fieldName)) {
            String fieldType = structType.getName();
            switch(fieldType) {
                case STRING:
                    {
                        bValue = new BString((String) fields.get(fieldName));
                        break;
                    }
                case INT:
                    {
                        bValue = new BInteger((Long) fields.get(fieldName));
                        break;
                    }
                case FLOAT:
                    {
                        Float value = (Float) fields.get(fieldName);
                        if (value != null) {
                            bValue = new BFloat(Double.parseDouble(value.toString()));
                        }
                        break;
                    }
                case DOUBLE:
                    {
                        Double value = (Double) fields.get(fieldName);
                        if (value != null) {
                            bValue = new BFloat(Double.parseDouble(value.toString()));
                        }
                        break;
                    }
                case BOOLEAN:
                    {
                        bValue = new BBoolean((Boolean) fields.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) Message(org.ballerinalang.net.grpc.Message) 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)

Aggregations

BStructType (org.ballerinalang.model.types.BStructType)40 BStruct (org.ballerinalang.model.values.BStruct)24 BType (org.ballerinalang.model.types.BType)13 BString (org.ballerinalang.model.values.BString)11 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)11 BRefValueArray (org.ballerinalang.model.values.BRefValueArray)7 BValue (org.ballerinalang.model.values.BValue)7 StructInfo (org.ballerinalang.util.codegen.StructInfo)7 BRefType (org.ballerinalang.model.values.BRefType)6 BBoolean (org.ballerinalang.model.values.BBoolean)5 BJSON (org.ballerinalang.model.values.BJSON)5 BTypeDescValue (org.ballerinalang.model.values.BTypeDescValue)5 Struct (java.sql.Struct)4 BFloat (org.ballerinalang.model.values.BFloat)4 BInteger (org.ballerinalang.model.values.BInteger)4 PackageInfo (org.ballerinalang.util.codegen.PackageInfo)4 BigDecimal (java.math.BigDecimal)3 SQLException (java.sql.SQLException)3 BMapType (org.ballerinalang.model.types.BMapType)3 BMap (org.ballerinalang.model.values.BMap)3