Skip to content

Commit

Permalink
added read column by Column Name
Browse files Browse the repository at this point in the history
  • Loading branch information
j0nimost committed Oct 17, 2023
1 parent acef17c commit 56feff4
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,15 @@ To also make it easier you can jump to a specific Row and Col

Example:
```c#
var stream = new MemoryStream(Encoding.UTF8.GetBytes(sampleCSV));
var stream = new MemoryStream(Encoding.UTF8.GetBytes(sampleCSV));

using var rows = Kafa.Read(stream);
using var rows = Kafa.Read(stream);

var result = rows[0].Cols[0].ToString();
var result = rows[0].Cols[0].ToString();
// or
var result2 = rows[new Index(0)].Cols[new Index(0)].ToString();
// or
var result2 = rows[new Index(0)].Cols[new Index(0)].ToString();
var result3 = row[0].Cols["date"].ToString(); // read Column by Name
```
#### `IEnumerable<T>`
Understanding that many users want a simple easy to use experience the library allows
Expand Down
19 changes: 19 additions & 0 deletions src/Kafa/Reader/KafaReader.Col.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ public Col(ColEnumerable colEnumerable, int index)

}

public Col(ColEnumerable colEnumerable, string columnName)
{
_colEnumerable = colEnumerable;
int index = _colEnumerable.ReadColByHeader(columnName);
Value = _colEnumerable.ReadColSpan(index);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public T Parse<T>() where T : ISpanParsable<T> => _colEnumerable.Parse<T>(Value);
Expand Down Expand Up @@ -57,6 +63,19 @@ public ColEnumerable(KafaReader reader, int startColIndex, int length)

public Col this[Index index] => new(this, index.GetOffset(this.Length));

public Col this[string columnName] => new(this, columnName);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int ReadColByHeader(string columnName)
{
if(_reader.Headers != null && _reader.Headers.Contains(columnName))
{
return (int)_reader.Headers[columnName]!;
}

throw new KafaException($"{columnName} Not Found");
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ReadOnlySpan<char> ReadColSpan(int index)
{
Expand Down
26 changes: 26 additions & 0 deletions src/KafaTests/KafaReadTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,21 @@ public void ReadColByIndexStruct(string sampleCSV)
Assert.Equal("AAL", rows[new Index(2)].Cols[new Index(6)].ToString());
}


[Theory]
[MemberData(nameof(GetObjects))]
public void ReadColByColumnName(string sampleCSV)
{
var stream = new MemoryStream(Encoding.UTF8.GetBytes(sampleCSV));

using var rows = Kafa.Read(stream);
Assert.NotEmpty(rows);
Assert.Equal("2013-02-08", rows[1].Cols["date"].ToString());
Assert.Equal("15.12", rows[1].Cols["high"].ToString());
Assert.Equal("AAL", rows[3].Cols["Name"].ToString());
Assert.Equal("AAL", rows[2].Cols["Name"].ToString());
}

[Theory]
[MemberData(nameof(GetObjects))]
public void ReadRowOutOfRange(string sampleCSV)
Expand All @@ -192,6 +207,17 @@ public void ReadColOutOfRange(string sampleCSV)
Assert.Throws<KafaException>(() => rows[0].Cols[-1].ToString());
}


[Theory]
[MemberData(nameof(GetObjects))]
public void ReadColNameNotFound(string sampleCSV)
{
var stream = new MemoryStream(Encoding.UTF8.GetBytes(sampleCSV));
using var rows = Kafa.Read(stream);
Assert.NotEmpty(rows);
Assert.Throws<KafaException>(() => rows[0].Cols["city"].ToString());
}

[Fact]
public void ReadUnequalRowSize()
{
Expand Down

0 comments on commit 56feff4

Please sign in to comment.