Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: method DataTypeName returns the field type as defined in the source #96

Merged
merged 2 commits into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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