Skip to content

Commit

Permalink
fix: apply sequences to numeric fields (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
Seddryck authored Jan 11, 2025
1 parent f6d2bbe commit 57a5583
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,18 @@ public void EmptyNames_ShouldThrow()
var ex = Assert.Throws<ArgumentException>(() => descriptor.Build());
Assert.That(ex!.Message.ToLower(), Does.Contain("empty or null"));
}

[Test]
public void IndexedWithSequences_ShouldSetSequences()
{
var descriptor = new SchemaDescriptorBuilder()
.Indexed()
.WithNumericField<int>((f) => f.WithSequence("NaN", "0"))
.WithField<string>((f) => f.WithSequence("", "Unknown"))
.Build();
Assert.That(descriptor, Is.Not.Null);
Assert.That(descriptor!.Fields, Has.Length.EqualTo(2));
Assert.That(descriptor.Fields[0].Sequences?.Count(), Is.EqualTo(1));
Assert.That(descriptor.Fields[1].Sequences?.Count(), Is.EqualTo(1));
}
}
11 changes: 10 additions & 1 deletion PocketCsvReader/Configuration/NumericFieldDescriptorBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ public NumericFieldDescriptorBuilder WithoutGroupChar()
return this;
}

public new NumericFieldDescriptorBuilder WithName(string value)
=> (NumericFieldDescriptorBuilder)base.WithName(value);

public new NumericFieldDescriptorBuilder WithFormat(string value)
=> (NumericFieldDescriptorBuilder)base.WithFormat(value);

public new NumericFieldDescriptorBuilder WithSequence(string pattern, string? value)
=> (NumericFieldDescriptorBuilder)base.WithSequence(pattern, value);

public override FieldDescriptor Build()
=> new NumericFieldDescriptor(_runtimeType, _name, _format, _decimalChar, _groupChar);
=> new NumericFieldDescriptor(_runtimeType, _name, _format, _sequences?.ToImmutable(), _decimalChar, _groupChar);
}
3 changes: 2 additions & 1 deletion PocketCsvReader/NumericFieldDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ public record NumericFieldDescriptor
Type RuntimeType
, string? Name = null
, string? Format = null
, ImmutableSequenceCollection? Sequences = null
, char ? DecimalChar = null
, char ? GroupChar = null
) : FieldDescriptor(RuntimeType, Name, Format)
) : FieldDescriptor(RuntimeType, Name, Format, Sequences)
{ }

0 comments on commit 57a5583

Please sign in to comment.