Search in sources :

Example 21 with ExtensionRegistry

use of com.google.protobuf.ExtensionRegistry in project j2objc by google.

the class MessagesTest method testParseFromByteArray.

public void testParseFromByteArray() throws Exception {
    ExtensionRegistry registry = ExtensionRegistry.newInstance();
    MessageFields.registerAllExtensions(registry);
    MessageData msg = MessageData.parseFrom(ALL_MESSAGES_BYTES, registry);
    checkFields(msg);
}
Also used : MessageData(protos.MessageData) ExtensionRegistry(com.google.protobuf.ExtensionRegistry)

Example 22 with ExtensionRegistry

use of com.google.protobuf.ExtensionRegistry in project j2objc by google.

the class MessagesTest method testMergeExistingMessageFields.

public void testMergeExistingMessageFields() throws Exception {
    ExtensionRegistry registry = ExtensionRegistry.newInstance();
    MessageFields.registerAllExtensions(registry);
    MessageData toMerge = MessageData.newBuilder().setMsgF(SubMsg.newBuilder().setUintF(123).build()).setGroupF(GroupF.newBuilder().setUintF(234).build()).addMsgR(SubMsg.newBuilder().setUintF(345).build()).addGroupR(GroupR.newBuilder().setUintF(456).build()).setExtension(MessageFields.msgFe, SubMsg.newBuilder().setUintF(567).build()).setExtension(MessageFields.groupFe, GroupFe.newBuilder().setUintF(678).build()).addExtension(MessageFields.msgRe, SubMsg.newBuilder().setUintF(789).build()).addExtension(MessageFields.groupRe, GroupRe.newBuilder().setUintF(890).build()).build();
    byte[] toMergeBytes = toMerge.toByteArray();
    // Save the singular fields so we can verify that they aren't modified by merging.
    SubMsg field1 = SubMsg.newBuilder().setIntF(321).build();
    GroupF field2 = GroupF.newBuilder().setIntF(432).build();
    SubMsg field3 = SubMsg.newBuilder().setIntF(765).build();
    GroupFe field4 = GroupFe.newBuilder().setIntF(876).build();
    MessageData.Builder builder = MessageData.newBuilder().setMsgF(field1).setGroupF(field2).addMsgR(SubMsg.newBuilder().setIntF(543).build()).addGroupR(GroupR.newBuilder().setIntF(654).build()).setExtension(MessageFields.msgFe, field3).setExtension(MessageFields.groupFe, field4).addExtension(MessageFields.msgRe, SubMsg.newBuilder().setIntF(987).build()).addExtension(MessageFields.groupRe, GroupRe.newBuilder().setIntF(98).build());
    MessageData.Builder builder1 = builder.build().toBuilder();
    builder1.mergeFrom(toMerge);
    checkMergedFields(builder1);
    MessageData.Builder builder2 = builder.build().toBuilder();
    builder2.mergeFrom(toMergeBytes, registry);
    // TODO(kstanger): This is a bug in the native ObjC runtime. It fails to
    // merge message type extension fields when reading from data. Instead it
    // just overwrites the existing message field.
    //checkMergedFields(builder2);
    assertFalse(field1.hasUintF());
    assertFalse(field2.hasUintF());
    assertFalse(field3.hasUintF());
    assertFalse(field4.hasUintF());
}
Also used : SubMsg(protos.MessageData.SubMsg) GroupF(protos.MessageData.GroupF) MessageData(protos.MessageData) GroupFe(protos.GroupFe) ExtensionRegistry(com.google.protobuf.ExtensionRegistry)

Example 23 with ExtensionRegistry

use of com.google.protobuf.ExtensionRegistry in project j2objc by google.

the class CompatibilityTest method testMergeAndParseFromInputStream.

public void testMergeAndParseFromInputStream() throws Exception {
    ExtensionRegistry registry = ExtensionRegistry.newInstance();
    registry.add(Typical.myPrimitiveExtension);
    byte[] rawData = asBytes(new int[] { 0x08, 0x06, 0x60, 0x01, 0x7A, 0x03, 0x62, 0x61, 0x72, 0xC8, 0x3E, 0x2D });
    checkMergeAndParse(TypicalData.newBuilder().mergeFrom(new ByteArrayInputStream(rawData), registry).build(), true);
    checkMergeAndParse(TypicalData.parseFrom(new ByteArrayInputStream(rawData), registry), true);
    // test API without ExtensionRegistry
    checkMergeAndParse(TypicalData.newBuilder().mergeFrom(new ByteArrayInputStream(rawData)).build(), false);
    checkMergeAndParse(TypicalData.parseFrom(new ByteArrayInputStream(rawData)), false);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ExtensionRegistry(com.google.protobuf.ExtensionRegistry)

Example 24 with ExtensionRegistry

use of com.google.protobuf.ExtensionRegistry in project heron by twitter.

the class ExtraActionUtils method getExtraActionInfo.

public static ExtraActionInfo getExtraActionInfo(String extraActionFile) {
    ExtensionRegistry registry = ExtensionRegistry.newInstance();
    ExtraActionsBase.registerAllExtensions(registry);
    try (InputStream stream = Files.newInputStream(Paths.get(extraActionFile))) {
        CodedInputStream coded = CodedInputStream.newInstance(stream);
        return ExtraActionInfo.parseFrom(coded, registry);
    } catch (IOException e) {
        throw new RuntimeException("ERROR: failed to deserialize extra action file " + extraActionFile + ": " + e.getMessage(), e);
    }
}
Also used : CodedInputStream(com.google.protobuf.CodedInputStream) InputStream(java.io.InputStream) CodedInputStream(com.google.protobuf.CodedInputStream) IOException(java.io.IOException) ExtensionRegistry(com.google.protobuf.ExtensionRegistry)

Example 25 with ExtensionRegistry

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

the class ProtobufUtilTest method testRecursiveDescriptorsMessageCWithExtensions.

@Test
public void testRecursiveDescriptorsMessageCWithExtensions() {
    // With extensions, Message C has a reference to Message A and Message B.
    ExtensionRegistry registry = ExtensionRegistry.newInstance();
    Proto2CoderTestMessages.registerAllExtensions(registry);
    assertThat(getRecursiveDescriptorFullNames(MessageC.class, registry), equalTo(MESSAGE_C_EXT));
}
Also used : MessageC(org.apache.beam.sdk.extensions.protobuf.Proto2CoderTestMessages.MessageC) ExtensionRegistry(com.google.protobuf.ExtensionRegistry) Test(org.junit.Test)

Aggregations

ExtensionRegistry (com.google.protobuf.ExtensionRegistry)29 ByteArrayInputStream (java.io.ByteArrayInputStream)10 Descriptor (com.google.protobuf.Descriptors.Descriptor)4 FieldDescriptor (com.google.protobuf.Descriptors.FieldDescriptor)4 MessageData (protos.MessageData)4 EnumDescriptor (com.google.protobuf.Descriptors.EnumDescriptor)3 EnumValueDescriptor (com.google.protobuf.Descriptors.EnumValueDescriptor)3 EnumMsg (protos.EnumMsg)3 IOException (java.io.IOException)2 Test (org.junit.Test)2 PrimitiveFields (protos.PrimitiveFields)2 StringMsg (protos.StringMsg)2 TypicalData (protos.TypicalData)2 TypicalDataMessage (protos.TypicalDataMessage)2 ByteString (com.google.protobuf.ByteString)1 CodedInputStream (com.google.protobuf.CodedInputStream)1 GenericDescriptor (com.google.protobuf.Descriptors.GenericDescriptor)1 ExtensionRegistryLite (com.google.protobuf.ExtensionRegistryLite)1 GeneratedMessage (com.google.protobuf.GeneratedMessage)1 Message (com.google.protobuf.Message)1