From 79ecaf0c93a8e932250904dbb138417c1d997ee4 Mon Sep 17 00:00:00 2001 From: j0nimost Date: Sun, 29 Oct 2023 17:55:07 +0300 Subject: [PATCH] changed to extension methods --- README.md | 25 +++++----- src/Kafa/Kafa.Span.cs | 3 +- src/Kafa/Kafa.Stream.cs | 6 +-- src/Kafa/Kafa.csproj | 4 -- src/Kafa/Reader/ColParser.cs | 48 ++++++++++++++++++++ src/Kafa/Reader/KafaReader.Col.cs | 20 +------- src/Kafa/Reflection/KafaReflection.Writer.cs | 35 ++++++++------ 7 files changed, 84 insertions(+), 57 deletions(-) create mode 100644 src/Kafa/Reader/ColParser.cs diff --git a/README.md b/README.md index b7df462..7b9fce2 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,8 @@ Example: } ``` -This offers you the granular control over each element. In addition you can also use `Parse()` and `TryParse(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# @@ -42,7 +43,7 @@ Example: { foreach (var col in row.Cols) { - sum += col.Parse(); + sum += col.ParseInt(); } } ``` @@ -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 var spanOfbytes = await Kafa.Write(csvs); - string result = Encoding.UTF8.GetString(spanOfbytes); + string result = Encoding.UTF8.GetString(spanOfbytes); // or - // write to an Stream - - using var stream = await Kafa.WriteToStreamAsync(csvs); + // write to an Stream + using var stream = await Kafa.WriteToStreamAsync(csvs); // or - // Write to a IBufferWriter for zero allocation writes - + // Write to a IBufferWriter for zero allocation writes var arrayWriter = new ArrayBufferWriter(); Kafa.Write(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(csvs,@"C:\Users\ADMIN\Documents"); ``` diff --git a/src/Kafa/Kafa.Span.cs b/src/Kafa/Kafa.Span.cs index d4b6269..3acf299 100644 --- a/src/Kafa/Kafa.Span.cs +++ b/src/Kafa/Kafa.Span.cs @@ -19,8 +19,7 @@ public static IEnumerable Read(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(options); return reflection.SetProperties(rows); } diff --git a/src/Kafa/Kafa.Stream.cs b/src/Kafa/Kafa.Stream.cs index ac570e7..9f12f9a 100644 --- a/src/Kafa/Kafa.Stream.cs +++ b/src/Kafa/Kafa.Stream.cs @@ -31,8 +31,7 @@ public static RowEnumerable Read(Stream ioStream, KafaOptions? options = null) public static IEnumerable Read(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(options); return reflection.SetProperties(rowEnumerable); } @@ -64,8 +63,7 @@ public static async ValueTask ReadAsync(Stream ioStream, KafaOpti public static async ValueTask> ReadAsync(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(options); return reflection.SetProperties(rows); } diff --git a/src/Kafa/Kafa.csproj b/src/Kafa/Kafa.csproj index 7f21ffe..5cba791 100644 --- a/src/Kafa/Kafa.csproj +++ b/src/Kafa/Kafa.csproj @@ -7,8 +7,4 @@ true Generated - - - - diff --git a/src/Kafa/Reader/ColParser.cs b/src/Kafa/Reader/ColParser.cs new file mode 100644 index 0000000..6c00efd --- /dev/null +++ b/src/Kafa/Reader/ColParser.cs @@ -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); + } +} \ No newline at end of file diff --git a/src/Kafa/Reader/KafaReader.Col.cs b/src/Kafa/Reader/KafaReader.Col.cs index aa35138..d3c3987 100644 --- a/src/Kafa/Reader/KafaReader.Col.cs +++ b/src/Kafa/Reader/KafaReader.Col.cs @@ -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() where T : ISpanParsable => _colEnumerable.Parse(Value); - - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public bool TryParse(out T? result) where T : ISpanParsable => _colEnumerable.TryParse(Value, out result); - + public override string ToString() { return $"{Value}"; @@ -95,17 +88,6 @@ public ReadOnlySpan ReadColAsSpan(int index) return mem.Span; } - - public readonly T Parse(ReadOnlySpan scanSpan) where T : ISpanParsable - { - return T.Parse(scanSpan, _reader.cultureInfo); - } - - public readonly bool TryParse(ReadOnlySpan colValue, out T? result) where T : ISpanParsable - { - return T.TryParse(colValue, _reader.cultureInfo, out result); - } - public Enumerator GetEnumerator() => new Enumerator(this); diff --git a/src/Kafa/Reflection/KafaReflection.Writer.cs b/src/Kafa/Reflection/KafaReflection.Writer.cs index 1208f7a..14b87e8 100644 --- a/src/Kafa/Reflection/KafaReflection.Writer.cs +++ b/src/Kafa/Reflection/KafaReflection.Writer.cs @@ -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 @@ -10,10 +11,12 @@ internal partial class KafaReflection private Dictionary 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) @@ -88,47 +91,51 @@ public IEnumerable SetProperties(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(); + return col.ParseInt(_cultureInfo); } else if(type == typeof(long)) { - return col.Parse(); + return col.ParseLong(_cultureInfo); } else if(type == typeof(float)) { - return col.Parse(); + return col.ParseFloat(_cultureInfo); } else if(type == typeof(double)) { - return col.Parse(); + return col.ParseDouble(_cultureInfo); } else if(type == typeof(decimal)) { - return col.Parse(); + return col.ParseDecimal(_cultureInfo); + } + else if(type == typeof(bool)) + { + return col.ParseBool(); } else if(type == typeof(DateTime)) { - return col.Parse(); + return col.ParseDateTime(_cultureInfo); } else if(type == typeof(DateTimeOffset)) { - return col.Parse(); + return col.ParseDateTimeOffSet(_cultureInfo); + } + else if (type == typeof(Guid)) + { + return col.ParseGuid(_cultureInfo); } else { - return default; + return null; } }