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;
}
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;
}
}
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;
}
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();
}
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();
}
Aggregations