Skip to content

Commit

Permalink
clean up : 3
Browse files Browse the repository at this point in the history
  • Loading branch information
j0nimost committed Oct 29, 2023
1 parent 79ecaf0 commit deddfd3
Show file tree
Hide file tree
Showing 14 changed files with 37 additions and 89 deletions.
5 changes: 1 addition & 4 deletions src/Kafa/Kafa.Span.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using nyingi.Kafa.Reader;
using nyingi.Kafa.Reflection;
using System;
using System.Xml;
using static nyingi.Kafa.Reader.KafaReader;

namespace nyingi.Kafa
Expand All @@ -17,7 +14,7 @@ public static RowEnumerable Read(ReadOnlySpan<char> span, KafaOptions? options =

public static IEnumerable<T> Read<T>(string content, KafaOptions? options = null)
{
ArgumentNullException.ThrowIfNullOrEmpty(content, nameof(content));
ArgumentException.ThrowIfNullOrEmpty(content, nameof(content));
var rows = Read(content.AsSpan(), options);
var reflection = SetupOptions<T>(options);
return reflection.SetProperties<T>(rows);
Expand Down
23 changes: 9 additions & 14 deletions src/Kafa/Kafa.Stream.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
using System.Buffers;
using nyingi.Kafa.Reader;
using nyingi.Kafa.Reflection;
using System.IO;
using System.Runtime.Serialization;
using System.Text;
using System.Threading;
using System.Xml;
using nyingi.Kafa.Writer;
using static nyingi.Kafa.Reader.KafaReader;

Expand Down Expand Up @@ -77,48 +72,48 @@ private static async ValueTask<RowEnumerable> ReadProcessorAsync(KafaReadState k
return reader.GetRows();
}

public static void Write<T>(IBufferWriter<byte> bufferWriter, List<T> entities, KafaOptions options = null)
public static void Write<T>(IBufferWriter<byte> bufferWriter, List<T> entities, KafaOptions? options = null)
{
ArgumentNullException.ThrowIfNull(entities, nameof(entities));
var reflection = SetupOptions<T>(options);
using var writer = new KafaWriter(bufferWriter, reflection.TypeInfo.KafaOptions);
reflection.GetProperties<T>(writer, entities);
reflection.GetProperties(writer, entities);
}


public static ReadOnlySpan<byte> Write<T>(List<T> entities, KafaOptions options = null)
public static ReadOnlySpan<byte> Write<T>(List<T> entities, KafaOptions? options = null)
{
ArgumentNullException.ThrowIfNull(entities, nameof(entities));
var reflection = SetupOptions<T>(options);
using var pooledBufferWriter = new KafaPooledWriter();
using var bufferWriter = new KafaWriter(pooledBufferWriter, reflection.TypeInfo.KafaOptions);
reflection.GetProperties<T>(bufferWriter, entities);
reflection.GetProperties(bufferWriter, entities);
return pooledBufferWriter.WrittenAsSpan;
}

public static async ValueTask<Stream> WriteToStreamAsync<T>(List<T> entities, KafaOptions options = null, CancellationToken token = default)
public static async ValueTask<Stream> WriteToStreamAsync<T>(List<T> entities, KafaOptions? options = null, CancellationToken token = default)
{
ArgumentNullException.ThrowIfNull(entities, nameof(entities));
var reflection = SetupOptions<T>(options);
var memoryStream = new MemoryStream();
using var bufferWriter = new KafaWriter(memoryStream, reflection.TypeInfo.KafaOptions);
reflection.GetProperties<T>(bufferWriter, entities);
reflection.GetProperties(bufferWriter, entities);
await bufferWriter.FlushAsync(token).ConfigureAwait(false);
return memoryStream;
}

public static async ValueTask WriteToFileAsync<T>(List<T> entities, string path, KafaOptions options = null, CancellationToken token=default)
public static async ValueTask WriteToFileAsync<T>(List<T> entities, string path, KafaOptions? options = null, CancellationToken token=default)
{
ArgumentNullException.ThrowIfNull(entities, nameof(entities));
ArgumentNullException.ThrowIfNull(path, nameof(path));
var reflection = SetupOptions<T>(options);
using var fs = new FileStream(path, FileMode.Create);
using var bufferWriter = new KafaWriter(fs, reflection.TypeInfo.KafaOptions);
reflection.GetProperties<T>(bufferWriter, entities);
reflection.GetProperties(bufferWriter, entities);
await bufferWriter.FlushAsync(token).ConfigureAwait(false);
}

private static KafaReflection SetupOptions<T>(KafaOptions options)
private static KafaReflection SetupOptions<T>(KafaOptions? options)
{
options = KafaOptions.ResolveKafaOptions(options);
var typeInfo = new KafaTypeInfo(typeof(T), options);
Expand Down
7 changes: 1 addition & 6 deletions src/Kafa/KafaColumnAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@

using System.Diagnostics.CodeAnalysis;

#nullable disable warnings

namespace nyingi.Kafa
namespace nyingi.Kafa
{
[System.AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple =false)]
public class KafaColumnAttribute : Attribute
Expand Down
8 changes: 2 additions & 6 deletions src/Kafa/KafaOptions.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Globalization;
using System.Text;
using System.Threading.Tasks;

namespace nyingi.Kafa
{
Expand All @@ -12,7 +8,7 @@ public class SeparatorFileType
public const char CSV = ',';
public const char TSV = ';';
}
public sealed partial class KafaOptions
public sealed class KafaOptions
{
public CultureInfo? CultureInfo { get; set; }
public Encoding? Encoding { get; set; }
Expand Down
5 changes: 1 addition & 4 deletions src/Kafa/KafaTypeInfo.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
using nyingi.Kafa;

namespace nyingi.Kafa
namespace nyingi.Kafa
{
internal class KafaTypeInfo
{
public readonly Type Type;
public readonly KafaOptions KafaOptions;
public bool HasAttributes { get; private set;}
public KafaTypeInfo(Type type, KafaOptions kafaOptions)
{
Type = type;
Expand Down
11 changes: 5 additions & 6 deletions src/Kafa/Reader/KafaReadState.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Buffers;
using System.Buffers;
using System.Collections.Specialized;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
Expand All @@ -18,7 +17,7 @@ public sealed class KafaReadState : IDisposable

public int OffSet { get; private set; }

public bool HasCRLF { get; private set; } = false;
public bool HasCRLF { get; private set; }

public int LastBufferIndex { get; private set; }

Expand All @@ -29,7 +28,7 @@ public sealed class KafaReadState : IDisposable
public readonly int BufferLength;


public OrderedDictionary Headers { get; private set; } = default;
public OrderedDictionary? Headers { get; private set; } = default;
public KafaReadState(int bufferLength, KafaOptions kafaOptions)
{
BufferLength = bufferLength;
Expand Down Expand Up @@ -119,7 +118,7 @@ private void ReadColMarkers()
i = j + 1;
continue;
}
else if (Buffer[i] == (int)Options.Separator || Buffer[i] == '\n' || Buffer[i + 1] == '\0')
else if (Buffer[i] == Options.Separator || Buffer[i] == '\n' || Buffer[i + 1] == '\0')
{
ColMarker[colIndexer] = i;
colIndexer++;
Expand Down Expand Up @@ -175,7 +174,7 @@ private void ReadColCount()
i = j + 1;
continue;
}
else if (Buffer[i] == (int)Options.Separator)
else if (Buffer[i] == Options.Separator)
{
ColCount++;
}
Expand Down
10 changes: 2 additions & 8 deletions src/Kafa/Reader/KafaReader.Col.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,8 @@ public readonly ref struct Col
{
public readonly ReadOnlySpan<char> Value;
private readonly ColEnumerable _colEnumerable;

public int Length
{
get
{
return Value.Length;
}
}

public int Length => Value.Length;

public Col(ColEnumerable colEnumerable, int index)
{
Expand Down
4 changes: 1 addition & 3 deletions src/Kafa/Reader/KafaReader.Row.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@

using System.Collections;
using System.Collections.Specialized;
using System.Collections.Specialized;

namespace nyingi.Kafa.Reader
{
Expand Down
3 changes: 0 additions & 3 deletions src/Kafa/Reader/KafaReader.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Specialized;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

Expand All @@ -8,11 +7,9 @@ namespace nyingi.Kafa.Reader
public partial class KafaReader : IDisposable
{
private readonly KafaReadState _kafaReadState;
private readonly CultureInfo? cultureInfo;
public KafaReader(KafaReadState kafaReadState)
{
_kafaReadState = kafaReadState;
cultureInfo = kafaReadState.Options.CultureInfo;
}

public int ColumnCount => _kafaReadState.ColCount;
Expand Down
15 changes: 0 additions & 15 deletions src/Kafa/Reflection/KafaListOfT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,5 @@ public void CreateCollection(KafaTypeInfo kafaTypeInfo, int length)
{
kafaTypeInfo.ReturnValue = Array.CreateInstance(kafaTypeInfo.Type, length);
}

public void AddElement(T t, KafaTypeInfo kafaTypeInfo)
{
((ICollection<T>)kafaTypeInfo.ReturnValue!).Add(t);
}

public IEnumerable<T> Write<T>(Type t, KafaTypeInfo kafaTypeInfo)
{

// Create a list of the required type, passing the values to the constructor
Type genericListType = typeof(List<>);
Type concreteListType = genericListType.MakeGenericType(t);

return (List<T>)Activator.CreateInstance(concreteListType, new object[] { kafaTypeInfo.ReturnValue });
}
}
}
6 changes: 1 addition & 5 deletions src/Kafa/Reflection/KafaReflection.Reader.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System.Diagnostics.Metrics;
using System.IO;
using System.Reflection;
using System.Reflection.Emit;
using System.Text;
using System.Reflection;
using nyingi.Kafa.Writer;

namespace nyingi.Kafa.Reflection
Expand Down
18 changes: 9 additions & 9 deletions src/Kafa/Reflection/KafaReflection.Writer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace nyingi.Kafa.Reflection
{
internal partial class KafaReflection
{
private Dictionary<int, PropertyInfo> properties= default;
private Dictionary<int, PropertyInfo>? _properties= default;

public readonly KafaTypeInfo TypeInfo;
private readonly CultureInfo? _cultureInfo;
Expand All @@ -19,9 +19,9 @@ public KafaReflection(KafaTypeInfo typeInfo)
_cultureInfo = TypeInfo.KafaOptions.CultureInfo;
}

private void ReadHeader(OrderedDictionary headers = null)
private void ReadHeader(OrderedDictionary? headers = null)
{
properties = new Dictionary<int, PropertyInfo>(TypeInfo.Type.GetProperties().Length); // ahead
_properties = new Dictionary<int, PropertyInfo>(TypeInfo.Type.GetProperties().Length); // ahead

int count = 0;
foreach (var property in TypeInfo.Type.GetProperties())
Expand All @@ -34,22 +34,22 @@ private void ReadHeader(OrderedDictionary headers = null)
{
if (!string.IsNullOrEmpty(kafa.FieldName))
{
properties.Add((int)headers[kafa.FieldName], property);
_properties.Add((int)headers[kafa.FieldName], property);

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

View workflow job for this annotation

GitHub Actions / build

Unboxing a possibly null value.

}
else
{
properties.Add((int)headers[kafa.FieldIndex], property);
_properties.Add((int)headers[kafa.FieldIndex], property);

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

View workflow job for this annotation

GitHub Actions / build

Unboxing a possibly null value.
}
}
else if (headers.Contains(property.Name))
{
properties.Add((int)headers[property.Name], property);
_properties.Add((int)headers[property.Name], property);

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

View workflow job for this annotation

GitHub Actions / build

Unboxing a possibly null value.
}
}
else
{
properties.Add(count, property);
_properties.Add(count, property);
count++;
}
}
Expand All @@ -61,7 +61,7 @@ public IEnumerable<T> SetProperties<T>(RowEnumerable rows)
// process header first
ReadHeader(rows.Headers);

if(properties.Count == 0)
if(_properties.Count == 0)

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

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
{
throw new Exception("{0} class is empty");
}
Expand All @@ -79,7 +79,7 @@ public IEnumerable<T> SetProperties<T>(RowEnumerable rows)
foreach (var col in row.Cols)
{
// populate the properties
var propertyInfo = properties[colIndex];
var propertyInfo = _properties[colIndex];
propertyInfo.SetValue(instanceOft, TypeResolver(propertyInfo.PropertyType, col));
colIndex++;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Kafa/Writer/KafaPooledWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ private void Resize(int sizeHint)
{
newCapacity += growBy;
}
var newBuffer = ArrayPool<byte>.Shared.Rent(newCapacity);
Array.Copy(_buffer, newBuffer, Capacity);
_buffer = null!;
_buffer = newBuffer;

var newBuffer = _buffer;
_buffer = ArrayPool<byte>.Shared.Rent(newCapacity);
Array.Copy(newBuffer, _buffer, Capacity);
ArrayPool<byte>.Shared.Return(newBuffer);
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/Kafa/Writer/KafaWriter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Buffers;
using System.Runtime.InteropServices;
using System.Text;

namespace nyingi.Kafa.Writer
Expand All @@ -26,7 +25,7 @@ public KafaWriter(in Stream stream, KafaOptions options)
{
_stream = stream;
_options = options;
_kafaPooledWriter = new KafaPooledWriter(0); // use the default 65k
_kafaPooledWriter = new KafaPooledWriter(); // use the default 65k
}

public void WriteSeparator()
Expand Down

0 comments on commit deddfd3

Please sign in to comment.