Пример #1
0
        public void ShouldTreatConstantAsNull_TypeMapper()
        {
            var nullHandler = ConstantNullHandler.For("----");
            var mapper      = SeparatedValueTypeMapper.Define <Product>();

            mapper.Property(p => p.Name).ColumnName("name").NullHandler(nullHandler);
            mapper.Property(p => p.Cost).ColumnName("cost").NullHandler(nullHandler).FormatProvider(CultureInfo.InvariantCulture);
            mapper.Property(p => p.Available).ColumnName("available").NullHandler(nullHandler);
            mapper.Property(p => p.Vendor).ColumnName("vendor").NullHandler(nullHandler);

            string       content      = "----,5.12,----,apple" + Environment.NewLine;
            StringReader stringReader = new StringReader(content);
            var          products     = mapper.Read(stringReader).ToArray();

            Assert.Single(products);

            Product product = products.Single();

            Assert.Null(product.Name);
            Assert.Equal(5.12m, product.Cost);
            Assert.Null(product.Available);
            Assert.Equal("apple", product.Vendor);

            StringWriter stringWriter = new StringWriter();

            mapper.Write(stringWriter, products);
            string output = stringWriter.ToString();

            Assert.Equal(content, output);
        }
Пример #2
0
        private static SeparatedValueSchema getSchema()
        {
            var nullHandler = ConstantNullHandler.For("----");

            SeparatedValueSchema schema = new SeparatedValueSchema();

            schema.AddColumn(new StringColumn("Name")
            {
                NullHandler = nullHandler
            });
            schema.AddColumn(new DecimalColumn("Cost")
            {
                NullHandler = nullHandler, FormatProvider = CultureInfo.InvariantCulture
            });
            schema.AddColumn(new SingleColumn("Available")
            {
                NullHandler = nullHandler
            });
            schema.AddColumn(new StringColumn("Vendor")
            {
                NullHandler = nullHandler
            });

            return(schema);
        }
Пример #3
0
        public SeparatedFileSchema BuildSchema(RawFileSchema rawSchema)
        {
            var separatedFileSchema = new SeparatedFileSchema();
            var rowDefinitionList   = rawSchema.Schema.RowDefinitions;

            separatedFileSchema.Delimeter = rawSchema.Delimeter;

            foreach (var rowDefinition in rowDefinitionList)
            {
                var columnDefinitions = rowDefinition.ColumnDefinitions;
                var rowSchema         = new SeparatedValueSchema();

                foreach (var columnDefinition in columnDefinitions)
                {
                    var definition = GetColumnDefinition(columnDefinition);
                    definition.NullHandler = ConstantNullHandler.For("NULL");
                    rowSchema.AddColumn(definition);
                }

                separatedFileSchema.SeparatedRecordSchemas.Add(new SeparatedRecordSchema
                {
                    RecordIdentifier     = rowDefinition.Identifier,
                    SeparatedValueSchema = rowSchema
                });
            }

            return(separatedFileSchema);
        }
        public void ShouldTreatConstantAsNull_TypeMapper()
        {
            var nullHandler = ConstantNullHandler.For("----");
            var mapper      = SeparatedValueTypeMapper.Define <Product>();

            mapper.Property(p => p.Name).ColumnName("name").NullHandler(nullHandler);
            mapper.Property(p => p.Cost).ColumnName("cost").NullHandler(nullHandler);
            mapper.Property(p => p.Available).ColumnName("available").NullHandler(nullHandler);
            mapper.Property(p => p.Vendor).ColumnName("vendor").NullHandler(nullHandler);

            string content = "----,5.12,----,apple" + Environment.NewLine;

            byte[]       encoded     = Encoding.Default.GetBytes(content);
            MemoryStream inputStream = new MemoryStream(encoded);
            var          products    = mapper.Read(inputStream);

            Assert.AreEqual(1, products.Count(), "The wrong number of products were found.");

            Product product = products.Single();

            Assert.IsNull(product.Name, "The name was not interpreted as null.");
            Assert.AreEqual(5.12m, product.Cost, "The cost was not read correctly.");
            Assert.IsNull(product.Available, "The available was not interpreted as null.");
            Assert.AreEqual("apple", product.Vendor, "The vendor was not read correctly.");

            MemoryStream outputStream = new MemoryStream();

            mapper.Write(outputStream, products);
            outputStream.Position = 0;
            string output = Encoding.Default.GetString(outputStream.ToArray());

            Assert.AreEqual(content, output, "The null handler was not respected when writing null.");
        }
Пример #5
0
        public FixedLengthFileSchema BuildSchema(RawFileSchema rawSchema)
        {
            var fixedLengthFileSchema = new FixedLengthFileSchema();
            var rowDefinitionList     = rawSchema.Schema.RowDefinitions;

            foreach (var rowDefinition in rowDefinitionList)
            {
                var columnDefinitions = rowDefinition.ColumnDefinitions;
                var rowSchema         = new FixedLengthSchema();

                foreach (var columnDefinition in columnDefinitions)
                {
                    var definition = GetColumnDefinition(columnDefinition);
                    definition.NullHandler = ConstantNullHandler.For("NULL");
                    rowSchema.AddColumn(
                        definition, BuildWindow(columnDefinition)
                        );
                }

                fixedLengthFileSchema.FixedLengthRecordSchemas.Add(new FixedLengthRecordSchema
                {
                    RecordIdentifier  = rowDefinition.Identifier,
                    FixedLengthSchema = rowSchema
                });
            }

            return(fixedLengthFileSchema);
        }