Skip to content

Commit

Permalink
changed to extension methods
Browse files Browse the repository at this point in the history
  • Loading branch information
j0nimost committed Oct 29, 2023
1 parent 7044fc7 commit 79ecaf0
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 57 deletions.
25 changes: 11 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ Example:
}
```

This offers you the granular control over each element. In addition you can also use `Parse<T>()` and `TryParse<T>(out var val)` on Col
This offers you the granular control over each element. The API offers a list of extension Methods for the `Col` struct which allows
you to Parse to any given type; `ParseInt()`, `ParseLong()`, `ParseFloat()`, `ParseDateTime()` etc.

Example:
```c#
Expand All @@ -42,7 +43,7 @@ Example:
{
foreach (var col in row.Cols)
{
sum += col.Parse<int>();
sum += col.ParseInt();
}
}
```
Expand Down Expand Up @@ -108,27 +109,23 @@ Behind the curtains Kafa is using `KafaPooledWriter` a pooled IBufferWriter to w
{
new CsvData{ Date = DateTime.Parse("10/10/2023 4:08:38 PM"), Open=12.45, Close=12.99, High=13.00, Low=12.1, Name="AMZN", Volume=1233435512},
new CsvData{ Date = DateTime.Parse("10/10/2023 4:08:38 PM"), Open=12.45, Close=12.99, High=13.00, Low=12.1, Name="AMZN", Volume=1233435512}
};

};
// get a ReadOnlySpan<bytes>
var spanOfbytes = await Kafa.Write<CsvData>(csvs);
string result = Encoding.UTF8.GetString(spanOfbytes);
string result = Encoding.UTF8.GetString(spanOfbytes);

// or
// write to an Stream
using var stream = await Kafa.WriteToStreamAsync<CsvData>(csvs);
// write to an Stream
using var stream = await Kafa.WriteToStreamAsync<CsvData>(csvs);

// or
// Write to a IBufferWriter<byte> for zero allocation writes
// Write to a IBufferWriter<byte> for zero allocation writes
var arrayWriter = new ArrayBufferWriter<byte>();
Kafa.Write<CsvData>(arrayWriter, csvs);
var str = Encoding.UTF8.GetString(arrayWriter.WrittenSpan);

var str = Encoding.UTF8.GetString(arrayWriter.WrittenSpan);
// or
// write directly to a file
// write directly to a file
await Kafa.WriteToFileAsync<CsvData>(csvs,@"C:\Users\ADMIN\Documents");

```
Expand Down
3 changes: 1 addition & 2 deletions src/Kafa/Kafa.Span.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ public static IEnumerable<T> Read<T>(string content, KafaOptions? options = null
{
ArgumentNullException.ThrowIfNullOrEmpty(content, nameof(content));
var rows = Read(content.AsSpan(), options);
var typeInfo = new KafaTypeInfo(typeof(T), options);
var reflection = new KafaReflection(typeInfo);
var reflection = SetupOptions<T>(options);

Check warning on line 22 in src/Kafa/Kafa.Span.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'options' in 'KafaReflection Kafa.SetupOptions<T>(KafaOptions options)'.
return reflection.SetProperties<T>(rows);
}

Expand Down
6 changes: 2 additions & 4 deletions src/Kafa/Kafa.Stream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ public static RowEnumerable Read(Stream ioStream, KafaOptions? options = null)
public static IEnumerable<T> Read<T>(Stream ioStream, KafaOptions? options = null)
{
var rowEnumerable = Read(ioStream, options);
var typeInfo = new KafaTypeInfo(typeof(T), options);
var reflection = new KafaReflection(typeInfo);
var reflection = SetupOptions<T>(options);

Check warning on line 34 in src/Kafa/Kafa.Stream.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'options' in 'KafaReflection Kafa.SetupOptions<T>(KafaOptions options)'.
return reflection.SetProperties<T>(rowEnumerable);
}

Expand Down Expand Up @@ -64,8 +63,7 @@ public static async ValueTask<RowEnumerable> ReadAsync(Stream ioStream, KafaOpti
public static async ValueTask<IEnumerable<T>> ReadAsync<T>(Stream ioStream, KafaOptions? options = null, CancellationToken cancellationToken = default)
{
var rows = await ReadAsync(ioStream, options, cancellationToken).ConfigureAwait(false);
var typeInfo = new KafaTypeInfo(typeof(T), options);
var reflection = new KafaReflection(typeInfo);
var reflection = SetupOptions<T>(options);
return reflection.SetProperties<T>(rows);
}

Expand Down
4 changes: 0 additions & 4 deletions src/Kafa/Kafa.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,4 @@
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>Generated</CompilerGeneratedFilesOutputPath>
</PropertyGroup>

<ItemGroup>
<Folder Include="Reader\" />
</ItemGroup>
</Project>
48 changes: 48 additions & 0 deletions src/Kafa/Reader/ColParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
namespace nyingi.Kafa.Reader;

public static class ColParser
{
public static bool? ParseBool(this KafaReader.Col col)
{
return bool.Parse(col.Value);
}
public static int? ParseInt(this KafaReader.Col col, IFormatProvider? formatProvider=null)
{
return int.Parse(col.Value, formatProvider);
}

public static long? ParseLong(this KafaReader.Col col, IFormatProvider? formatProvider=null)
{
return long.Parse(col.Value, formatProvider);
}

public static float? ParseFloat(this KafaReader.Col col, IFormatProvider? formatProvider=null)
{
return float.Parse(col.Value, formatProvider);
}

public static double? ParseDouble(this KafaReader.Col col, IFormatProvider? formatProvider=null)
{
return double.Parse(col.Value, formatProvider);
}

public static decimal? ParseDecimal(this KafaReader.Col col, IFormatProvider? formatProvider=null)
{
return decimal.Parse(col.Value, formatProvider);
}

public static DateTime? ParseDateTime(this KafaReader.Col col, IFormatProvider? formatProvider=null)
{
return DateTime.Parse(col.Value, formatProvider);
}

public static DateTimeOffset? ParseDateTimeOffSet(this KafaReader.Col col, IFormatProvider? formatProvider=null)
{
return DateTimeOffset.Parse(col.Value, formatProvider);
}

public static Guid? ParseGuid(this KafaReader.Col col, IFormatProvider? formatProvider=null)
{
return Guid.Parse(col.Value, formatProvider);
}
}
20 changes: 1 addition & 19 deletions src/Kafa/Reader/KafaReader.Col.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,7 @@ public Col(ColEnumerable colEnumerable, string columnName)
int index = _colEnumerable.ReadColByHeader(columnName);
Value = _colEnumerable.ReadColAsSpan(index);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public T Parse<T>() where T : ISpanParsable<T> => _colEnumerable.Parse<T>(Value);


[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryParse<T>(out T? result) where T : ISpanParsable<T> => _colEnumerable.TryParse(Value, out result);


public override string ToString()
{
return $"{Value}";
Expand Down Expand Up @@ -95,17 +88,6 @@ public ReadOnlySpan<char> ReadColAsSpan(int index)
return mem.Span;
}


public readonly T Parse<T>(ReadOnlySpan<char> scanSpan) where T : ISpanParsable<T>
{
return T.Parse(scanSpan, _reader.cultureInfo);
}

public readonly bool TryParse<T>(ReadOnlySpan<char> colValue, out T? result) where T : ISpanParsable<T>
{
return T.TryParse(colValue, _reader.cultureInfo, out result);
}

public Enumerator GetEnumerator() => new Enumerator(this);


Expand Down
35 changes: 21 additions & 14 deletions src/Kafa/Reflection/KafaReflection.Writer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Specialized;
using System.Globalization;
using System.Reflection;
using System.Reflection.PortableExecutable;
using nyingi.Kafa.Reader;
using static nyingi.Kafa.Reader.KafaReader;

namespace nyingi.Kafa.Reflection
Expand All @@ -10,10 +11,12 @@ internal partial class KafaReflection
private Dictionary<int, PropertyInfo> properties= default;

public readonly KafaTypeInfo TypeInfo;
private readonly CultureInfo? _cultureInfo;
public KafaReflection(KafaTypeInfo typeInfo)
{
// match propertyName with header
TypeInfo = typeInfo;
_cultureInfo = TypeInfo.KafaOptions.CultureInfo;
}

private void ReadHeader(OrderedDictionary headers = null)

Check warning on line 22 in src/Kafa/Reflection/KafaReflection.Writer.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.
Expand Down Expand Up @@ -88,47 +91,51 @@ public IEnumerable<T> SetProperties<T>(RowEnumerable rows)

return instance;
}



private object? TypeResolver(Type type, Col col)
{

if (type == typeof(string))
{
return col.Value.ToString();
return col.ToString();
}
else if (type == typeof(int))
{
return col.Parse<int>();
return col.ParseInt(_cultureInfo);
}
else if(type == typeof(long))
{
return col.Parse<long>();
return col.ParseLong(_cultureInfo);
}
else if(type == typeof(float))
{
return col.Parse<float>();
return col.ParseFloat(_cultureInfo);
}
else if(type == typeof(double))
{
return col.Parse<double>();
return col.ParseDouble(_cultureInfo);
}
else if(type == typeof(decimal))
{
return col.Parse<decimal>();
return col.ParseDecimal(_cultureInfo);
}
else if(type == typeof(bool))
{
return col.ParseBool();
}
else if(type == typeof(DateTime))
{
return col.Parse<DateTime>();
return col.ParseDateTime(_cultureInfo);
}
else if(type == typeof(DateTimeOffset))
{
return col.Parse<DateTimeOffset>();
return col.ParseDateTimeOffSet(_cultureInfo);
}
else if (type == typeof(Guid))
{
return col.ParseGuid(_cultureInfo);
}
else
{
return default;
return null;
}

}
Expand Down

0 comments on commit 79ecaf0

Please sign in to comment.