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

T

The 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

selector Expression<Func<T, TProperty>>

A simple property access expression, e.g. r => r.Name.

index int

The zero-based column index. Must be unique across fields and skips.

length int

The field width in characters. Must be greater than zero.

alignment FieldAlignment

Padding alignment for writing. Defaults to Left.

pad char

Pad character for writing. Defaults to ' '.

format string

Optional format string for parsing/formatting the value.

header string

Optional header label; defaults to the property name when writing headers.

numberStyles NumberStyles?

Optional NumberStyles for numeric parsing; defaults to the type's natural style.

trimValue bool

Whether to trim whitespace from the extracted value. Defaults to true.

Returns

FixedWidthSchemaBuilder<T>

The same builder, for chaining.

Type Parameters

TProperty

The property type.

Exceptions

ArgumentNullException

selector is null.

ArgumentException

selector is not a simple property access.

ArgumentOutOfRangeException

index is negative or length is 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

index int

The zero-based column index. Must be unique across fields and skips.

length int

The skipped width in characters. Must be greater than zero.

message string

Optional note describing the skipped column (surfaced by schema introspection).

Returns

FixedWidthSchemaBuilder<T>

The same builder, for chaining.

Exceptions

ArgumentOutOfRangeException

index is negative or length is not positive.