public static TLVAmqp getTlv(IByteBuffer buf)
        {
            SimpleConstructor constructor = getConstructor(buf);

            TLVAmqp tlv = getElement(constructor, buf);

            return(tlv);
        }
        public void CreateDeterminesPropertiesToCreateByProvidingConstructorArgsForNestedType()
        {
            var buildHistory = new BuildHistory();
            var model        = new SlimModel();
            var expected     = new SimpleConstructor(model);
            var age          = Environment.TickCount;

            var typeCapability     = Substitute.For <IBuildCapability>();
            var valueCapability    = Substitute.For <IBuildCapability>();
            var processor          = Substitute.For <IBuildProcessor>();
            var buildConfiguration = Substitute.For <IBuildConfiguration>();
            var propertyResolver   = Substitute.For <IPropertyResolver>();

            typeCapability.SupportsPopulate.Returns(true);
            typeCapability.AutoPopulate.Returns(true);
            typeCapability.SupportsCreate.Returns(true);
            typeCapability.ImplementedByType.Returns(typeof(DummyTypeCreator));
            valueCapability.SupportsCreate.Returns(true);
            valueCapability.ImplementedByType.Returns(typeof(DummyTypeCreator));

            var sut = new DefaultExecuteStrategy(buildHistory, _buildLog, processor);

            processor.GetBuildCapability(sut, Arg.Any <BuildRequirement>(),
                                         typeof(SimpleConstructor))
            .Returns(typeCapability);
            processor.GetBuildCapability(sut, Arg.Any <BuildRequirement>(),
                                         Arg.Any <PropertyInfo>())
            .Returns(valueCapability);
            processor.GetBuildCapability(sut, Arg.Any <BuildRequirement>(),
                                         typeof(int))
            .Returns(valueCapability);
            typeCapability.CreateType(sut, typeof(SimpleConstructor), Arg.Any <object[]>()).Returns(expected);
            valueCapability.CreateProperty(sut, Arg.Is <PropertyInfo>(x => x.Name == nameof(SimpleConstructor.Model)),
                                           Arg.Any <object[]>()).Returns(model);
            valueCapability.CreateProperty(sut, Arg.Is <PropertyInfo>(x => x.Name == nameof(SimpleConstructor.Age)),
                                           Arg.Any <object[]>()).Returns(age);
            typeCapability.Populate(sut, expected).Returns(expected);
            buildConfiguration.PropertyResolver.Returns(propertyResolver);
            propertyResolver.GetOrderedProperties(buildConfiguration, typeof(SimpleConstructor))
            .Returns(typeof(SimpleConstructor).GetProperties());

            sut.Initialize(buildConfiguration);

            var actual = (SimpleConstructor)sut.Create(typeof(SimpleConstructor), model) !;

            actual.Should().Be(expected);
            actual.Model.Should().Be(model);
            actual.Age.Should().Be(age);
        }
        public TLVArray(AMQPType code, List <TLVAmqp> elements) : base(new SimpleConstructor(code))
        {
            this.elements = elements;
            _width        = code == AMQPType.ARRAY_8 ? 1 : 4;
            _size        += _width;
            foreach (TLVAmqp element in elements)
            {
                _size += element.getLength() - element.Constructor.getLength();
                if (elementConstructor == null && element != null)
                {
                    elementConstructor = element.Constructor;
                }
            }

            _size += elementConstructor.getLength();
            _count = elements.Count;
        }
        public void addElement(TLVAmqp element)
        {
            if (elements.Count == 0)
            {
                elementConstructor = element.Constructor;
                _size += _width;
                _size += elementConstructor.getLength();
            }

            elements.Add(element);
            _count++;
            _size += element.getLength() - elementConstructor.getLength();
            if (_width == 1 && _size > 255)
            {
                Constructor.Code = AMQPType.ARRAY_32;
                _width           = 4;
                _size           += 3;
            }
        }
        private static SimpleConstructor getConstructor(IByteBuffer buf)
        {
            AMQPType          code        = AMQPType.NULL;
            SimpleConstructor constructor = null;
            byte codeByte = buf.ReadByte();

            if (codeByte == 0)
            {
                TLVAmqp descriptor = getTlv(buf);
                code        = (AMQPType)(buf.ReadByte() & 0x0ff);
                constructor = new DescribedConstructor(code, descriptor);
            }
            else
            {
                code        = (AMQPType)(codeByte & 0x0ff);
                constructor = new SimpleConstructor(code);
            }
            return(constructor);
        }
        private static TLVAmqp getElement(SimpleConstructor constructor, IByteBuffer buf)
        {
            TLVAmqp tlv = null;

            AMQPType code = constructor.Code;

            switch (code)
            {
            case AMQPType.NULL:
                tlv = new TLVNull();
                break;

            case AMQPType.BOOLEAN_TRUE:
            case AMQPType.BOOLEAN_FALSE:
            case AMQPType.UINT_0:
            case AMQPType.ULONG_0:
                tlv = new TLVFixed(code, new byte[0]);
                break;

            case AMQPType.BOOLEAN:
            case AMQPType.UBYTE:
            case AMQPType.BYTE:
            case AMQPType.SMALL_UINT:
            case AMQPType.SMALL_INT:
            case AMQPType.SMALL_ULONG:
            case AMQPType.SMALL_LONG:
                byte valueOne = buf.ReadByte();
                tlv = new TLVFixed(code, new byte[] { valueOne });
                break;

            case AMQPType.SHORT:
            case AMQPType.USHORT:
                byte[] valueTwo = new byte[2];
                buf.ReadBytes(valueTwo);
                tlv = new TLVFixed(code, valueTwo);
                break;

            case AMQPType.UINT:
            case AMQPType.INT:
            case AMQPType.FLOAT:
            case AMQPType.DECIMAL_32:
            case AMQPType.CHAR:
                byte[] valueFour = new byte[4];
                buf.ReadBytes(valueFour);
                tlv = new TLVFixed(code, valueFour);
                break;

            case AMQPType.ULONG:
            case AMQPType.LONG:
            case AMQPType.DECIMAL_64:
            case AMQPType.DOUBLE:
            case AMQPType.TIMESTAMP:
                byte[] valueEight = new byte[8];
                buf.ReadBytes(valueEight);
                tlv = new TLVFixed(code, valueEight);
                break;

            case AMQPType.DECIMAL_128:
            case AMQPType.UUID:
                byte[] valueSixteen = new byte[16];
                buf.ReadBytes(valueSixteen);
                tlv = new TLVFixed(code, valueSixteen);
                break;

            case AMQPType.STRING_8:
            case AMQPType.SYMBOL_8:
            case AMQPType.BINARY_8:
                int    var8length = buf.ReadByte() & 0xff;
                byte[] varValue8  = new byte[var8length];
                buf.ReadBytes(varValue8, 0, varValue8.Length);
                tlv = new TLVVariable(code, varValue8);
                break;

            case AMQPType.STRING_32:
            case AMQPType.SYMBOL_32:
            case AMQPType.BINARY_32:
                int    var32length = buf.ReadInt();
                byte[] varValue32  = new byte[var32length];
                buf.ReadBytes(varValue32, 0, varValue32.Length);
                tlv = new TLVVariable(code, varValue32);
                break;

            case AMQPType.LIST_0:
                tlv = new TLVList();
                break;

            case AMQPType.LIST_8:
                int            list8size   = buf.ReadByte() & 0xff;
                int            list8count  = buf.ReadByte() & 0xff;
                List <TLVAmqp> list8values = new List <TLVAmqp>();
                for (int i = 0; i < list8count; i++)
                {
                    list8values.Add(TLVFactory.getTlv(buf));
                }
                tlv = new TLVList(code, list8values);
                break;

            case AMQPType.LIST_32:
                int            list32size   = buf.ReadInt();
                int            list32count  = buf.ReadInt();
                List <TLVAmqp> list32values = new List <TLVAmqp>();
                for (int i = 0; i < list32count; i++)
                {
                    list32values.Add(TLVFactory.getTlv(buf));
                }
                tlv = new TLVList(code, list32values);
                break;

            case AMQPType.MAP_8:
                Dictionary <TLVAmqp, TLVAmqp> map8 = new Dictionary <TLVAmqp, TLVAmqp>();
                int map8size  = buf.ReadByte() & 0xff;
                int map8count = buf.ReadByte() & 0xff;
                int stop8     = buf.ReaderIndex + map8size - 1;
                while (buf.ReaderIndex < stop8)
                {
                    map8[TLVFactory.getTlv(buf)] = TLVFactory.getTlv(buf);
                }
                tlv = new TLVMap(code, map8);
                break;

            case AMQPType.MAP_32:
                Dictionary <TLVAmqp, TLVAmqp> map32 = new Dictionary <TLVAmqp, TLVAmqp>();
                int map32size  = buf.ReadInt();
                int map32count = buf.ReadInt();
                int stop32     = buf.ReaderIndex + map32size - 4;
                while (buf.ReaderIndex < stop32)
                {
                    map32[TLVFactory.getTlv(buf)] = TLVFactory.getTlv(buf);
                }
                tlv = new TLVMap(code, map32);
                break;

            case AMQPType.ARRAY_8:
                List <TLVAmqp>    arr8            = new List <TLVAmqp>();
                int               array8size      = buf.ReadByte() & 0xff;
                int               array8count     = buf.ReadByte() & 0xff;
                SimpleConstructor arr8constructor = getConstructor(buf);
                for (int i = 0; i < array8count; i++)
                {
                    arr8.Add(TLVFactory.getElement(arr8constructor, buf));
                }
                tlv = new TLVArray(code, arr8);
                break;

            case AMQPType.ARRAY_32:
                List <TLVAmqp>    arr32            = new List <TLVAmqp>();
                int               array32size      = buf.ReadInt();
                int               array32count     = buf.ReadInt();
                SimpleConstructor arr32constructor = getConstructor(buf);
                for (int i = 0; i < array32count; i++)
                {
                    arr32.Add(TLVFactory.getElement(arr32constructor, buf));
                }
                tlv = new TLVArray(code, arr32);
                break;

            default:
                break;
            }

            if (constructor is DescribedConstructor)
            {
                tlv.Constructor = constructor;
            }

            return(tlv);
        }
 public TLVAmqp(SimpleConstructor constructor)
 {
     this._constructor = constructor;
 }