From db0834f725b798f35badab512a1a7d9a2048a16a Mon Sep 17 00:00:00 2001 From: Travis Parks Date: Sat, 25 May 2019 13:04:02 -0400 Subject: [PATCH] Support pre- and post-parsing and formatting. --- CHANGELOG.md | 18 +++++ FlatFiles.Test/ColumnContextTester.cs | 53 +++++++++---- FlatFiles/ByteColumn.cs | 8 +- FlatFiles/ColumnDefinition.cs | 79 +++++++++++++++++++- FlatFiles/DateTimeColumn.cs | 5 +- FlatFiles/DateTimeOffsetColumn.cs | 4 +- FlatFiles/DecimalColumn.cs | 7 +- FlatFiles/DoubleColumn.cs | 7 +- FlatFiles/FixedLengthOptions.cs | 5 ++ FlatFiles/FlatFiles.csproj | 8 +- FlatFiles/IOptions.cs | 9 ++- FlatFiles/Int16Column.cs | 7 +- FlatFiles/Int32Column.cs | 7 +- FlatFiles/Int64Column.cs | 7 +- FlatFiles/RecordNumberColumn.cs | 5 +- FlatFiles/SByteColumn.cs | 7 +- FlatFiles/SeparatedValueOptions.cs | 5 ++ FlatFiles/SingleColumn.cs | 7 +- FlatFiles/TimeSpanColumn.cs | 8 +- FlatFiles/TypeMapping/ComplexMapperColumn.cs | 25 +++++++ FlatFiles/UInt16Column.cs | 7 +- FlatFiles/UInt32Column.cs | 7 +- FlatFiles/UInt64Column.cs | 7 +- 23 files changed, 237 insertions(+), 65 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 96c1d62..b35ab2e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,21 @@ +## 4.4.0 (2019-05-25) +** Summary ** - Allow pre- and post- parsing and formatting on columns. Allow specifying a global `IFormatProvider` in `IOptions`. + +### New Features +* The `IOptions` interface now has a `IFormatProvider FormatProvider` property. If provided, all columns will automatically use this `IFormatProvider` as their default. + * The `IFormatProvider` specified on a `IColumnDefinition` will override what is specified in `IOptions`. + * If no `IFormatProvider` is found at either level, FlatFiles defaults to `CultureInfo.CurrentInfo`, as before. +* The `IColumnDefinition` interface now exposes four new properties: + * OnParsing - Fires before attempting to parse the `string` value; the logical successor to `Preprocessor`. + * OnParsed - Fires after parsing the column, providing access to the parsed `object`. + * OnFormatting - Fires before formatting the column, providing access to the `object`. + * OnFormatted - Fires after formatting the column, providing access to the generated `string`. + +Unlike `Preprocessor`, each of the new properties have the type `Func`, providing access to the column context. + +### Deprecated +Since `Preprocessor` and `OnParsing` are effectively redundant, `Preprocessor` has been marked deprecated. Please upgrade any existing code to use `OnParsing` instead. The `Preprocessor` property will be removed in the next major release (a.k.a., v5.0). + ## 4.3.4 (2019-03-10) **Summary** - This release allows directly writing arbitrary text to the underlying `TextWriter`, for those who need it. diff --git a/FlatFiles.Test/ColumnContextTester.cs b/FlatFiles.Test/ColumnContextTester.cs index 33dae08..8aeef4d 100644 --- a/FlatFiles.Test/ColumnContextTester.cs +++ b/FlatFiles.Test/ColumnContextTester.cs @@ -129,7 +129,7 @@ internal class Person internal class IndexTrackingColumn : IColumnDefinition { - private readonly IColumnDefinition columnDefinition; + private readonly IColumnDefinition column; private readonly List physicalIndexes; private readonly List logicalIndexes; @@ -138,49 +138,74 @@ internal class IndexTrackingColumn : IColumnDefinition List physicalIndexes, List logicalIndexes) { - this.columnDefinition = columnDefinition; + this.column = columnDefinition; this.physicalIndexes = physicalIndexes; this.logicalIndexes = logicalIndexes; } - public string ColumnName => columnDefinition.ColumnName; + public string ColumnName => column.ColumnName; - public bool IsIgnored => columnDefinition.IsIgnored; + public bool IsIgnored => column.IsIgnored; - public bool IsNullable => columnDefinition.IsNullable; + public bool IsNullable => column.IsNullable; public IDefaultValue DefaultValue { - get => columnDefinition.DefaultValue; - set => columnDefinition.DefaultValue = value; + get => column.DefaultValue; + set => column.DefaultValue = value; } public INullFormatter NullFormatter { - get => columnDefinition.NullFormatter; - set => columnDefinition.NullFormatter = value; + get => column.NullFormatter; + set => column.NullFormatter = value; } + [Obsolete] public Func Preprocessor { - get => columnDefinition.Preprocessor; - set => columnDefinition.Preprocessor = value; + get => column.Preprocessor; + set => column.Preprocessor = value; } - public Type ColumnType => columnDefinition.ColumnType; + public Func OnParsing + { + get => column.OnParsing; + set => column.OnParsing = value; + } + + public Func OnParsed + { + get => column.OnParsed; + set => column.OnParsed = value; + } + + public Func OnFormatting + { + get => column.OnFormatting; + set => column.OnFormatting = value; + } + + public Func OnFormatted + { + get => column.OnFormatted; + set => column.OnFormatted = value; + } + + public Type ColumnType => column.ColumnType; public string Format(IColumnContext context, object value) { physicalIndexes.Add(context.PhysicalIndex); logicalIndexes.Add(context.LogicalIndex); - return columnDefinition.Format(context, value); + return column.Format(context, value); } public object Parse(IColumnContext context, string value) { physicalIndexes.Add(context.PhysicalIndex); logicalIndexes.Add(context.LogicalIndex); - return columnDefinition.Parse(context, value); + return column.Parse(context, value); } } } diff --git a/FlatFiles/ByteColumn.cs b/FlatFiles/ByteColumn.cs index 91c2c55..99be000 100644 --- a/FlatFiles/ByteColumn.cs +++ b/FlatFiles/ByteColumn.cs @@ -40,8 +40,7 @@ public ByteColumn(string columnName) /// The parsed byte value. protected override byte OnParse(IColumnContext context, string value) { - IFormatProvider provider = FormatProvider ?? CultureInfo.CurrentCulture; - return Byte.Parse(value, NumberStyles, provider); + return Byte.Parse(value, NumberStyles, GetFormatProvider(context, FormatProvider)); } /// @@ -52,11 +51,12 @@ protected override byte OnParse(IColumnContext context, string value) /// The formatted value. protected override string OnFormat(IColumnContext context, byte value) { + var provider = GetFormatProvider(context, FormatProvider); if (OutputFormat == null) { - return value.ToString(FormatProvider ?? CultureInfo.CurrentCulture); + return value.ToString(provider); } - return value.ToString(OutputFormat, FormatProvider ?? CultureInfo.CurrentCulture); + return value.ToString(OutputFormat, provider); } } } diff --git a/FlatFiles/ColumnDefinition.cs b/FlatFiles/ColumnDefinition.cs index ac3d26f..dd13129 100644 --- a/FlatFiles/ColumnDefinition.cs +++ b/FlatFiles/ColumnDefinition.cs @@ -1,4 +1,5 @@ using System; +using System.Globalization; using FlatFiles.Properties; namespace FlatFiles @@ -36,8 +37,29 @@ public interface IColumnDefinition /// /// Gets or sets a function used to preprocess input before trying to parse it. /// + [Obsolete("This property has been superseded by the OnParsing delegate.")] Func Preprocessor { get; set; } + /// + /// Gets or sets a function used to pre-process input before trying to parse it. + /// + Func OnParsing { get; set; } + + /// + /// Gets or sets a function used to post-process input after parsing it. + /// + Func OnParsed { get; set; } + + /// + /// Gets or sets a function used to pre-process output before trying to format it. + /// + Func OnFormatting { get; set; } + + /// + /// Gets or sets a function used to post-process output after formatting it. + /// + Func OnFormatted { get; set; } + /// /// Gets the type of the values in the column. /// @@ -139,6 +161,26 @@ public INullFormatter NullFormatter /// public Func Preprocessor { get; set; } + /// + /// Gets or sets a function used to pre-process input before trying to parse it. + /// + public Func OnParsing { get; set; } + + /// + /// Gets or sets a function used to post-process input after parsing it. + /// + public Func OnParsed { get; set; } + + /// + /// Gets or sets a function used to pre-process output before trying to format it. + /// + public Func OnFormatting { get; set; } + + /// + /// Gets or sets a function used to post-process output after formatting it. + /// + public Func OnFormatted { get; set; } + /// /// Gets the type of the values in the column. /// @@ -169,6 +211,21 @@ protected internal static string TrimValue(string value) /// The object to format. /// The formatted value. public abstract string Format(IColumnContext context, object value); + + /// + /// Gets the format provider to use. If the given provider is not null, it will be used. + /// Otherwise, the format provider set on the options object will be used. As a last resort, + /// the current culture specified by the operating system will be used. + /// + /// The current column context. + /// The format provider set on the column. + /// The format provider to use. + protected static IFormatProvider GetFormatProvider(IColumnContext context, IFormatProvider formatProvider) + { + return formatProvider + ?? context?.RecordContext.ExecutionContext.Options.FormatProvider + ?? CultureInfo.CurrentCulture; + } } /// @@ -203,6 +260,10 @@ public override object Parse(IColumnContext context, string value) { value = Preprocessor(value); } + if (OnParsing != null) + { + value = OnParsing(context, value); + } if (NullFormatter.IsNullValue(context, value)) { if (IsNullable) @@ -212,7 +273,12 @@ public override object Parse(IColumnContext context, string value) return DefaultValue.GetDefaultValue(context); // Should we check for the expected type? } string trimmed = IsTrimmed ? TrimValue(value) : value; - return OnParse(context, trimmed); + object result = OnParse(context, trimmed); + if (OnParsed != null) + { + result = OnParsed(context, result); + } + return result; } /// @@ -236,11 +302,20 @@ public override object Parse(IColumnContext context, string value) /// The formatted value. public override string Format(IColumnContext context, object value) { + if (OnFormatting != null) + { + value = OnFormatting(context, value); + } if (value == null) { return NullFormatter.FormatNull(context); } - return OnFormat(context, (T)value); + string result = OnFormat(context, (T)value); + if (OnFormatted != null) + { + result = OnFormatted(context, result); + } + return result; } /// diff --git a/FlatFiles/DateTimeColumn.cs b/FlatFiles/DateTimeColumn.cs index df84980..58a1acf 100644 --- a/FlatFiles/DateTimeColumn.cs +++ b/FlatFiles/DateTimeColumn.cs @@ -56,11 +56,12 @@ protected override DateTime OnParse(IColumnContext context, string value) /// The formatted value. protected override string OnFormat(IColumnContext context, DateTime value) { + var provider = GetFormatProvider(context, FormatProvider); if (OutputFormat == null) { - return value.ToString(FormatProvider ?? CultureInfo.CurrentCulture); + return value.ToString(provider); } - return value.ToString(OutputFormat, FormatProvider ?? CultureInfo.CurrentCulture); + return value.ToString(OutputFormat, provider); } } } diff --git a/FlatFiles/DateTimeOffsetColumn.cs b/FlatFiles/DateTimeOffsetColumn.cs index 05b78a1..629a529 100644 --- a/FlatFiles/DateTimeOffsetColumn.cs +++ b/FlatFiles/DateTimeOffsetColumn.cs @@ -35,7 +35,7 @@ public DateTimeOffsetColumn(string columnName) /// protected override DateTimeOffset OnParse(IColumnContext context, string value) { - var provider = FormatProvider ?? CultureInfo.CurrentCulture; + var provider = GetFormatProvider(context, FormatProvider); if (InputFormat == null) { return DateTimeOffset.Parse(value, provider); @@ -46,7 +46,7 @@ protected override DateTimeOffset OnParse(IColumnContext context, string value) /// protected override string OnFormat(IColumnContext context, DateTimeOffset value) { - var provider = FormatProvider ?? CultureInfo.CurrentCulture; + var provider = GetFormatProvider(context, FormatProvider); if (OutputFormat == null) { return value.ToString(provider); diff --git a/FlatFiles/DecimalColumn.cs b/FlatFiles/DecimalColumn.cs index 6f29412..e3dd181 100644 --- a/FlatFiles/DecimalColumn.cs +++ b/FlatFiles/DecimalColumn.cs @@ -40,7 +40,7 @@ public DecimalColumn(string columnName) /// The parsed Decimal. protected override decimal OnParse(IColumnContext context, string value) { - var provider = FormatProvider ?? CultureInfo.CurrentCulture; + var provider = GetFormatProvider(context, FormatProvider); return Decimal.Parse(value, NumberStyles, provider); } @@ -52,11 +52,12 @@ protected override decimal OnParse(IColumnContext context, string value) /// The formatted value. protected override string OnFormat(IColumnContext context, decimal value) { + var provider = GetFormatProvider(context, FormatProvider); if (OutputFormat == null) { - return value.ToString(FormatProvider ?? CultureInfo.CurrentCulture); + return value.ToString(provider); } - return value.ToString(OutputFormat, FormatProvider ?? CultureInfo.CurrentCulture); + return value.ToString(OutputFormat, provider); } } } diff --git a/FlatFiles/DoubleColumn.cs b/FlatFiles/DoubleColumn.cs index 84d5c06..6f68535 100644 --- a/FlatFiles/DoubleColumn.cs +++ b/FlatFiles/DoubleColumn.cs @@ -40,7 +40,7 @@ public DoubleColumn(string columnName) /// The parsed Double. protected override double OnParse(IColumnContext context, string value) { - var provider = FormatProvider ?? CultureInfo.CurrentCulture; + var provider = GetFormatProvider(context, FormatProvider); return Double.Parse(value, NumberStyles, provider); } @@ -52,11 +52,12 @@ protected override double OnParse(IColumnContext context, string value) /// The formatted value. protected override string OnFormat(IColumnContext context, double value) { + var provider = GetFormatProvider(context, FormatProvider); if (OutputFormat == null) { - return value.ToString(FormatProvider ?? CultureInfo.CurrentCulture); + return value.ToString(provider); } - return value.ToString(OutputFormat, FormatProvider ?? CultureInfo.CurrentCulture); + return value.ToString(OutputFormat, provider); } } } diff --git a/FlatFiles/FixedLengthOptions.cs b/FlatFiles/FixedLengthOptions.cs index e9ce022..0e0ed50 100644 --- a/FlatFiles/FixedLengthOptions.cs +++ b/FlatFiles/FixedLengthOptions.cs @@ -88,6 +88,11 @@ public OverflowTruncationPolicy TruncationPolicy /// public bool IsColumnContextDisabled { get; set; } + /// + /// Gets or sets the global, default format provider. + /// + public IFormatProvider FormatProvider { get; set; } + /// /// Duplicates the options. /// diff --git a/FlatFiles/FlatFiles.csproj b/FlatFiles/FlatFiles.csproj index beefe35..2b2f694 100644 --- a/FlatFiles/FlatFiles.csproj +++ b/FlatFiles/FlatFiles.csproj @@ -10,10 +10,10 @@ https://github.com/jehugaleahsa/FlatFiles.git git csv;comma;tab;separated;value;delimited;flat;file;fixed;width;fixed-width;length;fixed-length;parser;parsing;parse - FixedLengthReader not reading records with metadata. + Support pre- and post-parsing and formatting operations. Support overriding IFormatProvider globally in IOptions. true FlatFiles.snk - 4.3.4 + 4.4.0 @@ -27,8 +27,8 @@ 7.3 https://raw.githubusercontent.com/jehugaleahsa/FlatFiles/master/icon.png - 4.3.4.0 - 4.3.4.0 + 4.4.0.0 + 4.4.0.0 diff --git a/FlatFiles/IOptions.cs b/FlatFiles/IOptions.cs index ab809ab..b7df82b 100644 --- a/FlatFiles/IOptions.cs +++ b/FlatFiles/IOptions.cs @@ -1,4 +1,6 @@ -namespace FlatFiles +using System; + +namespace FlatFiles { /// /// Represents reader/writer options that are common among file types. @@ -14,5 +16,10 @@ public interface IOptions /// Gets whether column-level metadata should be disabled for non-metadata columns. /// bool IsColumnContextDisabled { get; } + + /// + /// Gets the global, default format provider to use. + /// + IFormatProvider FormatProvider { get; } } } diff --git a/FlatFiles/Int16Column.cs b/FlatFiles/Int16Column.cs index bd37547..f08e960 100644 --- a/FlatFiles/Int16Column.cs +++ b/FlatFiles/Int16Column.cs @@ -40,7 +40,7 @@ public Int16Column(string columnName) /// The parsed Int16. protected override short OnParse(IColumnContext context, string value) { - var provider = FormatProvider ?? CultureInfo.CurrentCulture; + var provider = GetFormatProvider(context, FormatProvider); return Int16.Parse(value, NumberStyles, provider); } @@ -52,11 +52,12 @@ protected override short OnParse(IColumnContext context, string value) /// The formatted value. protected override string OnFormat(IColumnContext context, short value) { + var provider = GetFormatProvider(context, FormatProvider); if (OutputFormat == null) { - return value.ToString(FormatProvider ?? CultureInfo.CurrentCulture); + return value.ToString(provider); } - return value.ToString(OutputFormat, FormatProvider ?? CultureInfo.CurrentCulture); + return value.ToString(OutputFormat, provider); } } } diff --git a/FlatFiles/Int32Column.cs b/FlatFiles/Int32Column.cs index 887726c..6afa094 100644 --- a/FlatFiles/Int32Column.cs +++ b/FlatFiles/Int32Column.cs @@ -40,7 +40,7 @@ public Int32Column(string columnName) /// The parsed Int32. protected override int OnParse(IColumnContext context, string value) { - var provider = FormatProvider ?? CultureInfo.CurrentCulture; + var provider = GetFormatProvider(context, FormatProvider); return Int32.Parse(value, NumberStyles, provider); } @@ -52,11 +52,12 @@ protected override int OnParse(IColumnContext context, string value) /// The formatted value. protected override string OnFormat(IColumnContext context, int value) { + var provider = GetFormatProvider(context, FormatProvider); if (OutputFormat == null) { - return value.ToString(FormatProvider ?? CultureInfo.CurrentCulture); + return value.ToString(provider); } - return value.ToString(OutputFormat, FormatProvider ?? CultureInfo.CurrentCulture); + return value.ToString(OutputFormat, provider); } } } diff --git a/FlatFiles/Int64Column.cs b/FlatFiles/Int64Column.cs index 527e491..b61c44d 100644 --- a/FlatFiles/Int64Column.cs +++ b/FlatFiles/Int64Column.cs @@ -40,7 +40,7 @@ public Int64Column(string columnName) /// The parsed Int64. protected override long OnParse(IColumnContext context, string value) { - var provider = FormatProvider ?? CultureInfo.CurrentCulture; + var provider = GetFormatProvider(context, FormatProvider); return Int64.Parse(value, NumberStyles, provider); } @@ -52,11 +52,12 @@ protected override long OnParse(IColumnContext context, string value) /// The formatted value. protected override string OnFormat(IColumnContext context, long value) { + var provider = GetFormatProvider(context, FormatProvider); if (OutputFormat == null) { - return value.ToString(FormatProvider ?? CultureInfo.CurrentCulture); + return value.ToString(provider); } - return value.ToString(OutputFormat, FormatProvider ?? CultureInfo.CurrentCulture); + return value.ToString(OutputFormat, provider); } } } diff --git a/FlatFiles/RecordNumberColumn.cs b/FlatFiles/RecordNumberColumn.cs index 4b4dc52..fa89240 100644 --- a/FlatFiles/RecordNumberColumn.cs +++ b/FlatFiles/RecordNumberColumn.cs @@ -50,11 +50,12 @@ public RecordNumberColumn(string columnName) protected override string OnFormat(IColumnContext context) { var recordNumber = GetRecordNumber(context); + var provider = GetFormatProvider(context, FormatProvider); if (OutputFormat == null) { - return recordNumber.ToString(FormatProvider ?? CultureInfo.CurrentCulture); + return recordNumber.ToString(provider); } - return recordNumber.ToString(OutputFormat, FormatProvider ?? CultureInfo.CurrentCulture); + return recordNumber.ToString(OutputFormat, provider); } /// diff --git a/FlatFiles/SByteColumn.cs b/FlatFiles/SByteColumn.cs index 7296ae7..9e952a1 100644 --- a/FlatFiles/SByteColumn.cs +++ b/FlatFiles/SByteColumn.cs @@ -40,7 +40,7 @@ public SByteColumn(string columnName) /// The parsed signed byte value. protected override sbyte OnParse(IColumnContext context, string value) { - var provider = FormatProvider ?? CultureInfo.CurrentCulture; + var provider = GetFormatProvider(context, FormatProvider); return SByte.Parse(value, NumberStyles, provider); } @@ -52,11 +52,12 @@ protected override sbyte OnParse(IColumnContext context, string value) /// The formatted value. protected override string OnFormat(IColumnContext context, sbyte value) { + var provider = GetFormatProvider(context, FormatProvider); if (OutputFormat == null) { - return value.ToString(FormatProvider ?? CultureInfo.CurrentCulture); + return value.ToString(provider); } - return value.ToString(OutputFormat, FormatProvider ?? CultureInfo.CurrentCulture); + return value.ToString(OutputFormat, provider); } } } diff --git a/FlatFiles/SeparatedValueOptions.cs b/FlatFiles/SeparatedValueOptions.cs index e4b137f..477d6b0 100644 --- a/FlatFiles/SeparatedValueOptions.cs +++ b/FlatFiles/SeparatedValueOptions.cs @@ -69,6 +69,11 @@ public string Separator /// public bool IsColumnContextDisabled { get; set; } + /// + /// Gets or sets the global, default format provider. + /// + public IFormatProvider FormatProvider { get; set; } + /// /// Duplicates the options. /// diff --git a/FlatFiles/SingleColumn.cs b/FlatFiles/SingleColumn.cs index f7c0d0e..fb38d8d 100644 --- a/FlatFiles/SingleColumn.cs +++ b/FlatFiles/SingleColumn.cs @@ -40,7 +40,7 @@ public SingleColumn(string columnName) /// The parsed Single. protected override float OnParse(IColumnContext context, string value) { - var provider = FormatProvider ?? CultureInfo.CurrentCulture; + var provider = GetFormatProvider(context, FormatProvider); return Single.Parse(value, NumberStyles, provider); } @@ -52,11 +52,12 @@ protected override float OnParse(IColumnContext context, string value) /// The formatted value. protected override string OnFormat(IColumnContext context, float value) { + var provider = GetFormatProvider(context, FormatProvider); if (OutputFormat == null) { - return value.ToString(FormatProvider ?? CultureInfo.CurrentCulture); + return value.ToString(provider); } - return value.ToString(OutputFormat, FormatProvider ?? CultureInfo.CurrentCulture); + return value.ToString(OutputFormat, provider); } } } diff --git a/FlatFiles/TimeSpanColumn.cs b/FlatFiles/TimeSpanColumn.cs index 6d02037..01761f7 100644 --- a/FlatFiles/TimeSpanColumn.cs +++ b/FlatFiles/TimeSpanColumn.cs @@ -125,12 +125,12 @@ public static IColumnDefinition FromTicks(Int64Column column) /// protected override TimeSpan OnParse(IColumnContext context, string value) { - var formatProvider = FormatProvider ?? CultureInfo.CurrentCulture; + var provider = GetFormatProvider(context, FormatProvider); if (InputFormat == null) { - return TimeSpan.Parse(value, formatProvider); + return TimeSpan.Parse(value, provider); } - return TimeSpan.ParseExact(value, InputFormat, formatProvider); + return TimeSpan.ParseExact(value, InputFormat, provider); } /// @@ -140,7 +140,7 @@ protected override string OnFormat(IColumnContext context, TimeSpan value) { return value.ToString(); } - return value.ToString(OutputFormat, FormatProvider ?? CultureInfo.CurrentCulture); + return value.ToString(OutputFormat, GetFormatProvider(context, FormatProvider)); } } } diff --git a/FlatFiles/TypeMapping/ComplexMapperColumn.cs b/FlatFiles/TypeMapping/ComplexMapperColumn.cs index b08caa1..1aac923 100644 --- a/FlatFiles/TypeMapping/ComplexMapperColumn.cs +++ b/FlatFiles/TypeMapping/ComplexMapperColumn.cs @@ -37,12 +37,37 @@ public INullFormatter NullFormatter set => column.NullFormatter = value; } + [Obsolete] public Func Preprocessor { get => column.Preprocessor; set => column.Preprocessor = value; } + public Func OnParsing + { + get => column.OnParsing; + set => column.OnParsing = value; + } + + public Func OnParsed + { + get => column.OnParsed; + set => column.OnParsed = value; + } + + public Func OnFormatting + { + get => column.OnFormatting; + set => column.OnFormatting = value; + } + + public Func OnFormatted + { + get => column.OnFormatted; + set => column.OnFormatted = value; + } + public object Parse(IColumnContext context, string value) { object parsed = column.Parse(context, value); diff --git a/FlatFiles/UInt16Column.cs b/FlatFiles/UInt16Column.cs index 32ce157..13b7858 100644 --- a/FlatFiles/UInt16Column.cs +++ b/FlatFiles/UInt16Column.cs @@ -40,7 +40,7 @@ public UInt16Column(string columnName) /// The parsed UInt16. protected override ushort OnParse(IColumnContext context, string value) { - var provider = FormatProvider ?? CultureInfo.CurrentCulture; + var provider = GetFormatProvider(context, FormatProvider); return UInt16.Parse(value, NumberStyles, provider); } @@ -52,11 +52,12 @@ protected override ushort OnParse(IColumnContext context, string value) /// The formatted value. protected override string OnFormat(IColumnContext context, ushort value) { + var provider = GetFormatProvider(context, FormatProvider); if (OutputFormat == null) { - return value.ToString(FormatProvider ?? CultureInfo.CurrentCulture); + return value.ToString(provider); } - return value.ToString(OutputFormat, FormatProvider ?? CultureInfo.CurrentCulture); + return value.ToString(OutputFormat, provider); } } } diff --git a/FlatFiles/UInt32Column.cs b/FlatFiles/UInt32Column.cs index d44af4b..6cc6574 100644 --- a/FlatFiles/UInt32Column.cs +++ b/FlatFiles/UInt32Column.cs @@ -43,7 +43,7 @@ public UInt32Column(string columnName) /// The parsed UInt32. protected override uint OnParse(IColumnContext context, string value) { - var provider = FormatProvider ?? CultureInfo.CurrentCulture; + var provider = GetFormatProvider(context, FormatProvider); return UInt32.Parse(value, NumberStyles, provider); } @@ -56,11 +56,12 @@ protected override uint OnParse(IColumnContext context, string value) /// The formatted value. protected override string OnFormat(IColumnContext context, uint value) { + var provider = GetFormatProvider(context, FormatProvider); if (OutputFormat == null) { - return value.ToString(FormatProvider ?? CultureInfo.CurrentCulture); + return value.ToString(provider); } - return value.ToString(OutputFormat, FormatProvider ?? CultureInfo.CurrentCulture); + return value.ToString(OutputFormat, provider); } } } diff --git a/FlatFiles/UInt64Column.cs b/FlatFiles/UInt64Column.cs index b5be30d..fcac9f7 100644 --- a/FlatFiles/UInt64Column.cs +++ b/FlatFiles/UInt64Column.cs @@ -40,7 +40,7 @@ public UInt64Column(string columnName) /// The parsed UInt64. protected override ulong OnParse(IColumnContext context, string value) { - var provider = FormatProvider ?? CultureInfo.CurrentCulture; + var provider = GetFormatProvider(context, FormatProvider); return UInt64.Parse(value, NumberStyles, provider); } @@ -52,11 +52,12 @@ protected override ulong OnParse(IColumnContext context, string value) /// The formatted value. protected override string OnFormat(IColumnContext context, ulong value) { + var provider = GetFormatProvider(context, FormatProvider); if (OutputFormat == null) { - return value.ToString(FormatProvider ?? CultureInfo.CurrentCulture); + return value.ToString(provider); } - return value.ToString(OutputFormat, FormatProvider ?? CultureInfo.CurrentCulture); + return value.ToString(OutputFormat, provider); } } }