Skip to content

Commit

Permalink
feat: CsvDataReader can read and parse a Guid (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
Seddryck authored Jan 5, 2025
1 parent 86360d5 commit 08909f9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
30 changes: 30 additions & 0 deletions PocketCsvReader.Testing/CsvDataReaderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,36 @@ public void GetFieldValue_WithoutSchema_CorrectParsing(string record)
Assert.That(dataReader.GetFieldValue<decimal>(1), Is.EqualTo(108m));
}

[Test]
[TestCase("foo\r\nd3c7f3e0-4b3a-4a95-a6b9-81a519e4a8c1")]
public void GetGuid_ValidGuid_CorrectParsing(string record)
{
var guid = new Guid("d3c7f3e0-4b3a-4a95-a6b9-81a519e4a8c1");
var buffer = new MemoryStream(Encoding.UTF8.GetBytes(record));

var builder = new CsvReaderBuilder()
.WithDialect(
(dialect) => dialect
.WithDelimiter(';')
.WithHeader(true))
.WithSchema(
(schema) => schema
.Named()
.WithField<Guid>("foo")
);
using var dataReader = builder.Build().ToDataReader(buffer);
dataReader.Read();
Assert.That(dataReader.FieldCount, Is.EqualTo(1));
Assert.That(dataReader.GetName(0), Is.EqualTo("foo"));
Assert.That(dataReader.GetFieldType(0), Is.EqualTo(typeof(Guid)));
Assert.That(dataReader.GetValue(0), Is.TypeOf<Guid>());
Assert.That(dataReader.GetFieldValue<Guid>(0), Is.EqualTo(guid));
Assert.That(dataReader.GetValue(0), Is.EqualTo(guid));
Assert.That(dataReader[0], Is.EqualTo(guid));
Assert.That(dataReader["foo"], Is.EqualTo(guid));
Assert.That(dataReader.GetGuid(0), Is.EqualTo(guid));
}

[Test]
[TestCase("Ansi")]
[TestCase("Utf16-BE")]
Expand Down
2 changes: 1 addition & 1 deletion PocketCsvReader/CsvDataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ protected bool TryGetFieldDescriptor(int i, [NotNullWhen(true)] out FieldDescrip
}

public float GetFloat(int i) => float.Parse(GetValueOrThrow(i), CultureInfo.InvariantCulture);
public Guid GetGuid(int i) => throw new NotImplementedException();
public Guid GetGuid(int i) => Guid.Parse(GetValueOrThrow(i), CultureInfo.InvariantCulture);
public short GetInt16(int i) => short.Parse(GetValueOrThrow(i), CultureInfo.InvariantCulture);
public int GetInt32(int i) => int.Parse(GetValueOrThrow(i), CultureInfo.InvariantCulture);
public long GetInt64(int i) => long.Parse(GetValueOrThrow(i), CultureInfo.InvariantCulture);
Expand Down

0 comments on commit 08909f9

Please sign in to comment.