Beispiel #1
0
        public void ParseMessageSet()
        {
            ExtensionRegistry extensionRegistry = ExtensionRegistry.CreateInstance();

            extensionRegistry.Add(TestMessageSetExtension1.MessageSetExtension);
            extensionRegistry.Add(TestMessageSetExtension2.MessageSetExtension);

            // Set up a RawMessageSet with two known messages and an unknown one.
            RawMessageSet raw =
                RawMessageSet.CreateBuilder()
                .AddItem(
                    RawMessageSet.Types.Item.CreateBuilder()
                    .SetTypeId(TypeId1)
                    .SetMessage(
                        TestMessageSetExtension1.CreateBuilder()
                        .SetI(123)
                        .Build().ToByteString())
                    .Build())
                .AddItem(
                    RawMessageSet.Types.Item.CreateBuilder()
                    .SetTypeId(TypeId2)
                    .SetMessage(
                        TestMessageSetExtension2.CreateBuilder()
                        .SetStr("foo")
                        .Build().ToByteString())
                    .Build())
                .AddItem(
                    RawMessageSet.Types.Item.CreateBuilder()
                    .SetTypeId(UnknownTypeId)
                    .SetMessage(ByteString.CopyFromUtf8("bar"))
                    .Build())
                .Build();

            ByteString data = raw.ToByteString();

            // Parse as a TestMessageSet and check the contents.
            TestMessageSet messageSet =
                TestMessageSet.ParseFrom(data, extensionRegistry);

            Assert.AreEqual(123, messageSet.GetExtension(TestMessageSetExtension1.MessageSetExtension).I);
            Assert.AreEqual("foo", messageSet.GetExtension(TestMessageSetExtension2.MessageSetExtension).Str);

            // Check for unknown field with type LENGTH_DELIMITED,
            //   number UNKNOWN_TYPE_ID, and contents "bar".
            UnknownFieldSet unknownFields = messageSet.UnknownFields;

            Assert.AreEqual(1, unknownFields.FieldDictionary.Count);
            Assert.IsTrue(unknownFields.HasField(UnknownTypeId));

            UnknownField field = unknownFields[UnknownTypeId];

            Assert.AreEqual(1, field.LengthDelimitedList.Count);
            Assert.AreEqual("bar", field.LengthDelimitedList[0].ToStringUtf8());
        }
        public void Group()
        {
            FieldDescriptor nestedFieldDescriptor =
                TestAllTypes.Types.OptionalGroup.Descriptor.FindDescriptor <FieldDescriptor>("a");

            Assert.IsNotNull(nestedFieldDescriptor);

            UnknownField field = GetField("optionalgroup");

            Assert.AreEqual(1, field.GroupList.Count);

            UnknownFieldSet group = field.GroupList[0];

            Assert.AreEqual(1, group.FieldDictionary.Count);
            Assert.IsTrue(group.HasField(nestedFieldDescriptor.FieldNumber));

            UnknownField nestedField = group[nestedFieldDescriptor.FieldNumber];

            Assert.AreEqual(1, nestedField.VarintList.Count);
            Assert.AreEqual(allFields.OptionalGroup.A, (long)nestedField.VarintList[0]);
        }