Search in sources :

Example 1 with UnsupportedFieldTypeException

use of org.ballerinalang.net.grpc.exception.UnsupportedFieldTypeException in project ballerina by ballerina-lang.

the class MessageUtils method generateProtoMessage.

/**
 * Returns protobuf message corresponding to the B7a message.
 *
 * @param responseValue B7a message.
 * @param outputType protobuf message type.
 * @return generated protobuf message.
 */
public static Message generateProtoMessage(BValue responseValue, Descriptors.Descriptor outputType) {
    Message.Builder responseBuilder = Message.newBuilder(outputType.getName());
    int stringIndex = 0;
    int intIndex = 0;
    int floatIndex = 0;
    int boolIndex = 0;
    int refIndex = 0;
    for (Descriptors.FieldDescriptor fieldDescriptor : outputType.getFields()) {
        String fieldName = fieldDescriptor.getName();
        switch(fieldDescriptor.getType().toProto().getNumber()) {
            case DescriptorProtos.FieldDescriptorProto.Type.TYPE_DOUBLE_VALUE:
                {
                    double value = 0F;
                    if (responseValue instanceof BStruct) {
                        value = ((BStruct) responseValue).getFloatField(floatIndex++);
                    } else {
                        if (responseValue instanceof BFloat) {
                            value = ((BFloat) responseValue).value();
                        }
                    }
                    responseBuilder.addField(fieldName, value);
                    break;
                }
            case DescriptorProtos.FieldDescriptorProto.Type.TYPE_FLOAT_VALUE:
                {
                    float value = 0F;
                    if (responseValue instanceof BStruct) {
                        value = Float.parseFloat(String.valueOf(((BStruct) responseValue).getFloatField(floatIndex++)));
                    } else {
                        if (responseValue instanceof BFloat) {
                            value = Float.parseFloat(String.valueOf(((BFloat) responseValue).value()));
                        }
                    }
                    responseBuilder.addField(fieldName, value);
                    break;
                }
            case DescriptorProtos.FieldDescriptorProto.Type.TYPE_INT64_VALUE:
            case DescriptorProtos.FieldDescriptorProto.Type.TYPE_UINT64_VALUE:
            case DescriptorProtos.FieldDescriptorProto.Type.TYPE_FIXED64_VALUE:
                {
                    long value = 0;
                    if (responseValue instanceof BStruct) {
                        value = ((BStruct) responseValue).getIntField(intIndex++);
                    } else {
                        if (responseValue instanceof BInteger) {
                            value = ((BInteger) responseValue).value();
                        }
                    }
                    responseBuilder.addField(fieldName, value);
                    break;
                }
            case DescriptorProtos.FieldDescriptorProto.Type.TYPE_INT32_VALUE:
            case DescriptorProtos.FieldDescriptorProto.Type.TYPE_FIXED32_VALUE:
                {
                    int value = 0;
                    if (responseValue instanceof BStruct) {
                        value = Integer.parseInt(String.valueOf(((BStruct) responseValue).getIntField(intIndex++)));
                    } else {
                        if (responseValue instanceof BInteger) {
                            value = Integer.parseInt(String.valueOf(((BInteger) responseValue).value()));
                        }
                    }
                    responseBuilder.addField(fieldName, value);
                    break;
                }
            case DescriptorProtos.FieldDescriptorProto.Type.TYPE_BOOL_VALUE:
                {
                    boolean value = false;
                    if (responseValue instanceof BStruct) {
                        value = ((BStruct) responseValue).getBooleanField(boolIndex++) > 0;
                    } else {
                        if (responseValue instanceof BBoolean) {
                            value = ((BBoolean) responseValue).value();
                        }
                    }
                    responseBuilder.addField(fieldName, value);
                    break;
                }
            case DescriptorProtos.FieldDescriptorProto.Type.TYPE_STRING_VALUE:
                {
                    String value = null;
                    if (responseValue instanceof BStruct) {
                        value = ((BStruct) responseValue).getStringField(stringIndex++);
                    } else {
                        if (responseValue instanceof BString) {
                            value = ((BString) responseValue).value();
                        }
                    }
                    responseBuilder.addField(fieldName, value);
                    break;
                }
            case DescriptorProtos.FieldDescriptorProto.Type.TYPE_MESSAGE_VALUE:
                {
                    if (responseValue instanceof BStruct) {
                        BValue bValue = ((BStruct) responseValue).getRefField(refIndex++);
                        responseBuilder.addField(fieldName, generateProtoMessage(bValue, fieldDescriptor.getMessageType()));
                    }
                    break;
                }
            default:
                {
                    throw new UnsupportedFieldTypeException("Error while decoding request message. Field " + "type is not supported : " + fieldDescriptor.getType());
                }
        }
    }
    return responseBuilder.build();
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) BString(org.ballerinalang.model.values.BString) BValue(org.ballerinalang.model.values.BValue) 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) BFloat(org.ballerinalang.model.values.BFloat) Descriptors(com.google.protobuf.Descriptors)

Example 2 with UnsupportedFieldTypeException

use of org.ballerinalang.net.grpc.exception.UnsupportedFieldTypeException in project ballerina by ballerina-lang.

the class Message method writeTo.

public void writeTo(com.google.protobuf.CodedOutputStream output) throws java.io.IOException {
    Descriptors.Descriptor messageDescriptor = getDescriptor();
    for (Descriptors.FieldDescriptor fieldDescriptor : messageDescriptor.getFields()) {
        if (fields.containsKey(fieldDescriptor.getName())) {
            switch(fieldDescriptor.getType().toProto().getNumber()) {
                case DescriptorProtos.FieldDescriptorProto.Type.TYPE_DOUBLE_VALUE:
                    {
                        Object msgObject = fields.get(fieldDescriptor.getName());
                        if (MessageUtils.isArray(msgObject)) {
                            Double[] messages = (Double[]) msgObject;
                            for (Double message : messages) {
                                output.writeDouble(fieldDescriptor.getNumber(), message);
                            }
                        } else {
                            output.writeDouble(fieldDescriptor.getNumber(), (Double) msgObject);
                        }
                        break;
                    }
                case DescriptorProtos.FieldDescriptorProto.Type.TYPE_FLOAT_VALUE:
                    {
                        Object msgObject = fields.get(fieldDescriptor.getName());
                        if (MessageUtils.isArray(msgObject)) {
                            Float[] messages = (Float[]) msgObject;
                            for (Float message : messages) {
                                output.writeFloat(fieldDescriptor.getNumber(), message);
                            }
                        } else {
                            output.writeFloat(fieldDescriptor.getNumber(), (Float) msgObject);
                        }
                        break;
                    }
                case DescriptorProtos.FieldDescriptorProto.Type.TYPE_INT64_VALUE:
                    {
                        Object msgObject = fields.get(fieldDescriptor.getName());
                        if (MessageUtils.isArray(msgObject)) {
                            Long[] messages = (Long[]) msgObject;
                            for (Long message : messages) {
                                output.writeInt64(fieldDescriptor.getNumber(), message);
                            }
                        } else {
                            output.writeInt64(fieldDescriptor.getNumber(), (Long) msgObject);
                        }
                        break;
                    }
                case DescriptorProtos.FieldDescriptorProto.Type.TYPE_UINT64_VALUE:
                    {
                        Object msgObject = fields.get(fieldDescriptor.getName());
                        if (MessageUtils.isArray(msgObject)) {
                            Long[] messages = (Long[]) msgObject;
                            for (Long message : messages) {
                                output.writeUInt64(fieldDescriptor.getNumber(), message);
                            }
                        } else {
                            output.writeUInt64(fieldDescriptor.getNumber(), (Long) msgObject);
                        }
                        break;
                    }
                case DescriptorProtos.FieldDescriptorProto.Type.TYPE_INT32_VALUE:
                    {
                        Object msgObject = fields.get(fieldDescriptor.getName());
                        if (MessageUtils.isArray(msgObject)) {
                            Integer[] messages = (Integer[]) msgObject;
                            for (Integer message : messages) {
                                output.writeInt32(fieldDescriptor.getNumber(), message);
                            }
                        } else {
                            output.writeInt32(fieldDescriptor.getNumber(), (Integer) msgObject);
                        }
                        break;
                    }
                case DescriptorProtos.FieldDescriptorProto.Type.TYPE_FIXED64_VALUE:
                    {
                        Object msgObject = fields.get(fieldDescriptor.getName());
                        if (MessageUtils.isArray(msgObject)) {
                            Long[] messages = (Long[]) msgObject;
                            for (Long message : messages) {
                                output.writeFixed64(fieldDescriptor.getNumber(), message);
                            }
                        } else {
                            output.writeFixed64(fieldDescriptor.getNumber(), (Long) msgObject);
                        }
                        break;
                    }
                case DescriptorProtos.FieldDescriptorProto.Type.TYPE_FIXED32_VALUE:
                    {
                        Object msgObject = fields.get(fieldDescriptor.getName());
                        if (MessageUtils.isArray(msgObject)) {
                            Integer[] messages = (Integer[]) msgObject;
                            for (Integer message : messages) {
                                output.writeFixed32(fieldDescriptor.getNumber(), message);
                            }
                        } else {
                            output.writeFixed32(fieldDescriptor.getNumber(), (Integer) msgObject);
                        }
                        break;
                    }
                case DescriptorProtos.FieldDescriptorProto.Type.TYPE_BOOL_VALUE:
                    {
                        Object msgObject = fields.get(fieldDescriptor.getName());
                        if (MessageUtils.isArray(msgObject)) {
                            Boolean[] messages = (Boolean[]) msgObject;
                            for (Boolean message : messages) {
                                output.writeBool(fieldDescriptor.getNumber(), message);
                            }
                        } else {
                            output.writeBool(fieldDescriptor.getNumber(), (Boolean) msgObject);
                        }
                        break;
                    }
                case DescriptorProtos.FieldDescriptorProto.Type.TYPE_STRING_VALUE:
                    {
                        Object msgObject = fields.get(fieldDescriptor.getName());
                        if (MessageUtils.isArray(msgObject)) {
                            String[] messages = (String[]) msgObject;
                            for (String message : messages) {
                                output.writeString(fieldDescriptor.getNumber(), message);
                            }
                        } else {
                            com.google.protobuf.GeneratedMessageV3.writeString(output, fieldDescriptor.getNumber(), msgObject);
                        }
                        break;
                    }
                case DescriptorProtos.FieldDescriptorProto.Type.TYPE_MESSAGE_VALUE:
                    {
                        Object msgObject = fields.get(fieldDescriptor.getName());
                        if (MessageUtils.isArray(msgObject)) {
                            Object[] messages = (Object[]) msgObject;
                            for (Object message : messages) {
                                output.writeMessage(fieldDescriptor.getNumber(), (MessageLite) message);
                            }
                        } else {
                            output.writeMessage(fieldDescriptor.getNumber(), (MessageLite) msgObject);
                        }
                        break;
                    }
                default:
                    {
                        throw new UnsupportedFieldTypeException("Error while writing output stream. Field " + "type is not supported : " + fieldDescriptor.getType());
                    }
            }
        }
    }
    unknownFields.writeTo(output);
}
Also used : UnsupportedFieldTypeException(org.ballerinalang.net.grpc.exception.UnsupportedFieldTypeException) Descriptors(com.google.protobuf.Descriptors) MessageLite(com.google.protobuf.MessageLite)

Example 3 with UnsupportedFieldTypeException

use of org.ballerinalang.net.grpc.exception.UnsupportedFieldTypeException in project ballerina by ballerina-lang.

the class Message method getSerializedSize.

public int getSerializedSize() {
    int size = memoizedSize;
    if (size != -1) {
        return size;
    }
    size = 0;
    Descriptors.Descriptor messageDescriptor = getDescriptor();
    for (Descriptors.FieldDescriptor fieldDescriptor : messageDescriptor.getFields()) {
        if (fields.containsKey(fieldDescriptor.getName())) {
            switch(fieldDescriptor.getType().toProto().getNumber()) {
                case DescriptorProtos.FieldDescriptorProto.Type.TYPE_DOUBLE_VALUE:
                    {
                        Object msgObject = fields.get(fieldDescriptor.getName());
                        if (MessageUtils.isArray(msgObject)) {
                            Double[] messages = (Double[]) msgObject;
                            for (Double message : messages) {
                                size += com.google.protobuf.CodedOutputStream.computeDoubleSize(fieldDescriptor.getNumber(), message);
                            }
                        } else {
                            size += com.google.protobuf.CodedOutputStream.computeDoubleSize(fieldDescriptor.getNumber(), (Double) fields.get(fieldDescriptor.getName()));
                        }
                        break;
                    }
                case DescriptorProtos.FieldDescriptorProto.Type.TYPE_FLOAT_VALUE:
                    {
                        Object msgObject = fields.get(fieldDescriptor.getName());
                        if (MessageUtils.isArray(msgObject)) {
                            Float[] messages = (Float[]) msgObject;
                            for (Float message : messages) {
                                size += com.google.protobuf.CodedOutputStream.computeFloatSize(fieldDescriptor.getNumber(), message);
                            }
                        } else {
                            size += com.google.protobuf.CodedOutputStream.computeFloatSize(fieldDescriptor.getNumber(), Float.parseFloat(fields.get(fieldDescriptor.getName()).toString()));
                        }
                        break;
                    }
                case DescriptorProtos.FieldDescriptorProto.Type.TYPE_INT64_VALUE:
                    {
                        Object msgObject = fields.get(fieldDescriptor.getName());
                        if (MessageUtils.isArray(msgObject)) {
                            Long[] messages = (Long[]) msgObject;
                            for (Long message : messages) {
                                size += com.google.protobuf.CodedOutputStream.computeInt64Size(fieldDescriptor.getNumber(), message);
                            }
                        } else {
                            size += com.google.protobuf.CodedOutputStream.computeInt64Size(fieldDescriptor.getNumber(), (Long) fields.get(fieldDescriptor.getName()));
                        }
                        break;
                    }
                case DescriptorProtos.FieldDescriptorProto.Type.TYPE_UINT64_VALUE:
                    {
                        Object msgObject = fields.get(fieldDescriptor.getName());
                        if (MessageUtils.isArray(msgObject)) {
                            Long[] messages = (Long[]) msgObject;
                            for (Long message : messages) {
                                size += com.google.protobuf.CodedOutputStream.computeUInt64Size(fieldDescriptor.getNumber(), message);
                            }
                        } else {
                            size += com.google.protobuf.CodedOutputStream.computeUInt64Size(fieldDescriptor.getNumber(), (Long) fields.get(fieldDescriptor.getName()));
                        }
                        break;
                    }
                case DescriptorProtos.FieldDescriptorProto.Type.TYPE_INT32_VALUE:
                    {
                        Object msgObject = fields.get(fieldDescriptor.getName());
                        if (MessageUtils.isArray(msgObject)) {
                            Integer[] messages = (Integer[]) msgObject;
                            for (Integer message : messages) {
                                size += com.google.protobuf.CodedOutputStream.computeInt32Size(fieldDescriptor.getNumber(), message);
                            }
                        } else {
                            size += com.google.protobuf.CodedOutputStream.computeInt32Size(fieldDescriptor.getNumber(), Integer.parseInt(fields.get(fieldDescriptor.getName()).toString()));
                        }
                        break;
                    }
                case DescriptorProtos.FieldDescriptorProto.Type.TYPE_FIXED64_VALUE:
                    {
                        Object msgObject = fields.get(fieldDescriptor.getName());
                        if (MessageUtils.isArray(msgObject)) {
                            Long[] messages = (Long[]) msgObject;
                            for (Long message : messages) {
                                size += com.google.protobuf.CodedOutputStream.computeFixed64Size(fieldDescriptor.getNumber(), message);
                            }
                        } else {
                            size += com.google.protobuf.CodedOutputStream.computeFixed64Size(fieldDescriptor.getNumber(), (Long) fields.get(fieldDescriptor.getName()));
                        }
                        break;
                    }
                case DescriptorProtos.FieldDescriptorProto.Type.TYPE_FIXED32_VALUE:
                    {
                        Object msgObject = fields.get(fieldDescriptor.getName());
                        if (MessageUtils.isArray(msgObject)) {
                            Integer[] messages = (Integer[]) msgObject;
                            for (Integer message : messages) {
                                size += com.google.protobuf.CodedOutputStream.computeFixed32Size(fieldDescriptor.getNumber(), message);
                            }
                        } else {
                            size += com.google.protobuf.CodedOutputStream.computeFixed32Size(fieldDescriptor.getNumber(), Integer.parseInt(fields.get(fieldDescriptor.getName()).toString()));
                        }
                        break;
                    }
                case DescriptorProtos.FieldDescriptorProto.Type.TYPE_BOOL_VALUE:
                    {
                        Object msgObject = fields.get(fieldDescriptor.getName());
                        if (MessageUtils.isArray(msgObject)) {
                            Boolean[] messages = (Boolean[]) msgObject;
                            for (Boolean message : messages) {
                                size += com.google.protobuf.CodedOutputStream.computeBoolSize(fieldDescriptor.getNumber(), message);
                            }
                        } else {
                            size += com.google.protobuf.CodedOutputStream.computeBoolSize(fieldDescriptor.getNumber(), (Boolean) fields.get(fieldDescriptor.getName()));
                        }
                        break;
                    }
                case DescriptorProtos.FieldDescriptorProto.Type.TYPE_STRING_VALUE:
                    {
                        Object msgObject = fields.get(fieldDescriptor.getName());
                        if (MessageUtils.isArray(msgObject)) {
                            String[] messages = (String[]) msgObject;
                            for (String message : messages) {
                                size += com.google.protobuf.GeneratedMessageV3.computeStringSize(fieldDescriptor.getNumber(), message);
                            }
                        } else {
                            size += com.google.protobuf.GeneratedMessageV3.computeStringSize(fieldDescriptor.getNumber(), fields.get(fieldDescriptor.getName()));
                        }
                        break;
                    }
                case DescriptorProtos.FieldDescriptorProto.Type.TYPE_MESSAGE_VALUE:
                    {
                        Object msgObject = fields.get(fieldDescriptor.getName());
                        if (MessageUtils.isArray(msgObject)) {
                            Object[] messages = (Object[]) msgObject;
                            for (Object message : messages) {
                                size += com.google.protobuf.CodedOutputStream.computeMessageSize(fieldDescriptor.getNumber(), (MessageLite) message);
                            }
                        } else {
                            size += com.google.protobuf.CodedOutputStream.computeMessageSize(fieldDescriptor.getNumber(), (MessageLite) fields.get(fieldDescriptor.getName()));
                        }
                        break;
                    }
                default:
                    {
                        throw new UnsupportedFieldTypeException("Error while calculating the serialized type. Field " + "type is not supported : " + fieldDescriptor.getType());
                    }
            }
        }
    }
    size += unknownFields.getSerializedSize();
    memoizedSize = size;
    return size;
}
Also used : UnsupportedFieldTypeException(org.ballerinalang.net.grpc.exception.UnsupportedFieldTypeException) Descriptors(com.google.protobuf.Descriptors)

Example 4 with UnsupportedFieldTypeException

use of org.ballerinalang.net.grpc.exception.UnsupportedFieldTypeException 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 UnsupportedFieldTypeException

use of org.ballerinalang.net.grpc.exception.UnsupportedFieldTypeException 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

UnsupportedFieldTypeException (org.ballerinalang.net.grpc.exception.UnsupportedFieldTypeException)6 BBoolean (org.ballerinalang.model.values.BBoolean)4 BFloat (org.ballerinalang.model.values.BFloat)4 BInteger (org.ballerinalang.model.values.BInteger)4 BString (org.ballerinalang.model.values.BString)4 BStruct (org.ballerinalang.model.values.BStruct)4 BValue (org.ballerinalang.model.values.BValue)4 Descriptors (com.google.protobuf.Descriptors)3 BStructType (org.ballerinalang.model.types.BStructType)3 BType (org.ballerinalang.model.types.BType)3 BRefType (org.ballerinalang.model.values.BRefType)3 Message (org.ballerinalang.net.grpc.Message)2 MessageLite (com.google.protobuf.MessageLite)1