public void Parameter_WithName_CreatesParameterMaps() { var map = new DefaultClassMap <Foo>(); map.Parameter("id"); map.Parameter("amount").TypeConverterOption.NumberStyles(NumberStyles.AllowParentheses); Assert.AreEqual(2, map.ParameterMaps.Count); Assert.IsNull(map.ParameterMaps[0].Data.TypeConverterOptions.NumberStyles); Assert.AreEqual(NumberStyles.AllowParentheses, map.ParameterMaps[1].Data.TypeConverterOptions.NumberStyles); }
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.AreEqual(2, map.ParameterMaps.Count); Assert.IsNull(map.ParameterMaps[0].Data.TypeConverterOptions.NumberStyles); Assert.AreEqual(NumberStyles.AllowParentheses, map.ParameterMaps[1].Data.TypeConverterOptions.NumberStyles); }
public void Parameter_WithName_CreatesParameterMaps() { var map = new DefaultClassMap <Foo>(); map.Parameter("id"); map.Parameter("name").Optional(); Assert.Equal(2, map.ParameterMaps.Count); Assert.False(map.ParameterMaps[0].Data.IsOptional); Assert.True(map.ParameterMaps[1].Data.IsOptional); }
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]); }
public void Parameter_WithName_CreatesParameterMaps() { var map = new DefaultClassMap <Foo>(); map.Parameter("id"); map.Parameter("name").Ignore(); Assert.AreEqual(2, map.ParameterMaps.Count); Assert.IsFalse(map.ParameterMaps[0].Data.Ignore); Assert.IsTrue(map.ParameterMaps[1].Data.Ignore); }
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.Equal(2, map.ParameterMaps.Count); Assert.Null(map.ParameterMaps[0].Data.TypeConverterOptions.DateTimeStyle); Assert.Equal(DateTimeStyles.AllowLeadingWhite, map.ParameterMaps[1].Data.TypeConverterOptions.DateTimeStyle); }
public void Parameter_WithName_CreatesParameterMaps() { var map = new DefaultClassMap <Foo>(); map.Parameter("id"); map.Parameter("date").TypeConverterOption.DateTimeStyles(DateTimeStyles.AllowLeadingWhite); Assert.Equal(2, map.ParameterMaps.Count); Assert.Null(map.ParameterMaps[0].Data.TypeConverterOptions.DateTimeStyle); Assert.Equal(DateTimeStyles.AllowLeadingWhite, map.ParameterMaps[1].Data.TypeConverterOptions.DateTimeStyle); }
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_WithName_CreatesParameterMaps() { var map = new DefaultClassMap <Foo>(); map.Parameter("id"); map.Parameter("name").TypeConverter <CustomConverter>(); Assert.Equal(2, map.ParameterMaps.Count); Assert.Null(map.ParameterMaps[0].Data.TypeConverter); Assert.IsType <CustomConverter>(map.ParameterMaps[1].Data.TypeConverter); }
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").TypeConverter <CustomConverter>(); Assert.Equal(2, map.ParameterMaps.Count); Assert.Null(map.ParameterMaps[0].Data.TypeConverter); Assert.IsType <CustomConverter>(map.ParameterMaps[1].Data.TypeConverter); }
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").Optional(); Assert.AreEqual(2, map.ParameterMaps.Count); Assert.IsFalse(map.ParameterMaps[0].Data.IsOptional); Assert.IsTrue(map.ParameterMaps[1].Data.IsOptional); }
public void Parameter_WithName_CreatesParameterMaps() { var map = new DefaultClassMap <Foo>(); map.Parameter("id").Index(0); map.Parameter("name").Index(1); Assert.AreEqual(2, map.ParameterMaps.Count); Assert.AreEqual(0, map.ParameterMaps[0].Data.Index); Assert.AreEqual(1, map.ParameterMaps[1].Data.Index); }
public void Parameter_WithName_CreatesParameterMaps() { var map = new DefaultClassMap <Foo>(); map.Parameter("id"); map.Parameter("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)); }
public void Parameter_WithName_CreatesParameterMaps() { var map = new DefaultClassMap <Foo>(); map.Parameter("id").Name("Id"); map.Parameter("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]); }
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); }
/// <summary> /// Writes the records to the speficed file using the specified mapping and settings. /// </summary> /// <typeparam name="T">The record type.</typeparam> /// <param name="csvFile">The CSV file to write.</param> /// <param name="records">The records to write.</param> /// <param name="mapping">The property column mapping.</param> /// <param name="settings">The settings.</param> public void WriteRecords <T>(FilePath csvFile, List <T> records, Dictionary <string, string> mapping, CsvHelperSettings settings) { if (mapping == null) { throw new ArgumentNullException(nameof(mapping)); } var customMap = new DefaultClassMap <T>(); customMap.AutoMap(); WriteRecords(csvFile, records, customMap, settings); }
public void AutoMap_WithBooleanFalseValuesAttribute_CreatesParameterMaps() { var map = new DefaultClassMap <Foo>(); map.Parameter("id"); map.Parameter("name").TypeConverterOption.NullValues("NULL"); Assert.Equal(2, map.ParameterMaps.Count); Assert.Empty(map.ParameterMaps[0].Data.TypeConverterOptions.NullValues); Assert.Single(map.ParameterMaps[1].Data.TypeConverterOptions.NullValues); Assert.Equal("NULL", map.ParameterMaps[1].Data.TypeConverterOptions.NullValues[0]); }
public static List <T> Deserialize(string input) { DefaultClassMap <T> mapping = CreateMapping(); using (var reader = new StringReader(input)) using (var csv = new CsvReader(reader)) { csv.Configuration.RegisterClassMap(mapping); csv.Configuration.Delimiter = ","; return(csv.GetRecords <T>().ToList()); } }
public void Parameter_WithName_CreatesParameterMaps() { var map = new DefaultClassMap <Foo>(); map.Parameter("id"); map.Parameter("date").TypeConverterOption.Format(FORMAT); Assert.AreEqual(2, map.ParameterMaps.Count); Assert.IsNull(map.ParameterMaps[0].Data.TypeConverterOptions.Formats); Assert.AreEqual(1, map.ParameterMaps[1].Data.TypeConverterOptions.Formats.Count()); Assert.AreEqual(FORMAT, map.ParameterMaps[1].Data.TypeConverterOptions.Formats[0]); }
public void Parameter_WithName_CreatesParameterMaps() { var map = new DefaultClassMap <Foo>(); map.Parameter("id"); map.Parameter("name").Constant("Bar"); Assert.Equal(2, map.ParameterMaps.Count); Assert.False(map.ParameterMaps[0].Data.IsConstantSet); Assert.True(map.ParameterMaps[1].Data.IsConstantSet); Assert.Equal("Bar", map.ParameterMaps[1].Data.Constant); }
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]); }
public void GetRecordsWithFields_IgnoreBaseAttribute_DoesNotMapBaseClass() { var map = new DefaultClassMap <ChildFields>(); map.AutoMap(new CsvConfiguration(CultureInfo.InvariantCulture) { MemberTypes = MemberTypes.Fields, }); Assert.Single(map.MemberMaps); Assert.Null(map.MemberMaps.Find <ChildFields>(m => m.Id)); Assert.NotNull(map.MemberMaps.Find <ChildFields>(m => m.Name)); }
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); }
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); }
public void AutoMap_WithBooleanFalseValuesAttribute_CreatesParameterMaps() { var map = new DefaultClassMap <Foo>(); map.Parameter("id"); map.Parameter("boolean").TypeConverterOption.BooleanValues(true, true, "Bar"); Assert.AreEqual(2, map.ParameterMaps.Count); Assert.AreEqual(0, map.ParameterMaps[0].Data.TypeConverterOptions.BooleanTrueValues.Count); Assert.AreEqual(0, map.ParameterMaps[0].Data.TypeConverterOptions.BooleanFalseValues.Count); Assert.AreEqual(1, map.ParameterMaps[1].Data.TypeConverterOptions.BooleanTrueValues.Count); Assert.AreEqual(0, map.ParameterMaps[1].Data.TypeConverterOptions.BooleanFalseValues.Count); Assert.AreEqual("Bar", map.ParameterMaps[1].Data.TypeConverterOptions.BooleanTrueValues[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); }
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)); }
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]); }
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]); }