use of com.google.protobuf.DynamicMessage in project beam by apache.
the class TableRowToStorageApiProtoTest method testNullRepeatedDescriptorFromTableSchema.
@Test
public void testNullRepeatedDescriptorFromTableSchema() throws Exception {
TableRow repeatedRow = new TableRow().set("repeated1", null).set("repeated2", null).set("repeatednof1", null).set("repeatednof2", null);
Descriptor descriptor = TableRowToStorageApiProto.getDescriptorFromTableSchema(REPEATED_MESSAGE_SCHEMA);
DynamicMessage msg = TableRowToStorageApiProto.messageFromTableRow(descriptor, repeatedRow);
Map<String, FieldDescriptor> fieldDescriptors = descriptor.getFields().stream().collect(Collectors.toMap(FieldDescriptor::getName, Functions.identity()));
List<DynamicMessage> repeated1 = (List<DynamicMessage>) msg.getField(fieldDescriptors.get("repeated1"));
assertTrue(repeated1.isEmpty());
List<DynamicMessage> repeated2 = (List<DynamicMessage>) msg.getField(fieldDescriptors.get("repeated2"));
assertTrue(repeated2.isEmpty());
List<DynamicMessage> repeatednof1 = (List<DynamicMessage>) msg.getField(fieldDescriptors.get("repeatednof1"));
assertTrue(repeatednof1.isEmpty());
List<DynamicMessage> repeatednof2 = (List<DynamicMessage>) msg.getField(fieldDescriptors.get("repeatednof2"));
assertTrue(repeatednof2.isEmpty());
}
use of com.google.protobuf.DynamicMessage in project druid by druid-io.
the class ProtobufReader method parseInputRows.
@Override
protected List<InputRow> parseInputRows(DynamicMessage intermediateRow) throws ParseException, JsonProcessingException {
Map<String, Object> record;
if (flattenSpec == null || JSONPathSpec.DEFAULT.equals(flattenSpec)) {
try {
record = CollectionUtils.mapKeys(intermediateRow.getAllFields(), k -> k.getJsonName());
} catch (Exception ex) {
throw new ParseException(null, ex, "Protobuf message could not be parsed");
}
} else {
try {
String json = JsonFormat.printer().print(intermediateRow);
record = recordFlattener.flatten(OBJECT_MAPPER.readValue(json, JsonNode.class));
} catch (InvalidProtocolBufferException e) {
throw new ParseException(null, e, "Protobuf message could not be parsed");
}
}
return Collections.singletonList(MapInputRowParser.parse(inputRowSchema, record));
}
use of com.google.protobuf.DynamicMessage in project druid by druid-io.
the class SchemaRegistryBasedProtobufBytesDecoder method parse.
@Override
public DynamicMessage parse(ByteBuffer bytes) {
// ignore first \0 byte
bytes.get();
// extract schema registry id
int id = bytes.getInt();
// ignore \0 byte before PB message
bytes.get();
int length = bytes.limit() - 2 - 4;
Descriptors.Descriptor descriptor;
try {
ProtobufSchema schema = (ProtobufSchema) registry.getSchemaById(id);
descriptor = schema.toDescriptor();
} catch (RestClientException e) {
LOGGER.error(e.getMessage());
throw new ParseException(null, e, "Fail to get protobuf schema because of can not connect to registry or failed http request!");
} catch (IOException e) {
LOGGER.error(e.getMessage());
throw new ParseException(null, e, "Fail to get protobuf schema because of invalid schema!");
}
try {
byte[] rawMessage = new byte[length];
bytes.get(rawMessage, 0, length);
DynamicMessage message = DynamicMessage.parseFrom(descriptor, rawMessage);
return message;
} catch (Exception e) {
LOGGER.error(e.getMessage());
throw new ParseException(null, e, "Fail to decode protobuf message!");
}
}
use of com.google.protobuf.DynamicMessage in project tesla by linking12.
the class DynamicGrpcClient method doRemoteCall.
@Override
public String doRemoteCall(final FilterRpcDO rpcDo, final String jsonInput) {
try {
final String serviceName = rpcDo.getServiceName();
final String methodName = rpcDo.getMethodName();
final String group = rpcDo.getServiceGroup();
final String version = rpcDo.getServiceVersion();
Pair<Descriptor, Descriptor> inOutType = ProtobufUtil.resolveServiceInputOutputType(rpcDo);
Descriptor inPutType = inOutType.getLeft();
Descriptor outPutType = inOutType.getRight();
MethodDescriptor<DynamicMessage, DynamicMessage> methodDesc = this.createGrpcMethodDescriptor(serviceName, methodName, inPutType, outPutType);
DynamicMessage message = this.createGrpcDynamicMessage(inPutType, jsonInput);
Message response = (Message) genricService.$invoke(serviceName, group, version, methodName, methodDesc, message);
return JSON2PROTOBUF.printToString(response);
} catch (IOException e) {
throw new RpcServiceException(String.format("json covert to DynamicMessage failed! the json is :%s, the protobuf type is: %s", jsonInput), e);
} catch (Throwable e) {
throw new RpcFrameworkException(String.format("service definition is wrong,please check the proto file you update,service is %s, method is %s", rpcDo.getServiceName(), rpcDo.getMethodName()), e);
}
}
use of com.google.protobuf.DynamicMessage in project BIMserver by opensourceBIM.
the class Handler method run.
@Override
public void run() {
running = true;
try {
DataInputStream dis = new DataInputStream(socket.getInputStream());
String token = dis.readUTF();
ReflectiveRpcChannel reflectiveRpcChannel = new ReflectiveRpcChannel(ServiceInterface.class, serviceFactory.get(token, AccessMethod.INTERNAL).get(ServiceInterface.class), protocolBuffersMetaData, servicesMap);
while (running) {
String serviceName = dis.readUTF();
String methodName = dis.readUTF();
MethodDescriptorContainer methodDescriptorContainer;
try {
methodDescriptorContainer = protocolBuffersMetaData.getMethod(serviceName, methodName);
Builder newBuilder = DynamicMessage.newBuilder(methodDescriptorContainer.getInputDescriptor());
newBuilder.mergeDelimitedFrom(dis);
DynamicMessage request = newBuilder.build();
Message response = reflectiveRpcChannel.callBlockingMethod(methodDescriptorContainer, request);
response.writeDelimitedTo(socket.getOutputStream());
} catch (ServiceNotFoundException e) {
// TODO should return a usable pb message for the user
LOGGER.error("", e);
} catch (ServiceMethodNotFoundException e) {
// TODO should return a usable pb message for the user
LOGGER.error("", e);
}
}
} catch (IOException e) {
LOGGER.error("", e);
} catch (UserException e) {
LOGGER.error("", e);
}
socketNotificationsClient.notifyDisconnect();
}
Aggregations