Пример #1
0
        private async Task <Task> DumpOutputFileAsync()
        {
            using (var writer = new StreamWriter(CommandLineOptions.Output))
                using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
                {
                    csv.Configuration.HasHeaderRecord = false;
                    csv.Configuration.Delimiter       = ";";

                    csv.Configuration.ShouldQuote = (field, context) =>
                    {
                        var index = context.Record.Count;
                        var type  = ((PropertyInfo)context.WriterConfiguration.Maps.Find <CsvOutput>().MemberMaps[index].Data.Member).PropertyType;

                        if (type == typeof(string))
                        {
                            return(true);
                        }

                        return(ConfigurationFunctions.ShouldQuote(field, context));
                    };

                    await csv.WriteRecordsAsync(CsvOutput);

                    await writer.FlushAsync();
                }

            return(Task.CompletedTask);
        }
Пример #2
0
        public void Test1()
        {
            Type type   = null;
            var  config = new CsvConfiguration(CultureInfo.InvariantCulture)
            {
                ShouldQuote = args =>
                {
                    type = args.FieldType;
                    return(ConfigurationFunctions.ShouldQuote(args));
                },
            };

            using (var writer = new StringWriter())
                using (var csv = new CsvWriter(writer, config))
                {
                    csv.WriteField(string.Empty);
                    Assert.AreEqual(typeof(string), type);

                    csv.WriteField(1);
                    Assert.AreEqual(typeof(int), type);

                    csv.WriteField(string.Empty);
                    Assert.AreEqual(typeof(string), type);
                }
        }
Пример #3
0
        public void Parameter_WithGetConstructorAndName_AddsParameter()
        {
            var map = new DefaultClassMap <Foo>();

            map.Parameter(() => ConfigurationFunctions.GetConstructor(typeof(Foo)), "id");

            Assert.AreEqual(1, map.ParameterMaps.Count);
            Assert.AreEqual("id", map.ParameterMaps[0].Data.Parameter.Name);
        }
Пример #4
0
        public void Parameter_WithConstructorFunctionAndName_CreatesParameterMaps()
        {
            var map = new DefaultClassMap <Foo>();

            map.Parameter(() => ConfigurationFunctions.GetConstructor(new GetConstructorArgs(typeof(Foo))), "id");
            map.Parameter(() => ConfigurationFunctions.GetConstructor(new GetConstructorArgs(typeof(Foo))), "date").TypeConverterOption.DateTimeStyles(DateTimeStyles.AllowLeadingWhite);

            Assert.AreEqual(2, map.ParameterMaps.Count);
            Assert.IsNull(map.ParameterMaps[0].Data.TypeConverterOptions.DateTimeStyle);
            Assert.AreEqual(DateTimeStyles.AllowLeadingWhite, map.ParameterMaps[1].Data.TypeConverterOptions.DateTimeStyle);
        }
Пример #5
0
        public void Parameter_WithConstructorFunctionAndName_CreatesParameterMaps()
        {
            var map = new DefaultClassMap <Foo>();

            map.Parameter(() => ConfigurationFunctions.GetConstructor(new GetConstructorArgs(typeof(Foo))), "id");
            map.Parameter(() => ConfigurationFunctions.GetConstructor(new GetConstructorArgs(typeof(Foo))), "name").Ignore();

            Assert.AreEqual(2, map.ParameterMaps.Count);
            Assert.IsFalse(map.ParameterMaps[0].Data.Ignore);
            Assert.IsTrue(map.ParameterMaps[1].Data.Ignore);
        }
Пример #6
0
        public void Parameter_WithConstructorAndParameter_AddsParameter()
        {
            var map         = new DefaultClassMap <Foo>();
            var constructor = ConfigurationFunctions.GetConstructor(typeof(Foo));
            var parameter   = constructor.GetParameters().Single(p => p.Name == "id");

            map.Parameter(constructor, parameter);

            Assert.AreEqual(1, map.ParameterMaps.Count);
            Assert.AreEqual("id", map.ParameterMaps[0].Data.Parameter.Name);
        }
        public void Parameter_WithConstructorFunctionAndName_CreatesParameterMaps()
        {
            var map = new DefaultClassMap <Foo>();

            map.Parameter(() => ConfigurationFunctions.GetConstructor(typeof(Foo)), "id");
            map.Parameter(() => ConfigurationFunctions.GetConstructor(typeof(Foo)), "name").TypeConverter <CustomConverter>();

            Assert.AreEqual(2, map.ParameterMaps.Count);
            Assert.IsNull(map.ParameterMaps[0].Data.TypeConverter);
            Assert.IsInstanceOfType(map.ParameterMaps[1].Data.TypeConverter, typeof(CustomConverter));
        }
Пример #8
0
        public void Parameter_WithConstructorFunctionAndName_CreatesParameterMaps()
        {
            var map = new DefaultClassMap <Foo>();

            map.Parameter(() => ConfigurationFunctions.GetConstructor(typeof(Foo)), "id").Name("Id");
            map.Parameter(() => ConfigurationFunctions.GetConstructor(typeof(Foo)), "name").Name("Name");

            Assert.AreEqual(2, map.ParameterMaps.Count);
            Assert.AreEqual("Id", map.ParameterMaps[0].Data.Names[0]);
            Assert.AreEqual("Name", map.ParameterMaps[1].Data.Names[0]);
        }
Пример #9
0
        public void Parameter_WithConstructorFunctionAndName_CreatesParameterMaps()
        {
            var map = new DefaultClassMap <Foo>();

            map.Parameter(() => ConfigurationFunctions.GetConstructor(new GetConstructorArgs(typeof(Foo))), "id").NameIndex(0);
            map.Parameter(() => ConfigurationFunctions.GetConstructor(new GetConstructorArgs(typeof(Foo))), "name").NameIndex(1);

            Assert.Equal(2, map.ParameterMaps.Count);
            Assert.Equal(0, map.ParameterMaps[0].Data.NameIndex);
            Assert.Equal(1, map.ParameterMaps[1].Data.NameIndex);
        }
Пример #10
0
        public void Parameter_WithConstructorFunctionAndName_CreatesParameterMaps()
        {
            var map = new DefaultClassMap <Foo>();

            map.Parameter(() => ConfigurationFunctions.GetConstructor(new GetConstructorArgs(typeof(Foo))), "id");
            map.Parameter(() => ConfigurationFunctions.GetConstructor(new GetConstructorArgs(typeof(Foo))), "amount").TypeConverterOption.NumberStyles(NumberStyles.AllowParentheses);

            Assert.Equal(2, map.ParameterMaps.Count);
            Assert.Null(map.ParameterMaps[0].Data.TypeConverterOptions.NumberStyles);
            Assert.Equal(NumberStyles.AllowParentheses, map.ParameterMaps[1].Data.TypeConverterOptions.NumberStyles);
        }
Пример #11
0
        public void Parameter_WithConstructorFunctionAndName_CreatesParameterMaps()
        {
            var map = new DefaultClassMap <Foo>();

            map.Parameter(() => ConfigurationFunctions.GetConstructor(new GetConstructorArgs(typeof(Foo))), "id");
            map.Parameter(() => ConfigurationFunctions.GetConstructor(new GetConstructorArgs(typeof(Foo))), "date").TypeConverterOption.Format(FORMAT);

            Assert.Equal(2, map.ParameterMaps.Count);
            Assert.Null(map.ParameterMaps[0].Data.TypeConverterOptions.Formats);
            Assert.Single(map.ParameterMaps[1].Data.TypeConverterOptions.Formats);
            Assert.Equal(FORMAT, map.ParameterMaps[1].Data.TypeConverterOptions.Formats[0]);
        }
Пример #12
0
        public void Parameter_WithConstructorAndProperty_CreatesParameterMaps()
        {
            var constructor = ConfigurationFunctions.GetConstructor(new GetConstructorArgs(typeof(Foo)));
            var parameters  = constructor.GetParameters();

            var map = new DefaultClassMap <Foo>();

            map.Parameter(constructor, parameters[0]);
            map.Parameter(constructor, parameters[1]).Ignore();

            Assert.AreEqual(2, map.ParameterMaps.Count);
            Assert.IsFalse(map.ParameterMaps[0].Data.Ignore);
            Assert.IsTrue(map.ParameterMaps[1].Data.Ignore);
        }
        public void Parameter_WithConstructorAndProperty_CreatesParameterMaps()
        {
            var constructor = ConfigurationFunctions.GetConstructor(typeof(Foo));
            var parameters  = constructor.GetParameters();

            var map = new DefaultClassMap <Foo>();

            map.Parameter(constructor, parameters[0]);
            map.Parameter(constructor, parameters[1]).TypeConverter <CustomConverter>();

            Assert.AreEqual(2, map.ParameterMaps.Count);
            Assert.IsNull(map.ParameterMaps[0].Data.TypeConverter);
            Assert.IsInstanceOfType(map.ParameterMaps[1].Data.TypeConverter, typeof(CustomConverter));
        }
Пример #14
0
        public void Parameter_WithConstructorAndProperty_CreatesParameterMaps()
        {
            var constructor = ConfigurationFunctions.GetConstructor(typeof(Foo));
            var parameters  = constructor.GetParameters();

            var map = new DefaultClassMap <Foo>();

            map.Parameter(constructor, parameters[0]).Name("Id");
            map.Parameter(constructor, parameters[1]).Name("Name");

            Assert.AreEqual(2, map.ParameterMaps.Count);
            Assert.AreEqual("Id", map.ParameterMaps[0].Data.Names[0]);
            Assert.AreEqual("Name", map.ParameterMaps[1].Data.Names[0]);
        }
Пример #15
0
        public void Parameter_WithConstructorAndProperty_CreatesParameterMaps()
        {
            var constructor = ConfigurationFunctions.GetConstructor(new GetConstructorArgs(typeof(Foo)));
            var parameters  = constructor.GetParameters();

            var map = new DefaultClassMap <Foo>();

            map.Parameter(constructor, parameters[0]);
            map.Parameter(constructor, parameters[1]).TypeConverterOption.DateTimeStyles(DateTimeStyles.AllowLeadingWhite);

            Assert.AreEqual(2, map.ParameterMaps.Count);
            Assert.IsNull(map.ParameterMaps[0].Data.TypeConverterOptions.DateTimeStyle);
            Assert.AreEqual(DateTimeStyles.AllowLeadingWhite, map.ParameterMaps[1].Data.TypeConverterOptions.DateTimeStyle);
        }
Пример #16
0
        public void Parameter_WithConstructorAndProperty_CreatesParameterMaps()
        {
            var constructor = ConfigurationFunctions.GetConstructor(new GetConstructorArgs(typeof(Foo)));
            var parameters  = constructor.GetParameters();

            var map = new DefaultClassMap <Foo>();

            map.Parameter(constructor, parameters[0]);
            map.Parameter(constructor, parameters[1]).TypeConverterOption.NumberStyles(NumberStyles.AllowParentheses);

            Assert.Equal(2, map.ParameterMaps.Count);
            Assert.Null(map.ParameterMaps[0].Data.TypeConverterOptions.NumberStyles);
            Assert.Equal(NumberStyles.AllowParentheses, map.ParameterMaps[1].Data.TypeConverterOptions.NumberStyles);
        }
Пример #17
0
        public void Parameter_WithConstructorAndProperty_CreatesParameterMaps()
        {
            var constructor = ConfigurationFunctions.GetConstructor(new GetConstructorArgs(typeof(Foo)));
            var parameters  = constructor.GetParameters();

            var map = new DefaultClassMap <Foo>();

            map.Parameter(constructor, parameters[0]).NameIndex(0);
            map.Parameter(constructor, parameters[1]).NameIndex(1);

            Assert.Equal(2, map.ParameterMaps.Count);
            Assert.Equal(0, map.ParameterMaps[0].Data.NameIndex);
            Assert.Equal(1, map.ParameterMaps[1].Data.NameIndex);
        }
Пример #18
0
        public void Parameter_WithConstructorAndProperty_CreatesParameterMaps()
        {
            var constructor = ConfigurationFunctions.GetConstructor(new GetConstructorArgs(typeof(Foo)));
            var parameters  = constructor.GetParameters();

            var map = new DefaultClassMap <Foo>();

            map.Parameter(constructor, parameters[0]);
            map.Parameter(constructor, parameters[1]).TypeConverterOption.Format(FORMAT);

            Assert.Equal(2, map.ParameterMaps.Count);
            Assert.Null(map.ParameterMaps[0].Data.TypeConverterOptions.Formats);
            Assert.Single(map.ParameterMaps[1].Data.TypeConverterOptions.Formats);
            Assert.Equal(FORMAT, map.ParameterMaps[1].Data.TypeConverterOptions.Formats[0]);
        }
Пример #19
0
        public void Test1()
        {
            var data   = new List <(int row, int column, string field)>();
            var config = new CsvConfiguration(CultureInfo.InvariantCulture)
            {
                ShouldQuote = args =>
                {
                    data.Add((args.Row.Row, args.Row.Index, args.Field));

                    return(ConfigurationFunctions.ShouldQuote(args));
                },
            };

            using (var writer = new StringWriter())
                using (var csv = new CsvWriter(writer, config))
                {
                    csv.WriteField("Id");
                    csv.WriteField("Name");
                    csv.NextRecord();

                    csv.WriteField("1");
                    csv.WriteField("one");
                    csv.NextRecord();

                    csv.Flush();
                }

            Assert.Equal(4, data.Count);

            Assert.Equal(1, data[0].row);
            Assert.Equal(0, data[0].column);
            Assert.Equal("Id", data[0].field);

            Assert.Equal(1, data[1].row);
            Assert.Equal(1, data[1].column);
            Assert.Equal("Name", data[1].field);

            Assert.Equal(2, data[2].row);
            Assert.Equal(0, data[2].column);
            Assert.Equal("1", data[2].field);

            Assert.Equal(2, data[3].row);
            Assert.Equal(1, data[3].column);
            Assert.Equal("one", data[3].field);
        }
Пример #20
0
        public IEnumerable <GrossWeightPremiumModel> GetAllCountryData()
        {
            try
            {
                string filePath = Path.Combine(Directory.GetParent(System.IO.Directory.GetCurrentDirectory()).Parent.Parent.Parent.FullName, "Galytix.WebApi.Data\\DataRepository\\gwpByCountry.csv");

                IEnumerable <GrossWeightPremiumModel> records;

                var config = new CsvConfiguration(CultureInfo.InvariantCulture)
                {
                    HasHeaderRecord   = true,
                    MissingFieldFound = null,
                    TrimOptions       = TrimOptions.Trim,
                    BadDataFound      = (context) =>
                    {
                        Console.WriteLine(context.RawRecord);
                        ConfigurationFunctions.BadDataFound(context); // let CsvHelper throw this.
                    }
                };

                using (var reader = new StreamReader(filePath))
                    using (var csv = new CsvReader(reader, config))
                    {
                        csv.Context.RegisterClassMap <GrowthWeightPremiumMap>();
                        var csvRecords = csv.GetRecords <GrossWeightPremiumModel>();
                        records = csvRecords.ToList();
                    }

                return(records);
            }
            catch (Exception ex)
            {
                string exception = ex.Message.ToString();

                return(new List <GrossWeightPremiumModel>());
            }
        }
Пример #21
0
        public void WriteRecords_ShouldQuote_HasCorrectFieldType()
        {
            var records = new List <Foo>
            {
                new Foo {
                    Id = 1, Name = "one"
                },
            };
            var config = new CsvConfiguration(CultureInfo.InvariantCulture)
            {
                ShouldQuote = args =>
                {
                    if (args.Row.Row > 1)
                    {
                        switch (args.Row.Index)
                        {
                        case 0:
                            Assert.Equal(typeof(int), args.FieldType);
                            break;

                        case 1:
                            Assert.Equal(typeof(string), args.FieldType);
                            break;
                        }
                    }

                    return(ConfigurationFunctions.ShouldQuote(args));
                },
            };

            using (var writer = new StringWriter())
                using (var csv = new CsvWriter(writer, config))
                {
                    csv.WriteRecords(records);
                }
        }