Class FixedWidthSchemaBuilder<T>
- Namespace
- Wolfgang.Etl.FixedWidth
- Assembly
- Wolfgang.Etl.FixedWidth.dll
Builds a FixedWidthSchema for T from a fluent, type-safe code
API instead of [FixedWidthField] / [FixedWidthSkip] attributes (#23). Use it when the
record type is not yours to decorate, when you prefer configuration-as-code, or when the layout is
built dynamically. The resulting schema is equivalent to one resolved from attributes — assign it to
Schema / Schema to
use it, or inspect it with Fields / ToDiagram().
public sealed class FixedWidthSchemaBuilder<T> where T : notnull
Type Parameters
TThe record type the schema describes.
- Inheritance
-
FixedWidthSchemaBuilder<T>
- Inherited Members
Examples
var schema = new FixedWidthSchemaBuilder<CustomerRecord>()
.Field(r => r.CustomerId, index: 0, length: 8)
.Field(r => r.Name, index: 1, length: 30)
.Skip(index: 2, length: 5)
.Field(r => r.Balance, index: 3, length: 9, alignment: FieldAlignment.Right, format: "0000000.00")
.Build();
var extractor = new FixedWidthExtractor<CustomerRecord>(reader) { Schema = schema };
Methods
Build()
Validates the accumulated fields and skips and produces the immutable FixedWidthSchema.
public FixedWidthSchema Build()
Returns
- FixedWidthSchema
The resolved schema.
Exceptions
- InvalidOperationException
No fields were defined, or two columns share the same index.
Field<TProperty>(Expression<Func<T, TProperty>>, int, int, FieldAlignment, char, string?, string?, NumberStyles?, bool)
Adds a mapped field. The index is a zero-based column ordinal (matching
[FixedWidthField(index, length)]); start positions are computed by summing the lengths of
all lower-indexed columns, so indexes only need to be unique, not contiguous.
public FixedWidthSchemaBuilder<T> Field<TProperty>(Expression<Func<T, TProperty>> selector, int index, int length, FieldAlignment alignment = FieldAlignment.Left, char pad = ' ', string? format = null, string? header = null, NumberStyles? numberStyles = null, bool trimValue = true)
Parameters
selectorExpression<Func<T, TProperty>>A simple property access expression, e.g.
r => r.Name.indexintThe zero-based column index. Must be unique across fields and skips.
lengthintThe field width in characters. Must be greater than zero.
alignmentFieldAlignmentPadding alignment for writing. Defaults to Left.
padcharPad character for writing. Defaults to
' '.formatstringOptional format string for parsing/formatting the value.
headerstringOptional header label; defaults to the property name when writing headers.
numberStylesNumberStyles?Optional NumberStyles for numeric parsing; defaults to the type's natural style.
trimValueboolWhether to trim whitespace from the extracted value. Defaults to true.
Returns
- FixedWidthSchemaBuilder<T>
The same builder, for chaining.
Type Parameters
TPropertyThe property type.
Exceptions
- ArgumentNullException
selectoris null.- ArgumentException
selectoris not a simple property access.- ArgumentOutOfRangeException
indexis negative orlengthis not positive.- InvalidOperationException
The selected property has no public setter.
Skip(int, int, string?)
Adds a skipped column — a range in the file mapped to no property. Its width still contributes to the positions of later fields.
public FixedWidthSchemaBuilder<T> Skip(int index, int length, string? message = null)
Parameters
indexintThe zero-based column index. Must be unique across fields and skips.
lengthintThe skipped width in characters. Must be greater than zero.
messagestringOptional note describing the skipped column (surfaced by schema introspection).
Returns
- FixedWidthSchemaBuilder<T>
The same builder, for chaining.
Exceptions
- ArgumentOutOfRangeException
indexis negative orlengthis not positive.