public CovidDataManager() { string path = System.IO.Directory.GetCurrentDirectory() + "\\sources.json"; externalSources = new List <AddedExternalSource>(); if (System.IO.File.Exists(path)) { string json = System.IO.File.ReadAllText(path); externalSources.AddRange(new List <AddedExternalSource>().FromJson(json)); } CsvConfiguration config = new CsvConfiguration(new CultureInfo("en-US", false)); config.HasHeaderRecord = true; config.IgnoreQuotes = false; config.IgnoreBlankLines = true; config.HeaderValidated = null; config.MissingFieldFound = null; config.BadDataFound = null; config.RegisterClassMap <HopkinsDataClassMap>(); config.RegisterClassMap <CovidGermanyStateClassMap>(); config.RegisterClassMap <WhoClassMap>(); config.AutoMap <CovidBrazilStates>(); config.AutoMap <CovidUSStates>(); config.AutoMap <RKIGeneral>(); config.AutoMap <RKILandkreis>(); csvConfig = config; UpdateData(); hopkinsData = new List <HopkinsData>(); UpdateHopkinsAndWHO(); }
/// <summary> /// Writes the header record from the given properties. /// </summary> /// <param name="type">The type of the record.</param> public virtual void WriteHeader(Type type) { if (type == null) { throw new ArgumentNullException(nameof(type)); } if (!configuration.HasHeaderRecord) { throw new CsvWriterException("Configuration.HasHeaderRecord is false. This will need to be enabled to write the header."); } if (hasHeaderBeenWritten) { throw new CsvWriterException("The header record has already been written. You can't write it more than once."); } if (hasRecordBeenWritten) { throw new CsvWriterException("Records have already been written. You can't write the header after writing records has started."); } if (type == typeof(Object)) { return; } if (configuration.Maps[type] == null) { configuration.Maps.Add(configuration.AutoMap(type)); } var properties = new CsvPropertyMapCollection(); AddProperties(properties, configuration.Maps[type]); foreach (var property in properties) { if (CanWrite(property)) { if (property.Data.IndexEnd >= property.Data.Index) { var count = property.Data.IndexEnd - property.Data.Index + 1; for (var i = 1; i <= count; i++) { WriteField(property.Data.Names.FirstOrDefault() + i); } } else { WriteField(property.Data.Names.FirstOrDefault()); } } } hasHeaderBeenWritten = true; }
public CsvStorage() { _conf.AutoMap <CityEvent>(); _conf.IsHeaderCaseSensitive = false; _conf.Delimiter = ";"; _conf.HasHeaderRecord = true; }
public void AutoMapWithExistingMapTest() { var config = new CsvConfiguration(); var existingMap = new SimpleMap(); config.Maps.Add(existingMap); var data = new { Simple = new Simple { Id = 1, Name = "one" } }; var map = config.AutoMap(data.GetType()); Assert.IsNotNull(map); Assert.AreEqual(0, map.PropertyMaps.Count); Assert.AreEqual(1, map.ReferenceMaps.Count); // Since Simple is a reference on the anonymous object, the type won't // be re-used. Types which are created from automapping aren't added // to the list of registered maps either. Assert.IsNotInstanceOfType(map.ReferenceMaps[0].Mapping, typeof(SimpleMap)); }
private void LoadTable <TEntity>(DbSet <TEntity> table) where TEntity : class { var name = typeof(TEntity).Name; var config = new CsvConfiguration(CultureInfo.InvariantCulture) { HasHeaderRecord = true, Delimiter = "\t", Escape = '\\', }; var map = config.AutoMap(typeof(TEntity)); map.ReferenceMaps.Clear(); foreach (var item in map.MemberMaps) { item.TypeConverterOption.NullValues("", "NULL"); } using var reader = new StringReader(Resources.ResourceManager.GetString(name)); using var csv = new CsvReader(reader, config); foreach (var item in csv.GetRecords <TEntity>()) { table.Add(item); } }
public void CircularDependencyWithMultiplePropertiesTest() { var config = new CsvConfiguration(); var map = config.AutoMap <A>(); Assert.AreEqual(1, map.PropertyMaps.Count); Assert.AreEqual(3, map.ReferenceMaps.Count); }
/// <summary> /// Writes the header record from the given properties. /// </summary> /// <param name="type">The type of the record.</param> public virtual void WriteHeader(Type type) { CheckDisposed(); if (type == null) { throw new ArgumentNullException("type"); } if (!configuration.HasHeaderRecord) { throw new CsvWriterException("Configuration.HasHeaderRecord is false. This will need to be enabled to write the header."); } if (hasHeaderBeenWritten) { throw new CsvWriterException("The header record has already been written. You can't write it more than once."); } if (hasRecordBeenWritten) { throw new CsvWriterException("Records have already been written. You can't write the header after writing records has started."); } if (configuration.Maps[type] == null) { configuration.Maps.Add(configuration.AutoMap(type)); } var properties = new CsvPropertyMapCollection(); AddProperties(properties, configuration.Maps[type]); foreach (var property in properties) { if (CanWrite(property)) { WriteField(property.Data.Names.FirstOrDefault()); } } NextRecord(); hasHeaderBeenWritten = true; }
public void AutoMap_WithCultureInfoAttributes_ConfiguresParameterMaps() { var config = new CsvConfiguration(CultureInfo.InvariantCulture); var map = config.AutoMap <Foo>(); 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 AutoMap_WithCultureInfoAttributes_ConfiguresParameterMaps() { var config = new CsvConfiguration(CultureInfo.InvariantCulture); var map = config.AutoMap <Foo>(); Assert.AreEqual(2, map.ParameterMaps.Count); Assert.IsNull(map.ParameterMaps[0].ReferenceMap); Assert.AreEqual("Bar_", map.ParameterMaps[1].ReferenceMap.Data.Prefix); }
public void AutoMap_WithConstantAttributes_ConfiguresParameterMaps() { var config = new CsvConfiguration(CultureInfo.InvariantCulture); var map = config.AutoMap <Foo>(); Assert.AreEqual(2, map.ParameterMaps.Count); Assert.IsFalse(map.ParameterMaps[0].Data.IsOptional); Assert.IsTrue(map.ParameterMaps[1].Data.IsOptional); }
public void AutoMap_WithConstantAttributes_ConfiguresParameterMaps() { var config = new CsvConfiguration(CultureInfo.InvariantCulture); var map = config.AutoMap <Foo>(); Assert.AreEqual(2, map.ParameterMaps.Count); Assert.IsInstanceOfType(map.ParameterMaps[0].Data.TypeConverter, typeof(Int32Converter)); Assert.IsInstanceOfType(map.ParameterMaps[1].Data.TypeConverter, typeof(CustomConverter)); }
public void AutoMap_WithNameAttributes_ConfiguresParameterMaps() { var config = new CsvConfiguration(CultureInfo.InvariantCulture); var map = config.AutoMap <Foo>(); 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 AutoMap_WithIgnoreAttributes_ConfiguresParameterMaps() { var config = new CsvConfiguration(CultureInfo.InvariantCulture); var map = config.AutoMap <Foo>(); Assert.AreEqual(2, map.ParameterMaps.Count); Assert.AreEqual("id", map.ParameterMaps[0].Data.Names.First()); Assert.AreEqual("name", map.ParameterMaps[1].Data.Names.First()); Assert.IsTrue(map.ParameterMaps[1].Data.Ignore); }
public void AutoMap_WithCultureInfoAttributes_ConfiguresParameterMaps() { var config = new CsvConfiguration(CultureInfo.InvariantCulture); var map = config.AutoMap <Foo>(); 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 AutoMap_WithBooleanFalseValuesAttribute_CreatesParameterMaps() { var config = new CsvConfiguration(CultureInfo.InvariantCulture); var map = config.AutoMap <Foo>(); Assert.AreEqual(2, map.ParameterMaps.Count); Assert.AreEqual(0, map.ParameterMaps[0].Data.TypeConverterOptions.NullValues.Count); Assert.AreEqual(1, map.ParameterMaps[1].Data.TypeConverterOptions.NullValues.Count); Assert.AreEqual("NULL", map.ParameterMaps[1].Data.TypeConverterOptions.NullValues[0]); }
public void AutoMapWithCircularDependencyTest() { var config = new CsvConfiguration(); var map = config.AutoMap <ACircular>(); Assert.IsNotNull(map); Assert.AreEqual(1, map.PropertyMaps.Count); Assert.AreEqual(1, map.ReferenceMaps.Count); Assert.AreEqual(1, map.ReferenceMaps[0].Mapping.PropertyMaps.Count); Assert.AreEqual(0, map.ReferenceMaps[0].Mapping.ReferenceMaps.Count); }
public void AutoMapEnumerableTest() { var config = new CsvConfiguration(); try { config.AutoMap(typeof(List <string>)); Assert.Fail(); } catch (CsvConfigurationException) {} }
public void AutoMapEnumerableTest() { var config = new CsvConfiguration(); try { config.AutoMap( typeof( List<string> ) ); Assert.Fail(); } catch( CsvConfigurationException ) { } }
public void AutoMapWithNestedHeaders() { var config = new CsvConfiguration { PrefixReferenceHeaders = true, }; var map = config.AutoMap <Nested>(); Assert.AreEqual("Simple1.Id", map.ReferenceMaps[0].Data.Mapping.PropertyMaps[0].Data.Names[0]); Assert.AreEqual("Simple1.Name", map.ReferenceMaps[0].Data.Mapping.PropertyMaps[1].Data.Names[0]); Assert.AreEqual("Simple2.Id", map.ReferenceMaps[1].Data.Mapping.PropertyMaps[0].Data.Names[0]); Assert.AreEqual("Simple2.Name", map.ReferenceMaps[1].Data.Mapping.PropertyMaps[1].Data.Names[0]); }
public void AutoMapWithExistingMapTest() { var config = new CsvConfiguration(); config.Maps.Add(new SimpleMap()); var data = new { Simple = new Simple { Id = 1, Name = "one" } }; var map = config.AutoMap(data.GetType()); Assert.IsNotNull(map); Assert.AreEqual(0, map.PropertyMaps.Count); Assert.AreEqual(1, map.ReferenceMaps.Count); Assert.IsInstanceOfType(map.ReferenceMaps[0].Mapping, typeof(SimpleMap)); }
/// <summary> /// Creates the read record func for the given type if it /// doesn't already exist. /// </summary> /// <param name="recordType">Type of the record.</param> /// <param name="expressionCompiler">The expression compiler.</param> protected virtual void CreateReadRecordFunc(Type recordType) { if (recordFuncs.ContainsKey(recordType)) { return; } var bindings = new List <MemberBinding>(); if (configuration.Maps[recordType] == null) { configuration.Maps.Add(configuration.AutoMap(recordType)); } CreatePropertyBindingsForMapping(configuration.Maps[recordType], recordType, bindings); var constructorExpression = configuration.Maps[recordType].Constructor ?? Expression.New(recordType); var body = Expression.MemberInit(constructorExpression, bindings); var funcType = typeof(Func <>).MakeGenericType(recordType); recordFuncs[recordType] = Expression.Lambda(funcType, body).Compile(); }
public byte[] GetInvoiceFile(List <InvoiceForDownload> invoices) { using (var stream = new MemoryStream()) using (TextWriter textWriter = new StreamWriter(stream)) { CsvConfiguration configuration = new CsvConfiguration() { Delimiter = ";" }; configuration.RegisterClassMap(configuration.AutoMap <InvoiceForDownload>()); using (var csvWriter = new CsvWriter(textWriter, configuration)) { csvWriter.WriteHeader <InvoiceForDownload>(); foreach (var invoice in invoices) { csvWriter.WriteRecord(invoice); csvWriter.WriteRecords(invoice.Modules); } textWriter.Flush(); stream.Seek(0, SeekOrigin.Begin); return(stream.ToArray()); } } }