/// <summary>Initializes the converter with an attribute</summary> public override void Initialize(CsvConverterAttribute attribute, IDefaultTypeConverterFactory defaultFactory) { base.Initialize(attribute, defaultFactory); if (!(attribute is CsvConverterNumberAttribute oneAttribute)) { throw new CsvConverterAttributeException( $"Please use the {nameof(CsvConverterNumberAttribute)} " + $"attribute with the {nameof(CsvConverterPercentage)} converter."); } if (string.IsNullOrWhiteSpace(oneAttribute.StringFormat)) { int precession = oneAttribute.NumberOfDecimalPlaces - 2; if (precession < 0) { precession = 0; } oneAttribute.StringFormat = $"P{precession}"; } _decimalConverter = defaultFactory.CreateConverter(typeof(decimal)); _decimalConverter.Initialize(oneAttribute, defaultFactory); }
public List <T> ReadCsvFile <T>(string validatedFileName, ICsvConverter <T> converter) where T : class { using (var reader = File.OpenText(validatedFileName)) { return(ReadCsvFile(reader, converter)); } }
public CitizenshipService(ILogger <CitizenshipService> logger, IOptions <ResourcesOptions> resourcesOptions, ICsvConverter csvConverter) { _logger = logger; _csvConverter = csvConverter; _resourcesOptions = resourcesOptions.Value; }
public List <T> ReadCsvFile <T>(StreamReader reader, ICsvConverter <T> converter) where T : class { var dataList = new List <T>(); var lineCount = 0; while (!reader.EndOfStream) { var csvData = reader.ReadLine(); lineCount++; if (lineCount > 10 && dataList.Count == 0) { throw new Exception("Too many errors occured trying to read file."); } if (lineCount == 1) { //Assume header line and ignore continue; } try { var data = converter.Convert(lineCount, csvData); dataList.Add(data); } catch (Exception) { _logger.LogWarning($"Invalid data on line {lineCount}"); } } return(dataList); }
/// <summary>Looks for CsvConverterAttribute on the property using PropertyInfo /// and then updates any relevant info on the map</summary> /// <param name="oneMap">The property map to examine</param> private void CreateAllUserSpecifiedConvertersForOneProperty(ColumnToPropertyMap oneMap) { List <CsvConverterAttribute> attributeList = oneMap.PropInformation.HelpFindAllAttributes <CsvConverterAttribute>(); foreach (var oneAttribute in attributeList) { // In the case where the user wants to be explicit about ignoring a property, let them. // So, we will NOT have a converter for reading or writing according to this attribute. if (oneAttribute.IgnoreWhenReading && oneAttribute.IgnoreWhenWriting) { oneMap.IgnoreWhenReading = true; oneMap.IgnoreWhenWriting = true; continue; } ICsvConverter converter = oneAttribute.CreateConverterForProperty(_theClassType, oneMap.PropInformation, _defaultFactory); bool isTypeConverter = CreateOneUserSpecifiedConverterForOneProperty(oneMap, converter, oneAttribute, true); // Only update column information for TYPE converters. // Pre and Post conveters should NOT contain column information! if (isTypeConverter) { UpdateColumnInformation(oneMap, oneAttribute); } } }
public InstallService( ICsvConverter csvConverter, IProductService cumulusDbContext, ILambdaContext lambdaContext) { this.csvConverter = csvConverter; this.cumulusDbContext = cumulusDbContext; this.lambdaContext = lambdaContext; }
public XetraService( IRepository repository, IProductService productService, IAmazonS3 amazonS3, ICsvConverter csvConverter, IEncryptionService encryptionService) : base(repository, amazonS3, productService, csvConverter, encryptionService) { }
private void CreateConverter(Type inputType, CsvConverterNumberAttribute oneAttribute, IDefaultTypeConverterFactory defaultFactory) { ICsvConverter converter = defaultFactory.CreateConverter(inputType); converter.Initialize(oneAttribute, defaultFactory); _converterDictionary.Add(inputType, converter); }
public MainViewModel(ICsvConverter csvConverter, IDataService dataService) { _csvConverter = csvConverter ?? throw new System.ArgumentNullException(nameof(csvConverter)); _dataService = dataService ?? throw new System.ArgumentNullException(nameof(dataService)); Separator = ';'; SeparatorVariants = new List <char> { ';', ',' }; }
public CsvToJsonConverterEngine( IConfigurationSection configurationSection, IRmqConsumer rmqConsumer, IRmqPublisher rmqPublisher, ICsvConverter csvToJsonParser) { this.configurationSection = configurationSection; this.rmqConsumer = rmqConsumer; this.rmqPublisher = rmqPublisher; this.csvToJsonParser = csvToJsonParser; }
public BaseStockService( IRepository repository, IAmazonS3 amazonS3, IProductService productService, ICsvConverter csvConverter, IEncryptionService encryptionService) { this.repository = repository; this.amazonS3 = amazonS3; this.productService = productService; this.csvConverter = csvConverter; this.encryptionService = encryptionService; }
private void CreateDefaultConverterForOnePropertyIfNecessary(ColumnToPropertyMap newMap) { // User doesn't want to use this column if (newMap.IgnoreWhenReading && newMap.IgnoreWhenWriting) { return; } // No converter specified for a type we understand if (newMap.ReadConverter == null && newMap.WriteConverter == null && IsTypeAllowed(newMap.PropInformation.PropertyType)) { ICsvConverter converter = _defaultFactory.CreateConverter(newMap.PropInformation.PropertyType); newMap.ReadConverter = newMap.IgnoreWhenReading ? null : converter; newMap.WriteConverter = newMap.IgnoreWhenWriting ? null : converter; } }
/// <summary>Initializes the converter with an attribute</summary> public override void Initialize(CsvConverterAttribute attribute, IDefaultTypeConverterFactory defaultFactory) { base.Initialize(attribute, defaultFactory); if (!(attribute is CsvConverterNumberAttribute oneAttribute)) { throw new CsvConverterAttributeException( $"Please use the {nameof(CsvConverterNumberAttribute)} " + $"attribute with the {nameof(CsvConverterDecimalToInt)} converter."); } _oneAttribute = oneAttribute; _writeDecimalConverter = defaultFactory.CreateConverter(typeof(decimal)); _writeDecimalConverter.Initialize(oneAttribute, defaultFactory); oneAttribute.NumberOfDecimalPlaces = 0; _readDecimalConverter = defaultFactory.CreateConverter(typeof(decimal)); _readDecimalConverter.Initialize(oneAttribute, defaultFactory); }
public string ConvertData(object[] csvRows) { if (csvRows.Length == 0) { return(string.Empty); } ICsvConverter row = (ICsvConverter)csvRows[0]; StringBuilder s = new StringBuilder(); s.Append(string.Join("\",\"", row.ColumnHeaders)); s.Append("\r\n"); for (int x = 0; x <= csvRows.GetUpperBound(0); x++) { row = (ICsvConverter)csvRows[x]; s.Append(string.Join("\",\"", row.ColumnData)); s.Append("\r\n"); } return(s.ToString()); }
public CsvDataSource(ICsvParser csvParser, ICsvConverter csvConverter) { _csvParser = csvParser; _csvConverter = csvConverter; }
public CsvDataSource() { _csvParser = new CsvParser(); _csvConverter = new CsvConverter(); _fileStorage = new FileStorage(); }
/// <summary> /// Sets the value of the current csv record at the specified index. /// </summary> /// <typeparam name="T">The type</typeparam> /// <param name="index">Index within the current csv record</param> /// <param name="value">A value</param> public void SetValue <T>(int index, T value, ICsvConverter valueConverter) { this[index] = valueConverter.ToString(value); }
/// <summary> /// Read the typed value of the current csv record at the specified index. /// </summary> /// <typeparam name="T">The type</typeparam> /// <param name="index">Index within the current csv record</param> /// <param name="converter">INstance of value converter to be used</param> /// <returns>A typed value</returns> public T GetValue <T>(int index, ICsvConverter converter) { return((T)GetValue(index, converter)); }
private bool CreateOneUserSpecifiedConverterForOneProperty(ColumnToPropertyMap oneMap, ICsvConverter converter, CsvConverterAttribute oneAttribute, bool throwExceptionIfConverterHasAlreadyBeenSpecified) { // Possible pre or post converter. if (oneAttribute is CsvConverterStringAttribute stringConverter && (stringConverter.IsPreConverter || stringConverter.IsPostConverter)) { AddOnePropertyPreOrPostConverter(oneMap, converter, stringConverter); return(false); }
public CsvConverterTest() { _csvConverter = new CsvConverter(); }
public static void AddConverter <T>(this CsvAccess csvAccess, ICsvConverter valueConverter) { csvAccess.ConverterResolver.AddConverter <T>(valueConverter); }
/// <summary> /// /// </summary> /// <param name="index"></param> /// <param name="converter"></param> /// <returns></returns> public object GetValue(int index, ICsvConverter converter) { return(converter.FromString(this[index])); }
/// <summary> /// /// </summary> /// <param name="name"></param> /// <param name="converter"></param> /// <returns></returns> public object GetValue(string name, ICsvConverter converter) { return(converter.FromString(this[name])); }
/// <summary> /// Sets the value of the specified key. /// </summary> /// <typeparam name="T">The type</typeparam> /// <param name="key">Key name</param> /// <param name="value">The value</param> public void SetValue <T>(string key, T value, ICsvConverter valueConverter) { this[key] = valueConverter.ToString(value); }
/// <summary> /// Reads the typed value of the current csv record at the posiiton of the specified header name. /// </summary> /// <typeparam name="T">The type</typeparam> /// <param name="name">Name of a header</param> /// <returns>A typed value</returns> public T GetValue <T>(string name, ICsvConverter converter) { return((T)GetValue(name, converter)); }
/// <summary> /// カラム情報を追加。 /// </summary> /// <param name="columnName">カラム(ヘッダ)名</param> /// <param name="propertyExpression">このカラムと紐付けを行うプロパティ</param> /// <param name="converter">値の変換を行うオブジェクト</param> public void Add(string columnName, Expression <Func <T, object> > propertyExpression, ICsvConverter <T> converter) { this.Add(columnName, propertyExpression, converter, null, false); }
/// <summary> /// Registers a new value converter for a given type /// </summary> /// <param name="type">The type</param> /// <param name="converter">The converter instance</param> public void AddConverter(Type type, ICsvConverter converter) { _converters[type] = converter; }
/// <summary> /// カラム情報を追加。 /// </summary> /// <param name="columnName"></param> /// <param name="propertyExpression"></param> /// <param name="converter"></param> /// <param name="constantValue"></param> /// <param name="isContantValue"></param> void Add(string columnName, Expression <Func <T, object> > propertyExpression, ICsvConverter <T> converter, string constantValue, bool isContantValue) { this._columns.Add(new CsvColumn <T>() { ColumnName = columnName, Property = propertyExpression, Converter = converter, IsConstantValue = isContantValue, ConstantValue = constantValue }); }
/// <summary> /// Sets the value of the current csv record at the position of the specified header name. /// </summary> /// <typeparam name="T">The type</typeparam> /// <param name="name">Name of a header</param> /// <param name="value">A value</param> public void SetValue <T>(string name, T value, ICsvConverter valueConverter) { this[name] = valueConverter.ToString(value); }
public MarketDataContext(IFileService fileService, ICsvConverter <MarketData> csvConverter) { _fileService = fileService; _csvConverter = csvConverter; }