Class FixedWidthConverter
- Namespace
- Wolfgang.Etl.FixedWidth
- Assembly
- Wolfgang.Etl.FixedWidth.dll
Provides built-in value converter, header converter, and value parser functions for use with FixedWidthLoader<TRecord, TProgress> and FixedWidthExtractor<TRecord, TProgress>.
public static class FixedWidthConverter
- Inheritance
-
FixedWidthConverter
- Inherited Members
Remarks
Value converters (Func<T1, T2, TResult> of object, FieldContext, string)
convert a typed property value to its string representation. Rules:
- DateTime, DateTimeOffset, and TimeSpan require an explicit Format. An InvalidOperationException is thrown if none is provided.
- All other IFormattable types use InvariantCulture with the optional Format if supplied.
- Null values are converted to Empty.
Header converters (Func<T1, T2, TResult> of string, FieldContext, string)
convert a header label to its padded string representation.
Value parsers (Func<T1, T2, TResult> of string, FieldContext, object)
convert a raw string read from the file back to the target property type.
DefaultParser is the built-in implementation used by
FixedWidthExtractor<TRecord, TProgress> by default.
Fields
DefaultParser
Converts a raw string read from the file to the target property type indicated by PropertyType. This is the default ValueParser.
public static readonly FixedWidthValueParser DefaultParser
Field Value
Examples
// Use DefaultParser explicitly (it is also the default):
extractor.ValueParser = FixedWidthConverter.DefaultParser;
// Handle "Y"/"N" booleans, fall back to DefaultParser for everything else:
extractor.ValueParser = (text, ctx) =>
ctx.PropertyType == typeof(bool)
? (object)(text.Span.SequenceEqual("Y".AsSpan()))
: FixedWidthConverter.DefaultParser(text, ctx);
Remarks
- Nullable types: empty string returns null, non-empty recurses on the underlying type.
- string: returned as-is.
- Empty string for non-nullable value types: returns the default value
(e.g.
0forint). - DateTime, DateTimeOffset, TimeSpan: parsed with Format using InvariantCulture.
- All other types: parsed via TypeDescriptor with InvariantCulture.
Exceptions
- InvalidOperationException
Thrown when parsing a DateTime, DateTimeOffset, or TimeSpan value and no Format has been specified on the field attribute.
- FormatException
Thrown when the raw string value cannot be parsed into the target type because the value does not match the expected format.
Strict
Converts the value to a string and throws a FieldOverflowException if the result exceeds FieldLength. This is the default value converter used by FixedWidthLoader<TRecord, TProgress>.
public static readonly Func<object, FieldContext, string> Strict
Field Value
Examples
// Use Strict explicitly (it is also the default):
loader.ValueConverter = FixedWidthConverter.Strict;
// Fall back to Strict for all types except bool:
loader.ValueConverter = (value, ctx) =>
ctx.PropertyType == typeof(bool)
? ((bool)value ? "Y" : "N")
: FixedWidthConverter.Strict(value, ctx);
Exceptions
- FieldOverflowException
Thrown when the converted string is longer than FieldLength.
- InvalidOperationException
Thrown when converting a DateTime, DateTimeOffset, or TimeSpan value and no Format has been specified on the field attribute.
StrictHeader
Validates that the header label fits within FieldLength, throwing a FieldOverflowException if it does not, then returns the label unchanged. Space-padding is applied by the framework after the converter returns. This is the default header converter used by FixedWidthLoader<TRecord, TProgress>.
public static readonly Func<string, FieldContext, string> StrictHeader
Field Value
Examples
// Use StrictHeader explicitly (it is also the default):
loader.HeaderConverter = FixedWidthConverter.StrictHeader;
// Convert header labels to upper-case, still enforcing the field width:
loader.HeaderConverter = (label, ctx) =>
FixedWidthConverter.StrictHeader(label.ToUpperInvariant(), ctx);
Remarks
The FieldContext is passed as-is from the attribute. If you want different alignment or padding for header cells, supply a custom HeaderConverter.
Exceptions
- FieldOverflowException
Thrown when the header label is longer than FieldLength.
Truncate
Converts the value to a string and silently truncates it to FieldLength if it is too long.
public static readonly Func<object, FieldContext, string> Truncate
Field Value
Examples
// Use Truncate when long values should be silently clipped:
loader.ValueConverter = FixedWidthConverter.Truncate;
Exceptions
- InvalidOperationException
Thrown when converting a DateTime, DateTimeOffset, or TimeSpan value and no Format has been specified on the field attribute.
TruncateHeader
Silently truncates the header label to FieldLength if it is too long, then returns it unchanged otherwise. Space-padding is applied by the framework after the converter returns.
public static readonly Func<string, FieldContext, string> TruncateHeader
Field Value
Examples
// Use TruncateHeader when header labels may exceed the field width:
loader.HeaderConverter = FixedWidthConverter.TruncateHeader;