Ejemplo n.º 1
0
        public object Decode(byte[] buffer)
        {
            var byteString = ByteString.CopyFrom(buffer);

            return(_extensions == null
                 ? _prototype.WeakToBuilder().WeakMergeFrom(byteString).WeakBuild()
                 : _prototype.WeakToBuilder().WeakMergeFrom(byteString, _extensions).WeakBuild());
        }
Ejemplo n.º 2
0
        private void MergeField(IFieldDescriptorLite field, object mergeValue)
        {
            object existingValue;

            fields.TryGetValue(field, out existingValue);
            if (field.IsRepeated)
            {
                if (existingValue == null)
                {
                    existingValue = new List <object>();
                    fields[field] = existingValue;
                }
                IList <object> list = (IList <object>)existingValue;
                foreach (object otherValue in (IEnumerable)mergeValue)
                {
                    list.Add(otherValue);
                }
            }
            else if (field.MappedType == MappedType.Message && existingValue != null)
            {
                IMessageLite existingMessage = (IMessageLite)existingValue;
                IMessageLite merged          = existingMessage.WeakToBuilder()
                                               .WeakMergeFrom((IMessageLite)mergeValue)
                                               .WeakBuild();
                this[field] = merged;
            }
            else
            {
                this[field] = mergeValue;
            }
        }
Ejemplo n.º 3
0
        public void TestMessageLiteWeakToBuilder()
        {
            IMessageLite msg = TestRequiredLite.CreateBuilder()
                               .SetD(42)
                               .SetEn(ExtraEnum.EXLITE_BAZ)
                               .Build();

            IMessageLite copy = msg.WeakToBuilder().WeakBuild();

            Assert.AreEqual(msg.ToByteArray(), copy.ToByteArray());
        }
Ejemplo n.º 4
0
        private void MergeField(IFieldDescriptorLite field, object mergeValue)
        {
            object obj;

            this.fields.TryGetValue(field, out obj);
            if (field.IsRepeated)
            {
                if (obj == null)
                {
                    obj = new List <object>();
                    this.fields[field] = obj;
                }
                IList <object> list       = (IList <object>)obj;
                IEnumerator    enumerator = ((IEnumerable)mergeValue).GetEnumerator();
                try
                {
                    while (enumerator.MoveNext())
                    {
                        object current = enumerator.Current;
                        list.Add(current);
                    }
                    return;
                }
                finally
                {
                    IDisposable disposable = enumerator as IDisposable;
                    if (disposable != null)
                    {
                        disposable.Dispose();
                    }
                }
            }
            if (field.MappedType == MappedType.Message && obj != null)
            {
                IMessageLite messageLite = (IMessageLite)obj;
                IMessageLite value       = messageLite.WeakToBuilder().WeakMergeFrom((IMessageLite)mergeValue).WeakBuild();
                this[field] = value;
                return;
            }
            this[field] = mergeValue;
        }
Ejemplo n.º 5
0
        protected override bool ParseUnknownField(CodedInputStream input,
                                                  ExtensionRegistry extensionRegistry, uint tag)
        {
            FieldSet extensions = MessageBeingBuilt.Extensions;

            WireFormat.WireType wireType = WireFormat.GetTagWireType(tag);
            int fieldNumber = WireFormat.GetTagFieldNumber(tag);
            IGeneratedExtensionLite extension = extensionRegistry[DefaultInstanceForType, fieldNumber];

            bool unknown = false;
            bool packed  = false;

            if (extension == null)
            {
                unknown = true; // Unknown field.
            }
            else if (wireType == FieldMappingAttribute.WireTypeFromFieldType(extension.Descriptor.FieldType, false /* isPacked */))
            {
                packed = false; // Normal, unpacked value.
            }
            else if (extension.Descriptor.IsRepeated &&
                     //?? just returns true ?? extension.Descriptor.type.isPackable() &&
                     wireType == FieldMappingAttribute.WireTypeFromFieldType(extension.Descriptor.FieldType, true /* isPacked */))
            {
                packed = true; // Packed value.
            }
            else
            {
                unknown = true; // Wrong wire type.
            }

            if (unknown) // Unknown field or wrong wire type.  Skip.
            {
                return(input.SkipField(tag));
            }

            if (packed)
            {
                int length = (int)Math.Min(int.MaxValue, input.ReadRawVarint32());
                int limit  = input.PushLimit(length);
                if (extension.Descriptor.FieldType == FieldType.Enum)
                {
                    while (!input.ReachedLimit)
                    {
                        int    rawValue = input.ReadEnum();
                        Object value    =
                            extension.Descriptor.EnumType.FindValueByNumber(rawValue);
                        if (value == null)
                        {
                            // If the number isn't recognized as a valid value for this
                            // enum, drop it (don't even add it to unknownFields).
                            return(true);
                        }
                        extensions.AddRepeatedField(extension.Descriptor, value);
                    }
                }
                else
                {
                    while (!input.ReachedLimit)
                    {
                        Object value = input.ReadPrimitiveField(extension.Descriptor.FieldType);
                        extensions.AddRepeatedField(extension.Descriptor, value);
                    }
                }
                input.PopLimit(limit);
            }
            else
            {
                Object value;
                switch (extension.Descriptor.MappedType)
                {
                case MappedType.Message: {
                    IBuilderLite subBuilder = null;
                    if (!extension.Descriptor.IsRepeated)
                    {
                        IMessageLite existingValue = extensions[extension.Descriptor] as IMessageLite;
                        if (existingValue != null)
                        {
                            subBuilder = existingValue.WeakToBuilder();
                        }
                    }
                    if (subBuilder == null)
                    {
                        subBuilder = extension.MessageDefaultInstance.WeakCreateBuilderForType();
                    }
                    if (extension.Descriptor.FieldType == FieldType.Group)
                    {
                        input.ReadGroup(extension.Number, subBuilder, extensionRegistry);
                    }
                    else
                    {
                        input.ReadMessage(subBuilder, extensionRegistry);
                    }
                    value = subBuilder.WeakBuild();
                    break;
                }

                case MappedType.Enum:
                    int rawValue = input.ReadEnum();
                    value = extension.Descriptor.EnumType.FindValueByNumber(rawValue);
                    // If the number isn't recognized as a valid value for this enum,
                    // drop it.
                    if (value == null)
                    {
                        return(true);
                    }
                    break;

                default:
                    value = input.ReadPrimitiveField(extension.Descriptor.FieldType);
                    break;
                }

                if (extension.Descriptor.IsRepeated)
                {
                    extensions.AddRepeatedField(extension.Descriptor, value);
                }
                else
                {
                    extensions[extension.Descriptor] = value;
                }
            }

            return(true);
        }