下面列出了com.google.protobuf.DynamicMessage#parseFrom ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
protected DynamicMessage readData(byte[] schema, ByteBuffer buffer, int start, int length) {
try {
Serde.Schema s = Serde.Schema.parseFrom(schema);
Descriptors.FileDescriptor fileDescriptor = toFileDescriptor(s);
byte[] bytes = new byte[length];
System.arraycopy(buffer.array(), start, bytes, 0, length);
ByteArrayInputStream is = new ByteArrayInputStream(bytes);
Serde.Ref ref = Serde.Ref.parseDelimitedFrom(is);
Descriptors.Descriptor descriptor = fileDescriptor.findMessageTypeByName(ref.getName());
return DynamicMessage.parseFrom(descriptor, is);
} catch (IOException | Descriptors.DescriptorValidationException e) {
throw new IllegalStateException(e);
}
}
/**
* Encodes the data portion of an ExecutionResult as ByteString.
*
* <p>The FileDescriptorSet must contain a message with the name "{operationName}Response". This
* message will be populated with data from the execution result and encoded as a ByteString.
*/
public static ByteString encodeResponse(
String operationName, FileDescriptorSet fileDescriptorSet, ExecutionResult executionResult) {
try {
// TODO: Support multiple FileDescriptors in FileDescriptorSet
FileDescriptor fileDescriptor =
FileDescriptor.buildFrom(fileDescriptorSet.getFileList().get(0), new FileDescriptor[] {});
Descriptor messageType = fileDescriptor.findMessageTypeByName(operationName + "Response");
Message message = DynamicMessage.parseFrom(messageType, ByteString.EMPTY);
Message responseData = QueryResponseToProto.buildMessage(message, executionResult.getData());
return responseData.toByteString();
} catch (DescriptorValidationException | InvalidProtocolBufferException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
/**
* Handle all the logic leading to the decoding of a Protobuf-encoded binary given a schema file path.
* @param schema Schema used to decode the binary data
* @param messageType Type of Protobuf Message
* @param encodedData Encoded data source
* @return A JSON representation of the data, contained in a Java String
* @throws InvalidProtocolBufferException Thrown when an error occurs during the encoding of the decoded data into JSON
* @throws Descriptors.DescriptorValidationException Thrown when the schema is invalid
* @throws UnknownMessageTypeException Thrown when the given message type is not contained in the schema
* @throws MessageDecodingException Thrown when an error occurs during the binary decoding
* @throws SchemaLoadingException Thrown when an error occurs while reading the schema file
*/
public static String decodeProtobuf(DynamicSchema schema, String messageType, InputStream encodedData) throws InvalidProtocolBufferException, Descriptors.DescriptorValidationException, UnknownMessageTypeException, MessageDecodingException, SchemaLoadingException {
Descriptors.Descriptor descriptor;
DynamicMessage message;
descriptor = schema.getMessageDescriptor(messageType);
if (descriptor == null) {
throw new UnknownMessageTypeException(messageType);
}
try {
message = DynamicMessage.parseFrom(descriptor, encodedData);
} catch (IOException e) {
throw new MessageDecodingException(e);
}
return JSONMapper.toJSON(message);
}
@Override
public Row expandRow (FDBStore store, Session session,
FDBStoreData storeData, Schema schema) {
ensureRowConverter();
DynamicMessage msg;
try {
msg = DynamicMessage.parseFrom(rowConverter.getMessageType(), storeData.rawValue);
} catch (InvalidProtocolBufferException ex) {
ProtobufReadException nex = new ProtobufReadException(rowDataConverter.getMessageType().getName(), ex.getMessage());
nex.initCause(ex);
throw nex;
}
Row row = rowConverter.decode(msg);
row = overlayBlobData(row.rowType(), row, store, session);
return row;
}
@Test
public void testGetDescriptor() throws IOException {
Descriptor descriptor2 = AddressBookProtos.AddressBook.getDescriptor();
FieldDescriptor stringMapFD = descriptor2.findFieldByName("person");
byte[] bytes = getProtoBytes2();
DynamicMessage parseFrom = DynamicMessage.parseFrom(descriptor2, bytes);
Object field = parseFrom.getField(stringMapFD);
Assert.assertTrue(field instanceof List);
Codec<AddressBookProtosPOJO> codec = ProtobufProxy.create(AddressBookProtosPOJO.class, true);
Descriptor descriptor = codec.getDescriptor();
stringMapFD = descriptor.findFieldByName("list");
bytes = getProtoBytes2();
parseFrom = DynamicMessage.parseFrom(descriptor, bytes);
Object field2 = parseFrom.getField(stringMapFD);
Assert.assertTrue(field2 instanceof List);
}
private Object convertFieldValue(Descriptors.FieldDescriptor fieldDescriptor, Object value) {
switch (fieldDescriptor.getType()) {
case MESSAGE:
try {
DynamicMessage dynamicMessage =
DynamicMessage.parseFrom(fieldDescriptor.getMessageType(), (ByteString) value);
return dynamicMessage;
} catch (IOException e) {
throw new RuntimeException(e);
}
case BOOL:
return value.equals(1L);
case ENUM:
case STRING:
ByteString byteString = (ByteString) value;
return byteString.toStringUtf8();
case INT32:
case INT64:
return unsignedToString((Long) value);
case DOUBLE:
return Double.longBitsToDouble((Long) value);
case FLOAT:
return Float.intBitsToFloat((Integer) value);
}
throw new RuntimeException(
"conversion of unknownfield for type "
+ fieldDescriptor.getType().toString()
+ " not implemented");
}
private static DynamicMessage dynamic(Message message) {
try {
return DynamicMessage.parseFrom(message.getDescriptorForType(), message.toByteString());
} catch (InvalidProtocolBufferException e) {
throw new AssertionError(e);
}
}
private static DynamicMessage dynamic(Message message) {
try {
return DynamicMessage.parseFrom(message.getDescriptorForType(), message.toByteString());
} catch (InvalidProtocolBufferException e) {
throw new AssertionError(e);
}
}
private static DynamicMessage dynamic(Message message) {
try {
return DynamicMessage.parseFrom(message.getDescriptorForType(), message.toByteString());
} catch (InvalidProtocolBufferException e) {
throw new AssertionError(e);
}
}
private static DynamicMessage dynamic(Message message) {
try {
return DynamicMessage.parseFrom(message.getDescriptorForType(), message.toByteString());
} catch (InvalidProtocolBufferException e) {
throw new AssertionError(e);
}
}
@Nonnull
protected DynamicMessage deserializeFromBytes(@Nonnull Descriptors.Descriptor storedDescriptor,
@Nonnull byte[] serialized) {
try {
return DynamicMessage.parseFrom(storedDescriptor, serialized);
} catch (InvalidProtocolBufferException ex) {
throw new RecordSerializationException("Error reading from byte array", ex)
.addLogInfo("recordType", storedDescriptor.getName());
}
}
/**
* Validate that it is legal to change the records descriptor from proto2 to proto3 as long as all of the records
* contained within that file are still the same syntax.
*/
@Test
public void onlyFileProto2ToProto3() throws InvalidProtocolBufferException {
assertNotEquals(TestRecords1Proto.getDescriptor().getSyntax(), TestRecords1ImportedProto.getDescriptor().getSyntax());
MetaDataEvolutionValidator.getDefaultInstance().validateUnion(
TestRecords1Proto.RecordTypeUnion.getDescriptor(),
TestRecords1ImportedProto.RecordTypeUnion.getDescriptor()
);
MetaDataEvolutionValidator.getDefaultInstance().validateUnion(
TestRecords1ImportedProto.RecordTypeUnion.getDescriptor(),
TestRecords1Proto.RecordTypeUnion.getDescriptor()
);
RecordMetaData metaData1 = RecordMetaData.build(TestRecords1Proto.getDescriptor());
RecordMetaData metaData2 = replaceRecordsDescriptor(metaData1, TestRecords1ImportedProto.getDescriptor());
MetaDataEvolutionValidator.getDefaultInstance().validate(metaData1, metaData2);
// Validate that the nested proto2 records in the proto3 file have proper nullability semantics
TestRecords1Proto.RecordTypeUnion unionRecordProto2 = TestRecords1Proto.RecordTypeUnion.newBuilder()
.setMySimpleRecord(TestRecords1Proto.MySimpleRecord.newBuilder().setRecNo(0).setStrValueIndexed(""))
.build();
assertThat(unionRecordProto2.getMySimpleRecord().hasNumValue2(), is(false));
assertThat(unionRecordProto2.getMySimpleRecord().hasStrValueIndexed(), is(true));
TestRecords1ImportedProto.RecordTypeUnion unionRecordProto3 = TestRecords1ImportedProto.RecordTypeUnion.parseFrom(unionRecordProto2.toByteString());
assertThat(unionRecordProto3.getMySimpleRecord().hasNumValue2(), is(false));
assertThat(unionRecordProto3.getMySimpleRecord().hasStrValueIndexed(), is(true));
final FieldDescriptor unionField = TestRecords1ImportedProto.RecordTypeUnion.getDescriptor().findFieldByName("_MySimpleRecord");
final FieldDescriptor numValue2Field = TestRecords1Proto.MySimpleRecord.getDescriptor().findFieldByName("num_value_2");
final FieldDescriptor strValueIndexedField = TestRecords1Proto.MySimpleRecord.getDescriptor().findFieldByName("str_value_indexed");
DynamicMessage dynamicUnionRecordProto3 = DynamicMessage.parseFrom(TestRecords1ImportedProto.RecordTypeUnion.getDescriptor(), unionRecordProto2.toByteString());
Message dynamicSimpleRecord = (Message)dynamicUnionRecordProto3.getField(unionField);
assertThat(dynamicSimpleRecord.hasField(numValue2Field), is(false));
assertThat(dynamicSimpleRecord.hasField(strValueIndexedField), is(true));
}
/**
* Returns a Map containing data from a ByteString that is parsed using the Proto Descriptor.
*
* <p>The FileDescriptorSet must contain a message with the name "{operationName}Request". This
* message will be used to parse the ByteString, and the resulting message will be transformed and
* returned as a Map.
*/
public static Map<String, Object> decodeVariables(
String operationName, FileDescriptorSet fileDescriptorSet, ByteString encodedRequest) {
try {
// TODO: Support multiple FileDescriptors in FileDescriptorSet
FileDescriptor fileDescriptor =
FileDescriptor.buildFrom(fileDescriptorSet.getFileList().get(0), new FileDescriptor[] {});
Descriptor messageType = fileDescriptor.findMessageTypeByName(operationName + "Request");
Message message = DynamicMessage.parseFrom(messageType, encodedRequest);
return ProtoToMap.messageToMap(message);
} catch (DescriptorValidationException | InvalidProtocolBufferException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
@Test
public void testDifferentClasses() throws InvalidProtocolBufferException {
Message message = parse("o_int: 3");
DynamicMessage dynamicMessage =
DynamicMessage.parseFrom(message.getDescriptorForType(), message.toByteString());
assertThat(message).isEqualTo(dynamicMessage);
assertThat(dynamicMessage).isEqualTo(message);
}
@Override
public Iterable<Row> translate(Row message) throws Exception {
byte[] value = message.getAs(Translator.VALUE_FIELD_NAME);
if (value == null || value.length < 1) {
throw new RuntimeException("Payload value is null or empty");
}
DynamicMessage msg;
BufferedInputStream valueInputStream = new BufferedInputStream(new ByteArrayInputStream(value));
// Parse into Message
try {
// Check if the value is gzipped
if (ProtobufUtils.isGzipped(valueInputStream)) {
LOG.trace("Decompressing GZIP byte array");
msg = DynamicMessage.parseFrom(descriptor, new GZIPInputStream(valueInputStream));
} else {
msg = DynamicMessage.parseFrom(descriptor, valueInputStream);
}
} catch (InvalidProtocolBufferException ex) {
throw new RuntimeException("Error while parsing message from descriptor and raw bytes", ex);
}
// Populate set of row values matching full schema
// NOTE: very likely this will be sparse
List<Object> rowValues = ProtobufUtils.buildRowValues(descriptor, msg, schema);
Row row = new RowWithSchema(schema, rowValues.toArray());
return Collections.singletonList(row);
}
@Override
public Row expandRow(MemoryStore store, Session session, MemoryStoreData storeData, Schema schema) {
ensureRowConverter();
DynamicMessage msg;
try {
msg = DynamicMessage.parseFrom(rowConverter.getMessageType(), storeData.rawValue);
} catch(InvalidProtocolBufferException ex) {
ProtobufReadException nex = new ProtobufReadException(rowConverter.getMessageType().getName(), ex.getMessage());
nex.initCause(ex);
throw nex;
}
return rowConverter.decode(msg);
}
@Test
public void testGetDescriptor() throws IOException {
Codec<SimpleMapPOJO> codec = ProtobufProxy.create(SimpleMapPOJO.class, false);
Descriptor descriptor = codec.getDescriptor();
String escapeBytes = StringUtils.escapeBytes(descriptor.toProto().toByteArray());
System.out.println(escapeBytes);
FieldDescriptor stringMapFD = descriptor.findFieldByName("stringMap");
byte[] bytes = getProtoBytes2();
DynamicMessage parseFrom = DynamicMessage.parseFrom(descriptor, bytes);
Object field = parseFrom.getField(stringMapFD);
Assert.assertTrue(field instanceof List);
Assert.assertEquals(2, ((List) field).size());
Descriptor descriptor2 = AddressBookProtos.Person.getDescriptor();
stringMapFD = descriptor2.findFieldByName("stringMap");
bytes = getProtoBytes2();
parseFrom = DynamicMessage.parseFrom(descriptor2, bytes);
field = parseFrom.getField(stringMapFD);
Assert.assertTrue(field instanceof List);
Assert.assertEquals(2, ((List) field).size());
}
@Test
public void testCompareSerialization() throws Exception {
final TestRecordsNulls2Proto.MyNullRecord emptyProto2 = buildRecord2("record", null, null);
final TestRecordsNulls3Proto.MyNullRecord emptyProto3 = buildRecord3("record", null, null);
final DynamicMessage emptyDynamic2 = buildRecord2Dynamic("record", null, null);
final DynamicMessage emptyDynamic3 = buildRecord3Dynamic("record", null, null);
final TestRecordsNulls2Proto.MyNullRecord defaultProto2 = buildRecord2("record", 0, "");
final TestRecordsNulls3Proto.MyNullRecord defaultProto3 = buildRecord3("record", 0, "");
final DynamicMessage defaultDynamic2 = buildRecord2Dynamic("record", 0, "");
final DynamicMessage defaultDynamic3 = buildRecord3Dynamic("record", 0, "");
final TestRecordsNulls2Proto.MyNullRecord oneProto2 = buildRecord2("record", 1, "A");
final TestRecordsNulls3Proto.MyNullRecord oneProto3 = buildRecord3("record", 1, "A");
final DynamicMessage oneDynamic2 = buildRecord2Dynamic("record", 1, "A");
final DynamicMessage oneDynamic3 = buildRecord3Dynamic("record", 1, "A");
checkHasField(emptyProto2, emptyProto3, emptyDynamic2, emptyDynamic3,
defaultProto2, defaultProto3, defaultDynamic2, defaultDynamic3,
oneProto2, oneProto3, oneDynamic2, oneDynamic3);
final byte[] emptySerialized = emptyProto2.toByteArray();
assertThat("empty serialized from proto3 should be the same as proto2", emptyProto3.toByteArray(), equalTo(emptySerialized));
assertThat("empty serialized from dynamic2 should be the same as proto2", emptyDynamic2.toByteArray(), equalTo(emptySerialized));
assertThat("empty serialized from dynamic3 should be the same as proto2", emptyDynamic3.toByteArray(), equalTo(emptySerialized));
final byte[] defaultSerialized = defaultProto2.toByteArray();
assertThat("empty and default serialized by proto2 should differ", defaultSerialized, not(equalTo(emptySerialized)));
assertThat("default serialized from dynamic2 should be the same as proto2", defaultDynamic2.toByteArray(), equalTo(defaultSerialized));
if (isProto3()) {
// Fields set to default cannot be distinguished from cleared, so not serialized.
assertThat("default serialized from proto3 should be the same as proto2 empty", defaultProto3.toByteArray(), equalTo(emptySerialized));
assertThat("default serialized from dynamic3 should be the same as proto2 empty", defaultDynamic3.toByteArray(), equalTo(emptySerialized));
} else {
assertThat("default serialized from proto3 should be the same as proto2", defaultProto3.toByteArray(), equalTo(defaultSerialized));
assertThat("default serialized from dynamic3 should be the same as proto2", defaultDynamic3.toByteArray(), equalTo(defaultSerialized));
}
final byte[] oneSerialized = oneProto2.toByteArray();
assertThat("empty and one serialized by proto2 should differ", oneSerialized, not(equalTo(emptySerialized)));
assertThat("one serialized from proto3 should be the same as proto2", oneProto3.toByteArray(), equalTo(oneSerialized));
assertThat("one serialized from dynamic2 should be the same as proto2", oneDynamic2.toByteArray(), equalTo(oneSerialized));
assertThat("one serialized from dynamic3 should be the same as proto2", oneDynamic3.toByteArray(), equalTo(oneSerialized));
final TestRecordsNulls2Proto.MyNullRecord emptyProto2x = TestRecordsNulls2Proto.MyNullRecord.parseFrom(emptySerialized);
final TestRecordsNulls3Proto.MyNullRecord emptyProto3x = TestRecordsNulls3Proto.MyNullRecord.parseFrom(emptySerialized);
final DynamicMessage emptyDynamic2x = DynamicMessage.parseFrom(TestRecordsNulls2Proto.MyNullRecord.getDescriptor(), emptySerialized);
final DynamicMessage emptyDynamic3x = DynamicMessage.parseFrom(TestRecordsNulls3Proto.MyNullRecord.getDescriptor(), emptySerialized);
final TestRecordsNulls2Proto.MyNullRecord defaultProto2x = TestRecordsNulls2Proto.MyNullRecord.parseFrom(defaultSerialized);
final TestRecordsNulls3Proto.MyNullRecord defaultProto3x = TestRecordsNulls3Proto.MyNullRecord.parseFrom(defaultSerialized);
final DynamicMessage defaultDynamic2x = DynamicMessage.parseFrom(TestRecordsNulls2Proto.MyNullRecord.getDescriptor(), defaultSerialized);
final DynamicMessage defaultDynamic3x = DynamicMessage.parseFrom(TestRecordsNulls3Proto.MyNullRecord.getDescriptor(), defaultSerialized);
final TestRecordsNulls2Proto.MyNullRecord oneProto2x = TestRecordsNulls2Proto.MyNullRecord.parseFrom(oneSerialized);
final TestRecordsNulls3Proto.MyNullRecord oneProto3x = TestRecordsNulls3Proto.MyNullRecord.parseFrom(oneSerialized);
final DynamicMessage oneDynamic2x = DynamicMessage.parseFrom(TestRecordsNulls2Proto.MyNullRecord.getDescriptor(), oneSerialized);
final DynamicMessage oneDynamic3x = DynamicMessage.parseFrom(TestRecordsNulls3Proto.MyNullRecord.getDescriptor(), oneSerialized);
checkHasField(emptyProto2x, emptyProto3x, emptyDynamic2x, emptyDynamic3x,
defaultProto2x, defaultProto3x, defaultDynamic2x, defaultDynamic3x,
oneProto2x, oneProto3x, oneDynamic2x, oneDynamic3x);
}
@BeforeClass
public static void createMessages() throws IOException {
ProtobufSingleMessage.SingleExample msg = ProtobufSingleMessage.SingleExample.newBuilder()
.setString("foo")
.setDouble(123.456D)
.setFloat(123.456F)
.setInt32(123)
.setInt64(123L)
.setUint32(123)
.setUint64(123L)
.setSint32(123)
.setSint64(123L)
.setFixed32(123)
.setFixed64(123L)
.setSfixed32(123)
.setSfixed64(123L)
.setBoolean(true)
.setBytes(ByteString.copyFromUtf8("foo"))
.setEnum(ProtobufSingleMessage.SingleExample.EnumExample.THREE)
.setNested(ProtobufSingleMessage.SingleExample.NestedExample.newBuilder()
.setNested("single")
)
.putMapInt("first", 1)
.putMapInt("second", 2)
.addRepeatingMessage(ProtobufSingleMessage.SingleExample.NestedExample.newBuilder()
.setNested("repeated 1")
)
.addRepeatingMessage(ProtobufSingleMessage.SingleExample.NestedExample.newBuilder()
.setNested("repeated 2")
)
.addRepeatingInt32(567)
.addRepeatingInt32(890)
.addRepeatingEnum(ProtobufSingleMessage.SingleExample.EnumExample.ONE)
.addRepeatingEnum(ProtobufSingleMessage.SingleExample.EnumExample.TWO)
.build();
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
msg.writeTo(outputStream);
message = DynamicMessage.parseFrom(SINGLE_DESC, outputStream.toByteArray());
}
}
private DynamicMessage toDynamic(Message message) throws InvalidProtocolBufferException {
return DynamicMessage.parseFrom(message.getDescriptorForType(), message.toByteArray());
}