Skip to content

Commit

Permalink
feat: reinforce cohesion between named and indexed schema builders (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
Seddryck authored Jan 5, 2025
1 parent 0bb3792 commit f15eb30
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,32 @@ public void NamedWithFields_ShouldSetFields()
Assert.That(field.Name, Is.Not.Null.Or.Empty);
}

private static IEnumerable<ISchemaDescriptorBuilder> CreateBuilder()
{
yield return new SchemaDescriptorBuilder().Named();
yield return new SchemaDescriptorBuilder().Indexed();
}

[Test]
[TestCaseSource(nameof(CreateBuilder))]
public void InterfaceWithFields_ShouldSetFields(ISchemaDescriptorBuilder builder)
{
builder.WithField(typeof(int), "foo", (x) => x);
builder.WithField(typeof(DateTime), "bar", (f) => f.WithFormat("%Y-%M-%d"));
var descriptor = builder.Build();
Assert.That(descriptor, Is.Not.Null);
Assert.That(descriptor!.Fields, Has.Length.EqualTo(2));
Assert.That(descriptor.Fields["foo"].RuntimeType, Is.EqualTo(typeof(int)));
Assert.That(descriptor.Fields["foo"].Name, Is.EqualTo("foo"));
Assert.That(descriptor.Fields["foo"].Format, Is.Null);
Assert.That(descriptor.Fields["bar"].RuntimeType, Is.EqualTo(typeof(DateTime)));
Assert.That(descriptor.Fields["bar"].Name, Is.EqualTo("bar"));
Assert.That(descriptor.Fields["bar"].Format, Is.EqualTo("%Y-%M-%d"));

foreach (var field in descriptor.Fields)
Assert.That(field.Name, Is.Not.Null.Or.Empty);
}

[Test]
public void DuplicateNames_ShouldThrow()
{
Expand Down
2 changes: 1 addition & 1 deletion PocketCsvReader/Configuration/FieldCollectionDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public override FieldDescriptor this[int index]

public override FieldDescriptor this[string name]
{
get => throw new NotSupportedException();
get => _list.FirstOrDefault(f => f.Name == name) ?? throw new KeyNotFoundException($"Field '{name}' not found.");
}

public override bool TryGetValue(string name, [NotNullWhen(true)] out FieldDescriptor? field)
Expand Down
15 changes: 14 additions & 1 deletion PocketCsvReader/Configuration/SchemaDescriptorBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public NamedSchemaDescriptorBuilder Named()
public interface ISchemaDescriptorBuilder
{
SchemaDescriptor? Build();
ISchemaDescriptorBuilder WithField(Type type, string name, Func<FieldDescriptorBuilder, FieldDescriptorBuilder> func);
}

public class IndexedSchemaDescriptorBuilder : ISchemaDescriptorBuilder
Expand All @@ -40,6 +41,12 @@ public IndexedSchemaDescriptorBuilder WithField(Type type, Func<FieldDescriptorB
return this;
}

public IndexedSchemaDescriptorBuilder WithField(Type type, string name, Func<FieldDescriptorBuilder, FieldDescriptorBuilder> func)
=> WithField(type, (builder) => func(builder.WithName(name)));

ISchemaDescriptorBuilder ISchemaDescriptorBuilder.WithField(Type type, string name, Func<FieldDescriptorBuilder, FieldDescriptorBuilder> func)
=> WithField(type, name, func);

public SchemaDescriptor? Build()
{
foreach (var field in _fields)
Expand All @@ -58,11 +65,17 @@ public NamedSchemaDescriptorBuilder WithField<T>(string name)
=> WithField<T>(name, x => x);

public NamedSchemaDescriptorBuilder WithField<T>(string name, Func<FieldDescriptorBuilder, FieldDescriptorBuilder> func)
=> WithField(typeof(T), name, func);

public NamedSchemaDescriptorBuilder WithField(Type type, string name, Func<FieldDescriptorBuilder, FieldDescriptorBuilder> func)
{
_fields.Add(func(new FieldDescriptorBuilder(typeof(T)).WithName(name)));
_fields.Add(func(new FieldDescriptorBuilder(type).WithName(name)));
return this;
}

ISchemaDescriptorBuilder ISchemaDescriptorBuilder.WithField(Type type, string name, Func<FieldDescriptorBuilder, FieldDescriptorBuilder> func)
=> WithField(type, name, func);

public SchemaDescriptor? Build()
{
foreach (var field in _fields)
Expand Down

0 comments on commit f15eb30

Please sign in to comment.