public void MapByBytesExpression()
        {
            var mapperFactory = new MapperFactoryConfig()
                                .DefaultDelimiter(null)
                                .DefaultFiller(0x30)
                                .CreateMapByExpression <BytesAttributeObject>(8, config => config
                                                                              .ForMember(x => x.BytesValue, m => m.Bytes(4))
                                                                              .ForMember(x => x.CustomBytesValue, m => m.Bytes(4).Filler(0x30)))
                                .ToMapperFactory();
            var mapper = mapperFactory.Create <BytesAttributeObject>();

            var buffer = new byte[mapper.Size];
            var obj    = new BytesAttributeObject
            {
                BytesValue = new byte[] { 0x01, 0x02, 0x03, 0x04 }
            };

            // Write
            mapper.ToByte(buffer, 0, obj);

            Assert.Equal(new byte[] { 0x01, 0x02, 0x03, 0x04, 0x30, 0x30, 0x30, 0x30 }, buffer);

            // Read
            for (var i = 0; i < buffer.Length; i++)
            {
                buffer[i] = 0xff;
            }

            mapper.FromByte(buffer, 0, obj);

            Assert.Equal(new byte[] { 0xff, 0xff, 0xff, 0xff }, obj.BytesValue);
        }
        public void MapByTextExpression()
        {
            var mapperFactory = new MapperFactoryConfig()
                                .DefaultDelimiter(null)
                                .DefaultEncoding(Encoding.ASCII)
                                .DefaultTrim(true)
                                .DefaultTextPadding(Padding.Right)
                                .DefaultTextFiller(0x20)
                                .CreateMapByExpression <TextExpressionObject>(8, config => config
                                                                              .ForMember(x => x.StringValue, m => m.Text(4))
                                                                              .ForMember(x => x.CustomStringValue, m => m.Text(4).Encoding(Encoding.ASCII).Trim(false).Padding(Padding.Left).Filler((byte)'_')))
                                .ToMapperFactory();
            var mapper = mapperFactory.Create <TextExpressionObject>();

            var buffer = new byte[mapper.Size];
            var obj    = new TextExpressionObject();

            // Write
            mapper.ToByte(buffer, 0, obj);

            Assert.Equal(Encoding.ASCII.GetBytes("    ____"), buffer);

            // Read
            mapper.FromByte(Encoding.ASCII.GetBytes("12  __AB"), 0, obj);

            Assert.Equal("12", obj.StringValue);
            Assert.Equal("__AB", obj.CustomStringValue);
        }
        public void MapByUnicodeAttribute()
        {
            var mapperFactory = new MapperFactoryConfig()
                                .UseOptionsDefault()
                                .DefaultDelimiter(null)
                                .DefaultTrim(true)
                                .DefaultTextPadding(Padding.Right)
                                .DefaultUnicodeFiller(' ')
                                .CreateMapByAttribute <UnicodeAttributeObject>()
                                .ToMapperFactory();
            var mapper = mapperFactory.Create <UnicodeAttributeObject>();

            var buffer = new byte[mapper.Size];
            var obj    = new UnicodeAttributeObject();

            // Write
            mapper.ToByte(buffer, 0, obj);

            Assert.Equal(Encoding.Unicode.GetBytes("  __"), buffer);

            // Read
            mapper.FromByte(Encoding.Unicode.GetBytes("1 _A"), 0, obj);

            Assert.Equal("1", obj.StringValue);
            Assert.Equal("_A", obj.CustomStringValue);
        }
Example #4
0
        public void CoverageFix()
        {
            // Map
            Assert.Throws <ArgumentOutOfRangeException>(() =>
                                                        new MapperFactoryConfig().CreateMapByExpression <DummyObject>(0, c => c.Constant(-1, Array.Empty <byte>())));

            // ForMember
            Assert.Throws <ArgumentOutOfRangeException>(() =>
                                                        new MapperFactoryConfig().CreateMapByExpression <DummyObject>(0, c => c.ForMember("x", -1, null)));
            Assert.Throws <ArgumentNullException>(() =>
                                                  new MapperFactoryConfig().CreateMapByExpression <DummyObject>(0, c => c.ForMember("x", null)));
            Assert.Throws <ArgumentException>(() =>
                                              new MapperFactoryConfig().CreateMapByExpression <DummyObject>(0, c => c.ForMember("x", m => { })));
            Assert.Throws <InvalidOperationException>(() =>
                                                      new MapperFactoryConfig().CreateMapByExpression <DummyObject>(0, c => c.ForMember(x => x.IntValue, m => { })));
            Assert.Throws <ByteMapperException>(() =>
                                                new MapperFactoryConfig().CreateMapByExpression <DummyObject>(0, c => c.ForMember(x => x.IntValue, m => m.Boolean())));

            var mapperFactory = new MapperFactoryConfig()
                                .DefaultDelimiter(null)
                                .Also(config =>
            {
                config.CreateMapByExpression <DummyObject>(5, c => c
                                                           .ForMember(nameof(DummyObject.IntValue), 0, m => m.Binary())
                                                           .ForMember(nameof(DummyObject.BoolValue), m => m.Boolean()));
            })
                                .ToMapperFactory();

            Assert.NotNull(mapperFactory.Create <DummyObject>());
        }
        public void MapByByteExpression()
        {
            var mapperFactory = new MapperFactoryConfig()
                                .DefaultDelimiter(null)
                                .CreateMapByExpression <ByteExpressionObject>(1, config => config
                                                                              .ForMember(x => x.ByteValue, m => m.Byte()))
                                .ToMapperFactory();
            var mapper = mapperFactory.Create <ByteExpressionObject>();

            var buffer = new byte[mapper.Size];
            var obj    = new ByteExpressionObject
            {
                ByteValue = 1,
            };

            // Write
            mapper.ToByte(buffer, 0, obj);

            Assert.Equal(new byte[] { 0x01 }, buffer);

            // Read
            buffer[0] = 0x02;

            mapper.FromByte(buffer, 0, obj);

            Assert.Equal(2, obj.ByteValue);
        }
        public void MapByUnicodeExpression()
        {
            var mapperFactory = new MapperFactoryConfig()
                                .UseOptionsDefault()
                                .DefaultDelimiter(null)
                                .DefaultTrim(true)
                                .DefaultTextPadding(Padding.Right)
                                .DefaultUnicodeFiller(' ')
                                .CreateMapByExpression <UnicodeExpressionObject>(8, config => config
                                                                                 .ForMember(
                                                                                     x => x.StringValue,
                                                                                     m => m.Unicode(4))
                                                                                 .ForMember(
                                                                                     x => x.CustomStringValue,
                                                                                     m => m.Unicode(4).Trim(false).Padding(Padding.Left).Filler('_')))
                                .ToMapperFactory();
            var mapper = mapperFactory.Create <UnicodeExpressionObject>();

            var buffer = new byte[mapper.Size];
            var obj    = new UnicodeExpressionObject();

            // Write
            mapper.ToByte(buffer, 0, obj);

            Assert.Equal(Encoding.Unicode.GetBytes("  __"), buffer);

            // Read
            mapper.FromByte(Encoding.Unicode.GetBytes("1 _A"), 0, obj);

            Assert.Equal("1", obj.StringValue);
            Assert.Equal("_A", obj.CustomStringValue);
        }
Example #7
0
        public void MapByBooleanAttribute()
        {
            var mapperFactory = new MapperFactoryConfig()
                                .DefaultDelimiter(null)
                                .DefaultFiller(Filler)
                                .DefaultTrueValue(True)
                                .DefaultFalseValue(False)
                                .CreateMapByAttribute <BooleanAttributeObject>()
                                .ToMapperFactory();
            var mapper = mapperFactory.Create <BooleanAttributeObject>();

            var buffer = new byte[mapper.Size];
            var obj    = new BooleanAttributeObject();

            // Write
            mapper.ToByte(buffer, 0, obj);

            Assert.Equal(new[] { False, Filler, No, Filler }, buffer);

            // Read
            mapper.FromByte(new[] { True, True, Yes, Yes }, 0, obj);

            Assert.True(obj.BooleanValue);
            Assert.True(obj.NullableBooleanValue);
            Assert.True(obj.CustomBooleanValue);
            Assert.True(obj.CustomNullableBooleanValue);

            // Read default
            mapper.FromByte(new[] { Filler, Filler, Filler, Filler }, 0, obj);

            Assert.False(obj.BooleanValue);
            Assert.Null(obj.NullableBooleanValue);
            Assert.False(obj.CustomBooleanValue);
            Assert.Null(obj.CustomNullableBooleanValue);
        }
        public void MapByStringAttribute()
        {
            var mapperFactory = new MapperFactoryConfig()
                                .DefaultDelimiter(null)
                                .DefaultEncoding(Encoding.ASCII)
                                .DefaultTrim(true)
                                .DefaultTextPadding(Padding.Right)
                                .DefaultTextFiller(0x20)
                                .CreateMapByAttribute <TextAttributeObject>()
                                .ToMapperFactory();
            var mapper = mapperFactory.Create <TextAttributeObject>();

            var buffer = new byte[mapper.Size];
            var obj    = new TextAttributeObject();

            // Write
            mapper.ToByte(buffer, 0, obj);

            Assert.Equal(Encoding.ASCII.GetBytes("    ______"), buffer);

            // Read
            mapper.FromByte(Encoding.ASCII.GetBytes("12  __AB*_"), 0, obj);

            Assert.Equal("12", obj.StringValue);
            Assert.Equal("__AB", obj.CustomStringValue);
            Assert.Equal("*", obj.CustomStringValue2);
        }
        public void MapByByteAttribute()
        {
            var mapperFactory = new MapperFactoryConfig()
                                .DefaultDelimiter(null)
                                .CreateMapByAttribute <ByteAttributeObject>()
                                .ToMapperFactory();
            var mapper = mapperFactory.Create <ByteAttributeObject>();

            var buffer = new byte[mapper.Size];
            var obj    = new ByteAttributeObject
            {
                ByteValue = 1,
            };

            // Write
            mapper.ToByte(buffer, 0, obj);

            Assert.Equal(new byte[] { 0x01 }, buffer);

            // Read
            buffer[0] = 0x02;

            mapper.FromByte(buffer, 0, obj);

            Assert.Equal(2, obj.ByteValue);
        }
        public void Setup()
        {
            var mapperFactory = new MapperFactoryConfig()
                                .CreateMapByAttribute <BinaryObject>()
                                .ToMapperFactory();

            mapper = mapperFactory.Create <BinaryObject>();
        }
 public static MapperFactoryConfig UseOptionsDefault(this MapperFactoryConfig config)
 {
     config.DefaultNumberPadding(Padding.Left);
     config.DefaultZeroFill(false);
     config.DefaultUseGrouping(false);
     config.DefaultNumberFiller(0x20);
     return(config);
 }
        public void MapByArrayExpression()
        {
            var mapperFactory = new MapperFactoryConfig()
                                .DefaultDelimiter(null)
                                .DefaultFiller(0x00)
                                .DefaultEndian(Endian.Big)
                                .CreateMapByExpression <ArrayExpressionObject>(19, config => config
                                                                               .ForMember(x => x.ArrayValue, m => m.Array(3, e => e.Binary()))
                                                                               .ForMember(x => x.ByteArrayValue, m => m.Array(7, e => e.Byte()).Filler(0xFF)))
                                .ToMapperFactory();
            var mapper = mapperFactory.Create <ArrayExpressionObject>();

            var buffer = new byte[mapper.Size];
            var obj    = new ArrayExpressionObject();

            // Write
            mapper.ToByte(buffer, 0, obj);

            Assert.Equal(
                new byte[]
            {
                0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00,
                0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
            },
                buffer);

            // Write
            obj.ArrayValue     = new[] { 1, 2, 3 };
            obj.ByteArrayValue = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };

            mapper.ToByte(buffer, 0, obj);

            Assert.Equal(
                new byte[]
            {
                0x00, 0x00, 0x00, 0x01,
                0x00, 0x00, 0x00, 0x02,
                0x00, 0x00, 0x00, 0x03,
                0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
            },
                buffer);

            // Read
            for (var i = 0; i < buffer.Length; i++)
            {
                if (buffer[i] > 0)
                {
                    buffer[i]++;
                }
            }

            mapper.FromByte(buffer, 0, obj);

            Assert.Equal(new[] { 2, 3, 4 }, obj.ArrayValue);
            Assert.Equal(new byte[] { 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }, obj.ByteArrayValue);
        }
        public void MapByDecimalAttribute()
        {
            var mapperFactory = new MapperFactoryConfig()
                                .UseOptionsDefault()
                                .DefaultDelimiter(null)
                                .DefaultZeroFill(false)
                                .DefaultNumberPadding(Padding.Left)
                                .DefaultNumberFiller(0x20)
                                .CreateMapByAttribute <DecimalAttributeObject>()
                                .ToMapperFactory();
            var mapper = mapperFactory.Create <DecimalAttributeObject>();

            var buffer = new byte[mapper.Size];
            var obj    = new DecimalAttributeObject
            {
                DecimalValue             = 1234.5m,
                PaddingRightDecimalValue = -1m,
                ZeroFillDecimalValue     = 1m
            };

            // Write
            mapper.ToByte(buffer, 0, obj);
            Assert.Equal(
                Encoding.ASCII.GetBytes(
                    "  1,234.50" +
                    "______" +
                    "-1    " +
                    "000001"),
                buffer);

            // Read
            mapper.FromByte(
                Encoding.ASCII.GetBytes(
                    "   2,345.6" +
                    "_____2" +
                    "-2    " +
                    "000002"),
                obj);

            Assert.Equal(2345.60m, obj.DecimalValue);
            Assert.Equal(2m, obj.NullableDecimalValue);
            Assert.Equal(-2m, obj.PaddingRightDecimalValue);
            Assert.Equal(2m, obj.ZeroFillDecimalValue);

            mapper.FromByte(
                Encoding.ASCII.GetBytes(
                    "          " +
                    "______" +
                    "      " +
                    "000000"),
                obj);

            Assert.Equal(0m, obj.DecimalValue);
            Assert.Null(obj.NullableDecimalValue);
            Assert.Equal(0m, obj.PaddingRightDecimalValue);
            Assert.Equal(0m, obj.ZeroFillDecimalValue);
        }
Example #14
0
        public void MapByDateTimeTextAttribute()
        {
            var mapperFactory = new MapperFactoryConfig()
                                .DefaultDelimiter(null)
                                .DefaultTrim(true)
                                .DefaultTextFiller(0x20)
                                .DefaultDateTimeTextEncoding(Encoding.ASCII)
                                .DefaultDateTimeTextProvider(CultureInfo.InvariantCulture)
                                .DefaultDateTimeTextStyle(DateTimeStyles.None)
                                .CreateMapByAttribute <DateTimeTextAttributeObject>()
                                .ToMapperFactory();
            var mapper = mapperFactory.Create <DateTimeTextAttributeObject>();

            var buffer = new byte[mapper.Size];
            var obj    = new DateTimeTextAttributeObject
            {
                DateTimeValue       = new DateTime(2000, 12, 31, 0, 0, 0),
                DateTimeOffsetValue = new DateTimeOffset(new DateTime(2000, 12, 31, 0, 0, 0))
            };

            // Write
            mapper.ToByte(buffer, 0, obj);

            Assert.Equal(
                Encoding.ASCII.GetBytes(
                    "20001231" +
                    "        " +
                    "______________" +
                    "20001231" +
                    "        " +
                    "______________"),
                buffer);

            // Read
            mapper.FromByte(buffer, obj);

            mapper.FromByte(
                Encoding.ASCII.GetBytes(
                    "20010101" +
                    "20010101" +
                    "20001231235959" +
                    "20010101" +
                    "20010101" +
                    "20001231235959"),
                obj);

            Assert.Equal(new DateTime(2001, 1, 1, 0, 0, 0), obj.DateTimeValue);
            Assert.Equal(new DateTime(2001, 1, 1, 0, 0, 0), obj.NullableDateTimeValue);
            Assert.Equal(new DateTime(2000, 12, 31, 23, 59, 59), obj.CustomDateTimeValue);
            Assert.Equal(new DateTimeOffset(new DateTime(2001, 1, 1, 0, 0, 0)), obj.DateTimeOffsetValue);
            Assert.Equal(new DateTimeOffset(new DateTime(2001, 1, 1, 0, 0, 0)), obj.NullableDateTimeOffsetValue);
            Assert.Equal(new DateTimeOffset(new DateTime(2000, 12, 31, 23, 59, 59)), obj.CustomDateTimeOffsetValue);
        }
        private static IBuilderContext CreateBuilderContext()
        {
            var config = new MapperFactoryConfig()
                         .UseOptionsDefault();

            config.DefaultEncoding(SjisEncoding.Instance);

            return(new BuilderContext(
                       ((IMapperFactoryConfig)config).ResolveComponents(),
                       ((IMapperFactoryConfig)config).ResolveParameters(),
                       new Dictionary <string, object>()));
        }
        public void MapByDateTimeAttribute()
        {
            var mapperFactory = new MapperFactoryConfig()
                                .UseOptionsDefault()
                                .DefaultDelimiter(null)
                                .DefaultTextFiller(0x20)
                                .DefaultDateTimeKind(DateTimeKind.Unspecified)
                                .CreateMapByAttribute <DateTimeAttributeObject>()
                                .ToMapperFactory();
            var mapper = mapperFactory.Create <DateTimeAttributeObject>();

            var buffer = new byte[mapper.Size];
            var obj    = new DateTimeAttributeObject
            {
                DateTimeValue       = new DateTime(2000, 12, 31, 0, 0, 0),
                DateTimeOffsetValue = new DateTimeOffset(new DateTime(2000, 12, 31, 0, 0, 0, DateTimeKind.Utc))
            };

            // Write
            mapper.ToByte(buffer, 0, obj);

            Assert.Equal(
                Encoding.ASCII.GetBytes(
                    "20001231" +
                    "        " +
                    "______________" +
                    "20001231" +
                    "        " +
                    "______________"),
                buffer);

            // Read
            mapper.FromByte(buffer, obj);

            mapper.FromByte(
                Encoding.ASCII.GetBytes(
                    "20010101" +
                    "20010101" +
                    "20001231235959" +
                    "20010101" +
                    "20010101" +
                    "20001231235959"),
                obj);

            Assert.Equal(new DateTime(2001, 1, 1, 0, 0, 0), obj.DateTimeValue);
            Assert.Equal(new DateTime(2001, 1, 1, 0, 0, 0), obj.NullableDateTimeValue);
            Assert.Equal(new DateTime(2000, 12, 31, 23, 59, 59), obj.CustomDateTimeValue);
            Assert.Equal(new DateTimeOffset(new DateTime(2001, 1, 1, 0, 0, 0, DateTimeKind.Utc)), obj.DateTimeOffsetValue);
            Assert.Equal(new DateTimeOffset(new DateTime(2001, 1, 1, 0, 0, 0, DateTimeKind.Utc)), obj.NullableDateTimeOffsetValue);
            Assert.Equal(new DateTimeOffset(new DateTime(2000, 12, 31, 23, 59, 59, DateTimeKind.Utc)), obj.CustomDateTimeOffsetValue);
        }
Example #17
0
        public void MapByConstantAttribute()
        {
            var mapperFactory = new MapperFactoryConfig()
                                .DefaultDelimiter(0x0D, 0x0A)
                                .CreateMapByAttribute <ConstAttributeObject>()
                                .ToMapperFactory();
            var mapper = mapperFactory.Create <ConstAttributeObject>();

            var buffer = new byte[mapper.Size];
            var obj    = new ConstAttributeObject();

            // Write
            mapper.ToByte(buffer, 0, obj);

            Assert.Equal(Encoding.ASCII.GetBytes("12\r\n"), buffer);
        }
Example #18
0
        public void MapUseNullFiller()
        {
            var mapperFactory = new MapperFactoryConfig()
                                .DefaultDelimiter(null)
                                .DefaultFiller(0xCC)
                                .CreateMapByExpression <MapNullFillerObject>(2, c => c.NullFiller(0xFF))
                                .CreateMapByExpression <DefaultNullFillerObject>(2, c => { })
                                .ToMapperFactory();
            var mapMapper     = mapperFactory.Create <MapNullFillerObject>();
            var defaultMapper = mapperFactory.Create <DefaultNullFillerObject>();

            // Write
            Assert.Equal(new byte[] { 0xFF, 0xFF }, mapMapper.ToByte(null));

            Assert.Equal(new byte[] { 0xCC, 0xCC }, defaultMapper.ToByte(null));
        }
        public void MapByFillerAttribute()
        {
            var mapperFactory = new MapperFactoryConfig()
                                .DefaultDelimiter(null)
                                .DefaultFiller((byte)' ')
                                .CreateMapByAttribute <FillerAttributeObject>()
                                .ToMapperFactory();
            var mapper = mapperFactory.Create <FillerAttributeObject>();

            var buffer = new byte[mapper.Size];
            var obj    = new FillerAttributeObject();

            // Write
            mapper.ToByte(buffer, 0, obj);

            Assert.Equal(Encoding.ASCII.GetBytes("    00"), buffer);
        }
        public void Setup()
        {
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

            var mapperFactory = new MapperFactoryConfig()
                                .UseOptionsDefault()
                                .DefaultEncoding(SjisEncoding.Instance)
                                .CreateMapByExpression <ComplexData>(144, config => config
                                                                     .ForMember(x => x.StringValue1, m => m.Text(20))
                                                                     .ForMember(x => x.StringValue2, m => m.Text(20))
                                                                     .ForMember(x => x.StringValue3, m => m.Text(20))
                                                                     .ForMember(x => x.IntValue1, m => m.Integer(8))
                                                                     .ForMember(x => x.IntValue2, m => m.Integer(8))
                                                                     .ForMember(x => x.IntValue3, m => m.Integer(8))
                                                                     .ForMember(x => x.IntValue4, m => m.Integer(8))
                                                                     .ForMember(x => x.DecimalValue1, m => m.Decimal(10, 2))
                                                                     .ForMember(x => x.DecimalValue2, m => m.Decimal(10, 2))
                                                                     .ForMember(x => x.BoolValue1, m => m.Boolean())
                                                                     .ForMember(x => x.BoolValue2, m => m.Boolean())
                                                                     .ForMember(x => x.DateTimeValue1, m => m.DateTime("yyyyMMddHHmmss"))
                                                                     .ForMember(x => x.DateTimeValue2, m => m.DateTime("yyyyMMddHHmmss")))
                                .ToMapperFactory();

            mapper = mapperFactory.Create <ComplexData>();

            allocatedBuffer = new byte[mapper.Size];
            allocatedData   = new ComplexData
            {
                StringValue1   = "XXXXXXXXXXXXXXXXXXXX",
                StringValue2   = "あああああ",
                StringValue3   = string.Empty,
                IntValue1      = 1,
                IntValue2      = 0,
                IntValue3      = 1,
                IntValue4      = null,
                BoolValue1     = true,
                BoolValue2     = null,
                DecimalValue1  = 1.23m,
                DecimalValue2  = null,
                DateTimeValue1 = new DateTime(2000, 12, 31, 23, 59, 59, 999),
                DateTimeValue2 = null
            };

            mapper.ToByte(allocatedBuffer, 0, allocatedData);
        }
Example #21
0
        public void MapUseTypeDefaultAttribute()
        {
            var mapperFactory = new MapperFactoryConfig()
                                .DefaultDelimiter(null)
                                .DefaultEncoding(Encoding.UTF8)
                                .DefaultTrim(false)
                                .DefaultTextPadding(Padding.Right)
                                .DefaultFiller((byte)' ')
                                .DefaultTextFiller((byte)' ')
                                .DefaultEndian(Endian.Big)
                                .DefaultTrueValue((byte)'1')
                                .DefaultFalseValue((byte)'1')
                                .DefaultNumberTextEncoding(Encoding.UTF8)
                                .DefaultNumberTextProvider(CultureInfo.CurrentCulture)
                                .DefaultNumberTextNumberStyle(NumberStyles.Integer)
                                .DefaultNumberTextDecimalStyle(NumberStyles.Integer)
                                .DefaultNumberTextPadding(Padding.Left)
                                .DefaultNumberTextFiller((byte)' ')
                                .DefaultDateTimeTextEncoding(Encoding.UTF8)
                                .DefaultDateTimeTextProvider(CultureInfo.CurrentCulture)
                                .DefaultDateTimeTextStyle(DateTimeStyles.None)
                                .DefaultUnicodeFiller(' ')
                                .CreateMapByAttribute <TypeDefaultAttributeObject>()
                                .ToMapperFactory();
            var mapper = mapperFactory.Create <TypeDefaultAttributeObject>();

            var buffer = new byte[mapper.Size];
            var obj    = new TypeDefaultAttributeObject
            {
                IntValue     = 1,
                DecimalValue = 1,
                BoolValue    = true,
                StringValue  = "1"
            };

            // Write
            mapper.ToByte(buffer, 0, obj);

            Assert.Equal(Encoding.ASCII.GetBytes("1_1__1Y*\r\n"), buffer);

            // Fix
            Assert.Equal(Encoding.ASCII.CodePage, ((Encoding) new TypeEncodingAttribute(Encoding.ASCII.CodePage).Value).CodePage);
            Assert.Equal(Encoding.ASCII.CodePage, ((Encoding) new TypeNumberTextEncodingAttribute(Encoding.ASCII.CodePage).Value).CodePage);
            Assert.Equal(Encoding.ASCII.CodePage, ((Encoding) new TypeDateTimeTextEncodingAttribute(Encoding.ASCII.CodePage).Value).CodePage);
        }
        public void MapByConstantExpression()
        {
            var mapperFactory = new MapperFactoryConfig()
                                .DefaultDelimiter(0x0D, 0x0A)
                                .CreateMapByExpression <ConstExpressionObject>(6, config => config
                                                                               .UseDelimiter(true)
                                                                               .Constant(0, new byte[] { 0x31, 0x32 })
                                                                               .Constant(new byte[] { 0x33, 0x34 }))
                                .ToMapperFactory();
            var mapper = mapperFactory.Create <ConstExpressionObject>();

            var buffer = new byte[mapper.Size];
            var obj    = new ConstExpressionObject();

            // Write
            mapper.ToByte(buffer, 0, obj);

            Assert.Equal(Encoding.ASCII.GetBytes("1234\r\n"), buffer);
        }
        public void MapByBooleanExpression()
        {
            var mapperFactory = new MapperFactoryConfig()
                                .DefaultDelimiter(null)
                                .DefaultFiller(Filler)
                                .DefaultTrueValue(True)
                                .DefaultFalseValue(False)
                                .CreateMapByExpression <BooleanExpressionObject>(4, config => config
                                                                                 .ForMember(x => x.BooleanValue, m => m.Boolean())
                                                                                 .ForMember(x => x.NullableBooleanValue, m => m.Boolean())
                                                                                 .ForMember(x => x.CustomBooleanValue, m => m.Boolean(Yes, No))
                                                                                 .ForMember(x => x.CustomNullableBooleanValue, m => m.Boolean(Yes, No, Filler)))
                                .ToMapperFactory();
            var mapper = mapperFactory.Create <BooleanExpressionObject>();

            var buffer = new byte[mapper.Size];
            var obj    = new BooleanExpressionObject();

            // Write
            mapper.ToByte(buffer, 0, obj);

            Assert.Equal(new[] { False, Filler, No, Filler }, buffer);

            // Read
            mapper.FromByte(new[] { True, True, Yes, Yes }, 0, obj);

            Assert.True(obj.BooleanValue);
            Assert.True(obj.NullableBooleanValue);
            Assert.True(obj.CustomBooleanValue);
            Assert.True(obj.CustomNullableBooleanValue);

            // Read default
            mapper.FromByte(new[] { Filler, Filler, Filler, Filler }, 0, obj);

            Assert.False(obj.BooleanValue);
            Assert.Null(obj.NullableBooleanValue);
            Assert.False(obj.CustomBooleanValue);
            Assert.Null(obj.CustomNullableBooleanValue);
        }
Example #24
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

            var mapperFactory = new MapperFactoryConfig()
                                .UseOptionsDefault()
                                .DefaultEncoding(Encoding.GetEncoding(932))
                                .CreateMapByExpression <SampleData>(59, c => c
                                                                    .ForMember(x => x.Code, m => m.Ascii(13))
                                                                    .ForMember(x => x.Name, m => m.Text(20))
                                                                    .ForMember(x => x.Qty, m => m.Integer(6))
                                                                    .ForMember(x => x.Price, m => m.Decimal(10, 2))
                                                                    .ForMember(x => x.Date, m => m.DateTime("yyyyMMdd")))
                                .CreateMapByExpression <SampleData>("short", 35, c => c
                                                                    .ForMember(x => x.Code, m => m.Ascii(13))
                                                                    .ForMember(x => x.Name, m => m.Text(20)))
                                .ToMapperFactory();

            services.AddMvc(options =>
            {
                var config = new ByteMapperFormatterConfig {
                    MapperFactory = mapperFactory
                };
                config.SupportedMediaTypes.Add("text/x-fixedrecord");

                options.OutputFormatters.Add(new ByteMapperOutputFormatter(config));
                options.InputFormatters.Add(new ByteMapperInputFormatter(config));

                options.FormatterMappings.SetMediaTypeMappingForFormat("dat", "text/x-fixedrecord");
            }).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

            // Swagger
            services.AddSwaggerGen(options =>
            {
                options.SwaggerDoc("example", new Info {
                    Title = "Example API", Version = "v1"
                });
            });
        }
Example #25
0
        public void MapByFillerExpression()
        {
            var mapperFactory = new MapperFactoryConfig()
                                .DefaultDelimiter(null)
                                .DefaultFiller((byte)' ')
                                .CreateMapByExpression <FillerExpressionObject>(4, config => config
                                                                                .AutoFiller(true)
                                                                                .Filler(0, 1)
                                                                                .Filler(1, 1, (byte)'0')
                                                                                .Filler(1)
                                                                                .Filler(1, (byte)'_'))
                                .ToMapperFactory();
            var mapper = mapperFactory.Create <FillerExpressionObject>();

            var buffer = new byte[mapper.Size];
            var obj    = new FillerExpressionObject();

            // Write
            mapper.ToByte(buffer, 0, obj);

            Assert.Equal(Encoding.ASCII.GetBytes(" 0 _"), buffer);
        }
Example #26
0
        public void MapUseDelimiter()
        {
            var mapperFactory = new MapperFactoryConfig()
                                .DefaultFiller(0x00)
                                .DefaultDelimiter(0xCC)
                                .CreateMapByExpression <TypeDelimiterObject>(2, c => c.AutoFiller(false).UseDelimiter(0xFF))
                                .CreateMapByExpression <DefaultDelimiterObject>(2, c => c.AutoFiller(false).UseDelimiter(true))
                                .CreateMapByExpression <NoDelimiterObject>(2, c => c.AutoFiller(false).UseDelimiter(null))
                                .ToMapperFactory();
            var typeMapper    = mapperFactory.Create <TypeDelimiterObject>();
            var defaultMapper = mapperFactory.Create <DefaultDelimiterObject>();
            var noMapper      = mapperFactory.Create <NoDelimiterObject>();

            // Write
            Assert.Equal(new byte[] { 0x00, 0xFF }, typeMapper.ToByte(new TypeDelimiterObject()));

            Assert.Equal(new byte[] { 0x00, 0xCC }, defaultMapper.ToByte(new DefaultDelimiterObject()));

            var buffer = new byte[noMapper.Size].Fill(0x11);

            noMapper.ToByte(buffer, 0, new NoDelimiterObject());
            Assert.Equal(new byte[] { 0x11, 0x11 }, buffer);
        }
Example #27
0
        public void MapUseAutoFiller()
        {
            var mapperFactory = new MapperFactoryConfig()
                                .DefaultDelimiter(null)
                                .DefaultFiller(0xCC)
                                .CreateMapByAttribute <TypeFillerObject>()
                                .CreateMapByAttribute <DefaultFillerObject>()
                                .CreateMapByAttribute <NoFillerObject>()
                                .ToMapperFactory();
            var typeMapper    = mapperFactory.Create <TypeFillerObject>();
            var defaultMapper = mapperFactory.Create <DefaultFillerObject>();
            var noMapper      = mapperFactory.Create <NoFillerObject>();

            // Write
            Assert.Equal(new byte[] { 0xFF, 0xFF }, typeMapper.ToByte(new TypeFillerObject()));

            Assert.Equal(new byte[] { 0xCC, 0xCC }, defaultMapper.ToByte(new DefaultFillerObject()));

            var buffer = new byte[noMapper.Size].Fill(0x11);

            noMapper.ToByte(buffer, 0, new NoFillerObject());
            Assert.Equal(new byte[] { 0x11, 0x11 }, buffer);
        }
 public static MapperFactoryConfig DefaultUnicodeFiller(this MapperFactoryConfig config, char value)
 {
     return(config.AddParameter(OptionsParameter.UnicodeFiller, value));
 }
 public static MapperFactoryConfig DefaultNumberFiller(this MapperFactoryConfig config, byte value)
 {
     return(config.AddParameter(OptionsParameter.NumberFiller, value));
 }
 public static MapperFactoryConfig DefaultUseGrouping(this MapperFactoryConfig config, bool value)
 {
     return(config.AddParameter(OptionsParameter.UseGrouping, value));
 }