Skip to content

Commit

Permalink
feat: method DataTypeName returns the field type as defined in the so…
Browse files Browse the repository at this point in the history
…urce (#96)

* refactor: split CsvDataReader into sub-classes
* feat: method DataTypeName returns the field type as defined in the source
  • Loading branch information
Seddryck authored Jan 12, 2025
1 parent 57a5583 commit ffb63ef
Show file tree
Hide file tree
Showing 9 changed files with 440 additions and 343 deletions.
25 changes: 25 additions & 0 deletions PocketCsvReader.Testing/CsvRawRecordTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using PocketCsvReader.Configuration;

namespace PocketCsvReader.Testing;
public class CsvRawRecordTest
{
[Test]
public void GetDataTypeName_WithIndexedSchema_CorrectNames()
{
var schema = new SchemaDescriptorBuilder()
.Indexed()
.WithNumericField<decimal>((f) => f.WithDataSourceTypeName("number"))
.WithField<string>((f) => f.WithDataSourceTypeName("string"))
.Build();
var profile = new CsvProfile(new DialectDescriptor(), schema);
var rawRecord = new CsvRawRecord(profile);
Assert.That(rawRecord.GetDataTypeName(0), Is.EqualTo("number"));
Assert.That(rawRecord.GetDataTypeName(1), Is.EqualTo("string"));
}
}
1 change: 1 addition & 0 deletions PocketCsvReader/Configuration/FieldDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ Type RuntimeType
, string? Name = null
, string? Format = null
, ImmutableSequenceCollection? Sequences = null
, string DataSourceTypeName = ""
)
{ }
9 changes: 8 additions & 1 deletion PocketCsvReader/Configuration/FieldDescriptorBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class FieldDescriptorBuilder
protected string? _format;
protected string? _name;
protected SequenceCollection? _sequences;
protected string? _dataSourceTypeName;

protected internal FieldDescriptorBuilder(Type runtimeType)
{
Expand All @@ -38,8 +39,14 @@ public FieldDescriptorBuilder WithSequence(string pattern, string? value)
return this;
}

public FieldDescriptorBuilder WithDataSourceTypeName(string typeName)
{
_dataSourceTypeName = typeName;
return this;
}

public virtual FieldDescriptor Build()
{
return new FieldDescriptor(_runtimeType, _name, _format, _sequences?.ToImmutable());
return new FieldDescriptor(_runtimeType, _name, _format, _sequences?.ToImmutable(), _dataSourceTypeName ?? string.Empty);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public NumericFieldDescriptorBuilder WithoutGroupChar()
public new NumericFieldDescriptorBuilder WithSequence(string pattern, string? value)
=> (NumericFieldDescriptorBuilder)base.WithSequence(pattern, value);

public new NumericFieldDescriptorBuilder WithDataSourceTypeName(string typeName)
=> (NumericFieldDescriptorBuilder)base.WithDataSourceTypeName(typeName);

public override FieldDescriptor Build()
=> new NumericFieldDescriptor(_runtimeType, _name, _format, _sequences?.ToImmutable(), _decimalChar, _groupChar);
=> new NumericFieldDescriptor(_runtimeType, _name, _format, _sequences?.ToImmutable(), _dataSourceTypeName ?? string.Empty, _decimalChar, _groupChar);
}
Loading

0 comments on commit ffb63ef

Please sign in to comment.