Search in sources :

Example 1 with DynamicMessage

use of com.google.protobuf.DynamicMessage in project druid by druid-io.

the class ProtoBufInputRowParser method buildStringKeyMap.

private Map<String, Object> buildStringKeyMap(ByteBuffer input) {
    final Descriptors.Descriptor descriptor = getDescriptor(descriptorFileInClasspath);
    final Map<String, Object> theMap = Maps.newHashMap();
    try {
        DynamicMessage message = DynamicMessage.parseFrom(descriptor, ByteString.copyFrom(input));
        Map<Descriptors.FieldDescriptor, Object> allFields = message.getAllFields();
        for (Map.Entry<Descriptors.FieldDescriptor, Object> entry : allFields.entrySet()) {
            String name = entry.getKey().getName();
            if (theMap.containsKey(name)) {
                continue;
            // Perhaps throw an exception here?
            // throw new RuntimeException("dupicate key " + name + " in " + message);
            }
            Object value = entry.getValue();
            if (value instanceof Descriptors.EnumValueDescriptor) {
                Descriptors.EnumValueDescriptor desc = (Descriptors.EnumValueDescriptor) value;
                value = desc.getName();
            }
            theMap.put(name, value);
        }
    } catch (InvalidProtocolBufferException e) {
        log.warn(e, "Problem with protobuf something");
    }
    return theMap;
}
Also used : InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ByteString(com.google.protobuf.ByteString) DynamicMessage(com.google.protobuf.DynamicMessage) Descriptors(com.google.protobuf.Descriptors) Map(java.util.Map)

Example 2 with DynamicMessage

use of com.google.protobuf.DynamicMessage in project BIMserver by opensourceBIM.

the class ProtocolBuffersConverter method convertFieldValue.

private Object convertFieldValue(SField field, Object val) throws ConvertException {
    if (val instanceof EnumValueDescriptor) {
        EnumValueDescriptor enumValueDescriptor = (EnumValueDescriptor) val;
        Class<?> enumClass;
        try {
            enumClass = Class.forName("org.bimserver.interfaces.objects." + enumValueDescriptor.getType().getName());
            for (Object v : enumClass.getEnumConstants()) {
                Enum<?> e = (Enum<?>) v;
                if (e.ordinal() == enumValueDescriptor.getNumber()) {
                    val = e;
                    break;
                }
            }
        } catch (ClassNotFoundException e) {
            LOGGER.error("", e);
        }
        return val;
    } else if (field.getType().getInstanceClass() == Date.class) {
        return new Date((Long) val);
    } else if (field.getType().getInstanceClass() == DataHandler.class) {
        ByteString byteString = (ByteString) val;
        ByteArrayDataSource byteArrayDataSource = new ByteArrayDataSource("test", byteString.toByteArray());
        return new DataHandler(byteArrayDataSource);
    } else if (val instanceof DynamicMessage) {
        return convertProtocolBuffersMessageToSObject((DynamicMessage) val, null, field.isAggregate() ? field.getGenericType() : field.getType());
    } else {
        return val;
    }
}
Also used : ByteString(com.google.protobuf.ByteString) DataHandler(javax.activation.DataHandler) DynamicMessage(com.google.protobuf.DynamicMessage) ByteArrayDataSource(org.bimserver.utils.ByteArrayDataSource) EnumValueDescriptor(com.google.protobuf.Descriptors.EnumValueDescriptor) Date(java.util.Date)

Example 3 with DynamicMessage

use of com.google.protobuf.DynamicMessage in project BIMserver by opensourceBIM.

the class ProtocolBuffersConverter method convertProtocolBuffersMessageToSObject.

@SuppressWarnings({ "rawtypes", "unchecked" })
public SBase convertProtocolBuffersMessageToSObject(Message message, SBase newInstance, SClass sClass) throws ConvertException {
    try {
        Descriptor descriptor = message.getDescriptorForType();
        if (newInstance == null) {
            newInstance = sClass.newInstance();
        }
        Message subTypeMessage = null;
        for (FieldDescriptor fieldDescriptor : descriptor.getFields()) {
            if (fieldDescriptor.getName().equals("__actual_type")) {
                sClass = sClass.getServicesMap().getSType((String) message.getField(fieldDescriptor));
                newInstance = sClass.newInstance();
            } else if (fieldDescriptor.getName().startsWith("__")) {
                if (fieldDescriptor.getName().substring(2).equals(sClass.getSimpleName())) {
                    subTypeMessage = (Message) message.getField(fieldDescriptor);
                }
            } else {
                Object val = message.getField(fieldDescriptor);
                SField field = newInstance.getSClass().getField(fieldDescriptor.getName());
                if (field == null) {
                    throw new ConvertException("No field on " + sClass.getName() + " with name " + fieldDescriptor.getName());
                }
                if (fieldDescriptor.isRepeated()) {
                    List list = new ArrayList();
                    if (val instanceof List) {
                        List oldList = (List) val;
                        for (Object x : oldList) {
                            list.add(convertFieldValue(field, x));
                        }
                    } else if (val instanceof DynamicMessage) {
                        int size = message.getRepeatedFieldCount(fieldDescriptor);
                        for (int index = 0; index < size; index++) {
                            Object repeatedField = message.getRepeatedField(fieldDescriptor, index);
                            list.add(convertFieldValue(field, repeatedField));
                        }
                    } else {
                        throw new ConvertException("Field " + sClass.getName() + "." + fieldDescriptor.getName() + " should have list value");
                    }
                    newInstance.sSet(field, list);
                } else {
                    SField sField = sClass.getField(fieldDescriptor.getName());
                    newInstance.sSet(sField, convertFieldValue(sField, val));
                }
            }
        }
        if (subTypeMessage != null) {
            convertProtocolBuffersMessageToSObject(subTypeMessage, newInstance, sClass);
        }
        return newInstance;
    } catch (IllegalArgumentException e) {
        LOGGER.error("", e);
    }
    return null;
}
Also used : DynamicMessage(com.google.protobuf.DynamicMessage) Message(com.google.protobuf.Message) ArrayList(java.util.ArrayList) Descriptor(com.google.protobuf.Descriptors.Descriptor) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) EnumValueDescriptor(com.google.protobuf.Descriptors.EnumValueDescriptor) SField(org.bimserver.shared.meta.SField) ArrayList(java.util.ArrayList) List(java.util.List) ByteString(com.google.protobuf.ByteString) DynamicMessage(com.google.protobuf.DynamicMessage) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor)

Example 4 with DynamicMessage

use of com.google.protobuf.DynamicMessage in project tesla by linking12.

the class DynamicGrpcClient method createGrpcDynamicMessage.

private DynamicMessage createGrpcDynamicMessage(final Descriptor messageDefine, String json) throws IOException {
    DynamicMessage.Builder messageBuilder = DynamicMessage.newBuilder(messageDefine);
    JSON2PROTOBUF.merge(new ByteArrayInputStream(json.getBytes()), messageBuilder);
    return messageBuilder.build();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) DynamicMessage(com.google.protobuf.DynamicMessage)

Example 5 with DynamicMessage

use of com.google.protobuf.DynamicMessage in project beam by apache.

the class BeamRowToStorageApiProto method messageFromBeamRow.

/**
 * Given a Beam {@link Row} object, returns a protocol-buffer message that can be used to write
 * data using the BigQuery Storage streaming API.
 */
public static DynamicMessage messageFromBeamRow(Descriptor descriptor, Row row) {
    Schema beamSchema = row.getSchema();
    DynamicMessage.Builder builder = DynamicMessage.newBuilder(descriptor);
    for (int i = 0; i < row.getFieldCount(); ++i) {
        Field beamField = beamSchema.getField(i);
        FieldDescriptor fieldDescriptor = Preconditions.checkNotNull(descriptor.findFieldByName(beamField.getName().toLowerCase()));
        @Nullable Object value = messageValueFromRowValue(fieldDescriptor, beamField, i, row);
        if (value != null) {
            builder.setField(fieldDescriptor, value);
        }
    }
    return builder.build();
}
Also used : Field(org.apache.beam.sdk.schemas.Schema.Field) Schema(org.apache.beam.sdk.schemas.Schema) DynamicMessage(com.google.protobuf.DynamicMessage) Nullable(javax.annotation.Nullable) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor)

Aggregations

DynamicMessage (com.google.protobuf.DynamicMessage)31 Test (org.junit.Test)13 FieldDescriptor (com.google.protobuf.Descriptors.FieldDescriptor)12 ByteString (com.google.protobuf.ByteString)11 Descriptor (com.google.protobuf.Descriptors.Descriptor)9 Message (com.google.protobuf.Message)6 IOException (java.io.IOException)6 List (java.util.List)6 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)5 Map (java.util.Map)4 TableRow (com.google.api.services.bigquery.model.TableRow)3 EnumValueDescriptor (com.google.protobuf.Descriptors.EnumValueDescriptor)3 Builder (com.google.protobuf.DynamicMessage.Builder)3 ByteBuffer (java.nio.ByteBuffer)3 ArrayList (java.util.ArrayList)3 Date (java.util.Date)3 DataHandler (javax.activation.DataHandler)3 Nullable (javax.annotation.Nullable)3 Descriptors (com.google.protobuf.Descriptors)2 JsonFormat (com.google.protobuf.util.JsonFormat)2