Example #1
0
 /// <summary>
 /// Merge the values in <paramref name="other" /> into this field.  For each list
 /// of values, <paramref name="other"/>'s values are append to the ones in this
 /// field.
 /// </summary>
 public Builder MergeFrom(UnknownField other)
 {
     varintList          = AddAll(varintList, other.VarintList);
     fixed32List         = AddAll(fixed32List, other.Fixed32List);
     fixed64List         = AddAll(fixed64List, other.Fixed64List);
     lengthDelimitedList = AddAll(lengthDelimitedList, other.LengthDelimitedList);
     groupList           = AddAll(groupList, other.GroupList);
     return(this);
 }
Example #2
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 PrintUnknownFields()
        {
            TestEmptyMessage message =
                TestEmptyMessage.CreateBuilder()
                .SetUnknownFields(
                    UnknownFieldSet.CreateBuilder()
                    .AddField(5,
                              UnknownField.CreateBuilder()
                              .AddVarint(1)
                              .AddFixed32(2)
                              .AddFixed64(3)
                              .AddLengthDelimited(ByteString.CopyFromUtf8("4"))
                              .AddGroup(
                                  UnknownFieldSet.CreateBuilder()
                                  .AddField(10,
                                            UnknownField.CreateBuilder()
                                            .AddVarint(5)
                                            .Build())
                                  .Build())
                              .Build())
                    .AddField(8,
                              UnknownField.CreateBuilder()
                              .AddVarint(1)
                              .AddVarint(2)
                              .AddVarint(3)
                              .Build())
                    .AddField(15,
                              UnknownField.CreateBuilder()
                              .AddVarint(0xABCDEF1234567890L)
                              .AddFixed32(0xABCD1234)
                              .AddFixed64(0xABCDEF1234567890L)
                              .Build())
                    .Build())
                .Build();

            Assert.AreEqual(
                "5: 1\n" +
                "5: 0x00000002\n" +
                "5: 0x0000000000000003\n" +
                "5: \"4\"\n" +
                "5 {\n" +
                "  10: 5\n" +
                "}\n" +
                "8: 1\n" +
                "8: 2\n" +
                "8: 3\n" +
                "15: 12379813812177893520\n" +
                "15: 0xabcd1234\n" +
                "15: 0xabcdef1234567890\n",
                TextFormat.PrintToString(message));
        }
Example #4
0
 /// <summary>
 /// Adds a field to the set. If a field with the same number already exists, it
 /// is replaced.
 /// </summary>
 public Builder AddField(int number, UnknownField field)
 {
     if (number == 0)
     {
         throw new ArgumentOutOfRangeException("number", "Zero is not a valid field number.");
     }
     if (lastField != null && lastFieldNumber == number)
     {
         // Discard this.
         lastField       = null;
         lastFieldNumber = 0;
     }
     fields[number] = field;
     return(this);
 }
Example #5
0
        public override bool Equals(object other)
        {
            if (ReferenceEquals(this, other))
            {
                return(true);
            }
            UnknownField otherField = other as UnknownField;

            return(otherField != null &&
                   Lists.Equals(varintList, otherField.varintList) &&
                   Lists.Equals(fixed32List, otherField.fixed32List) &&
                   Lists.Equals(fixed64List, otherField.fixed64List) &&
                   Lists.Equals(lengthDelimitedList, otherField.lengthDelimitedList) &&
                   Lists.Equals(groupList, otherField.groupList));
        }
        public void ParseUnknownEnumValue()
        {
            FieldDescriptor singularField =
                TestAllTypes.Descriptor.FindDescriptor <FieldDescriptor>("optional_nested_enum");
            FieldDescriptor repeatedField =
                TestAllTypes.Descriptor.FindDescriptor <FieldDescriptor>("repeated_nested_enum");

            Assert.IsNotNull(singularField);
            Assert.IsNotNull(repeatedField);

            ByteString data =
                UnknownFieldSet.CreateBuilder()
                .AddField(singularField.FieldNumber,
                          UnknownField.CreateBuilder()
                          .AddVarint((int)TestAllTypes.Types.NestedEnum.BAR)
                          .AddVarint(5)         // not valid
                          .Build())
                .AddField(repeatedField.FieldNumber,
                          UnknownField.CreateBuilder()
                          .AddVarint((int)TestAllTypes.Types.NestedEnum.FOO)
                          .AddVarint(4)         // not valid
                          .AddVarint((int)TestAllTypes.Types.NestedEnum.BAZ)
                          .AddVarint(6)         // not valid
                          .Build())
                .Build()
                .ToByteString();

            {
                TestAllTypes message = TestAllTypes.ParseFrom(data);
                Assert.AreEqual(TestAllTypes.Types.NestedEnum.BAR,
                                message.OptionalNestedEnum);
                TestUtil.AssertEqual(new[] { TestAllTypes.Types.NestedEnum.FOO, TestAllTypes.Types.NestedEnum.BAZ },
                                     message.RepeatedNestedEnumList);
                TestUtil.AssertEqual(new[] { 5UL }, message.UnknownFields[singularField.FieldNumber].VarintList);
                TestUtil.AssertEqual(new[] { 4UL, 6UL }, message.UnknownFields[repeatedField.FieldNumber].VarintList);
            }

            {
                TestAllExtensions message =
                    TestAllExtensions.ParseFrom(data, TestUtil.CreateExtensionRegistry());
                Assert.AreEqual(TestAllTypes.Types.NestedEnum.BAR,
                                message.GetExtension(UnitTestProtoFile.OptionalNestedEnumExtension));
                TestUtil.AssertEqual(new[] { TestAllTypes.Types.NestedEnum.FOO, TestAllTypes.Types.NestedEnum.BAZ },
                                     message.GetExtension(UnitTestProtoFile.RepeatedNestedEnumExtension));
                TestUtil.AssertEqual(new[] { 5UL }, message.UnknownFields[singularField.FieldNumber].VarintList);
                TestUtil.AssertEqual(new[] { 4UL, 6UL }, message.UnknownFields[repeatedField.FieldNumber].VarintList);
            }
        }
        public void LargeVarint()
        {
            ByteString data =
                UnknownFieldSet.CreateBuilder()
                .AddField(1,
                          UnknownField.CreateBuilder()
                          .AddVarint(0x7FFFFFFFFFFFFFFFL)
                          .Build())
                .Build()
                .ToByteString();
            UnknownFieldSet parsed = UnknownFieldSet.ParseFrom(data);
            UnknownField    field  = parsed[1];

            Assert.AreEqual(1, field.VarintList.Count);
            Assert.AreEqual(0x7FFFFFFFFFFFFFFFUL, field.VarintList[0]);
        }
Example #8
0
 /// <summary>
 /// Adds a field to the unknown field set. If a field with the same
 /// number already exists, the two are merged.
 /// </summary>
 public Builder MergeField(int number, UnknownField field)
 {
     if (number == 0)
     {
         throw new ArgumentOutOfRangeException("number", "Zero is not a valid field number.");
     }
     if (HasField(number))
     {
         GetFieldBuilder(number).MergeFrom(field);
     }
     else
     {
         // Optimization:  We could call getFieldBuilder(number).mergeFrom(field)
         // in this case, but that would create a copy of the Field object.
         // We'd rather reuse the one passed to us, so call AddField() instead.
         AddField(number, field);
     }
     return(this);
 }
Example #9
0
        private static void PrintUnknownFields(UnknownFieldSet unknownFields, TextGenerator generator)
        {
            foreach (KeyValuePair <int, UnknownField> entry in unknownFields.FieldDictionary)
            {
                String       prefix = entry.Key.ToString() + ": ";
                UnknownField field  = entry.Value;

                foreach (ulong value in field.VarintList)
                {
                    generator.Print(prefix);
                    generator.Print(value.ToString());
                    generator.Print("\n");
                }
                foreach (uint value in field.Fixed32List)
                {
                    generator.Print(prefix);
                    generator.Print(string.Format("0x{0:x8}", value));
                    generator.Print("\n");
                }
                foreach (ulong value in field.Fixed64List)
                {
                    generator.Print(prefix);
                    generator.Print(string.Format("0x{0:x16}", value));
                    generator.Print("\n");
                }
                foreach (ByteString value in field.LengthDelimitedList)
                {
                    generator.Print(entry.Key.ToString());
                    generator.Print(": \"");
                    generator.Print(EscapeBytes(value));
                    generator.Print("\"\n");
                }
                foreach (UnknownFieldSet value in field.GroupList)
                {
                    generator.Print(entry.Key.ToString());
                    generator.Print(" {\n");
                    generator.Indent();
                    PrintUnknownFields(value, generator);
                    generator.Outdent();
                    generator.Print("}\n");
                }
            }
        }
Example #10
0
        public void SerializeMessageSet()
        {
            // Set up a TestMessageSet with two known messages and an unknown one.
            TestMessageSet messageSet =
                TestMessageSet.CreateBuilder()
                .SetExtension(
                    TestMessageSetExtension1.MessageSetExtension,
                    TestMessageSetExtension1.CreateBuilder().SetI(123).Build())
                .SetExtension(
                    TestMessageSetExtension2.MessageSetExtension,
                    TestMessageSetExtension2.CreateBuilder().SetStr("foo").Build())
                .SetUnknownFields(
                    UnknownFieldSet.CreateBuilder()
                    .AddField(UnknownTypeId,
                              UnknownField.CreateBuilder()
                              .AddLengthDelimited(ByteString.CopyFromUtf8("bar"))
                              .Build())
                    .Build())
                .Build();

            ByteString data = messageSet.ToByteString();

            // Parse back using RawMessageSet and check the contents.
            RawMessageSet raw = RawMessageSet.ParseFrom(data);

            Assert.AreEqual(0, raw.UnknownFields.FieldDictionary.Count);

            Assert.AreEqual(3, raw.ItemCount);
            Assert.AreEqual(TypeId1, raw.ItemList[0].TypeId);
            Assert.AreEqual(TypeId2, raw.ItemList[1].TypeId);
            Assert.AreEqual(UnknownTypeId, raw.ItemList[2].TypeId);

            TestMessageSetExtension1 message1 = TestMessageSetExtension1.ParseFrom(raw.GetItem(0).Message.ToByteArray());

            Assert.AreEqual(123, message1.I);

            TestMessageSetExtension2 message2 = TestMessageSetExtension2.ParseFrom(raw.GetItem(1).Message.ToByteArray());

            Assert.AreEqual("foo", message2.Str);

            Assert.AreEqual("bar", raw.GetItem(2).Message.ToStringUtf8());
        }
        public void ParseKnownAndUnknown()
        {
            // Test mixing known and unknown fields when parsing.

            UnknownFieldSet fields =
                UnknownFieldSet.CreateBuilder(unknownFields)
                .AddField(123456,
                          UnknownField.CreateBuilder().AddVarint(654321).Build())
                .Build();

            ByteString   data        = fields.ToByteString();
            TestAllTypes destination = TestAllTypes.ParseFrom(data);

            TestUtil.AssertAllFieldsSet(destination);
            Assert.AreEqual(1, destination.UnknownFields.FieldDictionary.Count);

            UnknownField field = destination.UnknownFields[123456];

            Assert.AreEqual(1, field.VarintList.Count);
            Assert.AreEqual(654321, (long)field.VarintList[0]);
        }
        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]);
        }
        /// <summary>
        /// Constructs a protocol buffer which contains fields with all the same
        /// numbers as allFieldsData except that each field is some other wire
        /// type.
        /// </summary>
        private ByteString GetBizarroData()
        {
            UnknownFieldSet.Builder bizarroFields = UnknownFieldSet.CreateBuilder();

            UnknownField varintField  = UnknownField.CreateBuilder().AddVarint(1).Build();
            UnknownField fixed32Field = UnknownField.CreateBuilder().AddFixed32(1).Build();

            foreach (KeyValuePair <int, UnknownField> entry in unknownFields.FieldDictionary)
            {
                if (entry.Value.VarintList.Count == 0)
                {
                    // Original field is not a varint, so use a varint.
                    bizarroFields.AddField(entry.Key, varintField);
                }
                else
                {
                    // Original field *is* a varint, so use something else.
                    bizarroFields.AddField(entry.Key, fixed32Field);
                }
            }

            return(bizarroFields.Build().ToByteString());
        }
Example #14
0
        public void ToBuilder()
        {
            DynamicMessage.Builder builder =
                DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
            reflectionTester.SetAllFieldsViaReflection(builder);
            int   unknownFieldNum = 9;
            ulong unknownFieldVal = 90;

            builder.SetUnknownFields(UnknownFieldSet.CreateBuilder()
                                     .AddField(unknownFieldNum,
                                               UnknownField.CreateBuilder().AddVarint(unknownFieldVal).Build())
                                     .Build());
            DynamicMessage message = builder.Build();

            DynamicMessage derived = message.ToBuilder().Build();

            reflectionTester.AssertAllFieldsSetViaReflection(derived);

            IList <ulong> values = derived.UnknownFields.FieldDictionary[unknownFieldNum].VarintList;

            Assert.AreEqual(1, values.Count);
            Assert.AreEqual(unknownFieldVal, values[0]);
        }
Example #15
0
            /// <summary>
            /// Called by MergeFieldFrom to parse a MessageSet extension.
            /// </summary>
            private void MergeMessageSetExtensionFromCodedStream(ICodedInputStream input,
                                                                 ExtensionRegistry extensionRegistry, IBuilder builder)
            {
                MessageDescriptor type = builder.DescriptorForType;

                // The wire format for MessageSet is:
                //   message MessageSet {
                //     repeated group Item = 1 {
                //       required int32 typeId = 2;
                //       required bytes message = 3;
                //     }
                //   }
                // "typeId" is the extension's field number.  The extension can only be
                // a message type, where "message" contains the encoded bytes of that
                // message.
                //
                // In practice, we will probably never see a MessageSet item in which
                // the message appears before the type ID, or where either field does not
                // appear exactly once.  However, in theory such cases are valid, so we
                // should be prepared to accept them.

                int             typeId     = 0;
                ByteString      rawBytes   = null; // If we encounter "message" before "typeId"
                IBuilderLite    subBuilder = null;
                FieldDescriptor field      = null;

                uint   lastTag = WireFormat.MessageSetTag.ItemStart;
                uint   tag;
                string name;

                while (input.ReadTag(out tag, out name))
                {
                    if (tag == 0 && name != null)
                    {
                        if (name == "type_id")
                        {
                            tag = WireFormat.MessageSetTag.TypeID;
                        }
                        else if (name == "message")
                        {
                            tag = WireFormat.MessageSetTag.Message;
                        }
                    }
                    if (tag == 0)
                    {
                        if (input.SkipField())
                        {
                            continue; //can't merge unknown without field tag
                        }
                        break;
                    }

                    lastTag = tag;
                    if (tag == WireFormat.MessageSetTag.TypeID)
                    {
                        typeId = 0;
                        // Zero is not a valid type ID.
                        if (input.ReadInt32(ref typeId) && typeId != 0)
                        {
                            ExtensionInfo extension = extensionRegistry[type, typeId];
                            if (extension != null)
                            {
                                field      = extension.Descriptor;
                                subBuilder = extension.DefaultInstance.WeakCreateBuilderForType();
                                IMessageLite originalMessage = (IMessageLite)builder[field];
                                if (originalMessage != null)
                                {
                                    subBuilder.WeakMergeFrom(originalMessage);
                                }
                                if (rawBytes != null)
                                {
                                    // We already encountered the message.  Parse it now.
                                    // TODO(jonskeet): Check this is okay. It's subtly different from the Java, as it doesn't create an input stream from rawBytes.
                                    // In fact, why don't we just call MergeFrom(rawBytes)? And what about the extension registry?
                                    subBuilder.WeakMergeFrom(rawBytes.CreateCodedInput());
                                    rawBytes = null;
                                }
                            }
                            else
                            {
                                // Unknown extension number.  If we already saw data, put it
                                // in rawBytes.
                                if (rawBytes != null)
                                {
                                    MergeField(typeId, UnknownField.CreateBuilder().AddLengthDelimited(rawBytes).Build());
                                    rawBytes = null;
                                }
                            }
                        }
                    }
                    else if (tag == WireFormat.MessageSetTag.Message)
                    {
                        if (subBuilder != null)
                        {
                            // We already know the type, so we can parse directly from the input
                            // with no copying.  Hooray!
                            input.ReadMessage(subBuilder, extensionRegistry);
                        }
                        else if (input.ReadBytes(ref rawBytes))
                        {
                            if (typeId != 0)
                            {
                                // We don't know how to parse this.  Ignore it.
                                MergeField(typeId,
                                           UnknownField.CreateBuilder().AddLengthDelimited(rawBytes).Build());
                            }
                        }
                    }
                    else
                    {
                        // Unknown tag.  Skip it.
                        if (!input.SkipField())
                        {
                            break; // end of group
                        }
                    }
                }

                if (lastTag != WireFormat.MessageSetTag.ItemEnd)
                {
                    throw InvalidProtocolBufferException.InvalidEndTag();
                }

                if (subBuilder != null)
                {
                    builder[field] = subBuilder.WeakBuild();
                }
            }
Example #16
0
 /// <summary>
 /// Constructs a new Builder and initializes it to a copy of <paramref name="copyFrom"/>.
 /// </summary>
 public static Builder CreateBuilder(UnknownField copyFrom)
 {
     return(new Builder().MergeFrom(copyFrom));
 }