Skip to content

Commit

Permalink
Added DelimitedStreamBuilderTests, switch lots of code to use nullabl…
Browse files Browse the repository at this point in the history
…es and cleaned up.
  • Loading branch information
putridparrot committed Jun 3, 2023
1 parent b4bdc76 commit fde5e63
Show file tree
Hide file tree
Showing 32 changed files with 418 additions and 314 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ namespace PutridParrot.Delimited.Data.Attributes
public abstract class DelimitedFieldAttribute : Attribute
{
public int ColumnIndex { get; set; }
public string Heading { get; set; }
public string? Heading { get; set; }

protected DelimitedFieldAttribute()
: this(-1)
{
}

protected DelimitedFieldAttribute(string heading)
protected DelimitedFieldAttribute(string? heading)
: this(-1)
{
Heading = heading;
Expand All @@ -25,6 +25,6 @@ protected DelimitedFieldAttribute(string heading)
protected DelimitedFieldAttribute(int columnIndex)
{
ColumnIndex = columnIndex;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ public DelimitedFieldReadAttribute()
{
}

public DelimitedFieldReadAttribute(string heading) :
public DelimitedFieldReadAttribute(string? heading) :
this(heading, false)
{
}

public DelimitedFieldReadAttribute(string heading, bool required) :
public DelimitedFieldReadAttribute(string? heading, bool required) :
base(heading)
{
Required = required;
Expand All @@ -37,6 +37,6 @@ public DelimitedFieldReadAttribute(int columnIndex, bool required) :
}

public bool Required { get; set; }
public string[] AlternateNames { get; set; }
public string[]? AlternateNames { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public DelimitedFieldWriteAttribute()
{
}

public DelimitedFieldWriteAttribute(string heading) :
public DelimitedFieldWriteAttribute(string? heading) :
base(heading)
{
}
Expand Down
18 changes: 9 additions & 9 deletions PutridParrot.Delimited.Data/DelimitedDataEnumerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static IEnumerable Deserialize(IDelimitedSeparatedReader delimiterSeparat
/// <param name="data">A string representing delimited data</param>
/// <param name="options">Options for how the deserializer should handle data</param>
/// <returns>An IEnumerable representing the dynamic row data</returns>
public static IEnumerable Deserialize(IDelimitedSeparatedReader delimiterSeparatedReader, string data, DelimitedDeserializeOptions options)
public static IEnumerable Deserialize(IDelimitedSeparatedReader delimiterSeparatedReader, string data, DelimitedDeserializeOptions? options)
{
using var memoryStream = new MemoryStream();

Expand Down Expand Up @@ -76,11 +76,11 @@ private static string[] CreateColumnHeadings(int length)
/// <param name="stream">A stream representing the delimited data</param>
/// <param name="options">Options for how the deserializer should handle data</param>
/// <returns>An IEnumerable representing the dynamic row data</returns>
public static IEnumerable Deserialize(IDelimitedSeparatedReader delimiterSeparatedReader, Stream stream, DelimitedDeserializeOptions options)
public static IEnumerable Deserialize(IDelimitedSeparatedReader delimiterSeparatedReader, Stream stream, DelimitedDeserializeOptions? options)
{
using var reader = new DelimitedStreamReader(delimiterSeparatedReader, stream);
// remove any "ignore rows"
if (options != null && options.IgnoreFirstNRows > 0)
if (options is { IgnoreFirstNRows: > 0 })
{
var nRow = 0;
while (nRow < options.IgnoreFirstNRows && reader.ReadLine() != null)
Expand All @@ -91,26 +91,26 @@ public static IEnumerable Deserialize(IDelimitedSeparatedReader delimiterSeparat

if (options != null && !options.UseHeadings)
{
IEnumerable<string> fields;
IEnumerable<string>? fields;
while ((fields = reader.ReadLine()) != null)
{
var f = fields.ToArray();
string?[] f = fields.ToArray();
yield return new DelimitedRow(CreateColumnHeadings(f.Length), f);
}
}
else
{
IEnumerable<string> headings = reader.ReadLine();
var headings = reader.ReadLine();
if (headings != null)
{
var columnHeadings = headings.ToArray();

IEnumerable<string> fields;
IEnumerable<string>? fields;
while ((fields = reader.ReadLine()) != null)
{
if (options != null && options.IgnoreEmptyRows)
if (options is { IgnoreEmptyRows: true })
{
if (fields.All(String.IsNullOrEmpty))
if (fields.All(string.IsNullOrEmpty))
continue;
}

Expand Down
2 changes: 1 addition & 1 deletion PutridParrot.Delimited.Data/DelimitedDeserializeOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public DelimitedDeserializeOptions()
/// <summary>
/// Gets/Sets the mappings for the object to deserialize to
/// </summary>
public IList<FieldReadProperty> Mappings { get; set; }
public IList<FieldReadProperty>? Mappings { get; set; }
/// <summary>
/// Gets/Sets whether we should ignore empty rows
/// </summary>
Expand Down
10 changes: 5 additions & 5 deletions PutridParrot.Delimited.Data/DelimitedRow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace PutridParrot.Delimited.Data
public class DelimitedRow : DynamicObject
{
private readonly string[] _headings;
private readonly string[] _fields;
private readonly string?[] _fields;

[DebuggerStepThrough]
static DelimitedRow()
Expand All @@ -42,13 +42,13 @@ static DelimitedRow()
}

[DebuggerStepThrough]
internal DelimitedRow(string[] headings, string[] fields)
internal DelimitedRow(string[] headings, string?[] fields)
{
_headings = headings;
_fields = fields;
}

private bool GetField(string header, out string field)
private bool GetField(string header, out string? field)
{
field = null;
for (var i = 0; i < _headings.Length; i++)
Expand All @@ -67,7 +67,7 @@ private bool GetField(string header, out string field)

public static CultureInfo CultureInfo { get; set; }

public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result)
public override bool TryGetIndex(GetIndexBinder binder, object[]? indexes, out object? result)
{
result = null;

Expand All @@ -94,7 +94,7 @@ public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out ob
return false;
}

public override bool TryGetMember(GetMemberBinder binder, out object result)
public override bool TryGetMember(GetMemberBinder binder, out object? result)
{
if (binder == null)
{
Expand Down
4 changes: 2 additions & 2 deletions PutridParrot.Delimited.Data/DelimitedSeparatedReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public DelimitedSeparatedReader(DelimitedOptions options)

public DelimitedOptions Options { get; set; }

public IList<string> Read(StreamReader reader)
public IList<string>? Read(StreamReader reader)
{
if (reader == null)
{
Expand All @@ -37,7 +37,7 @@ public IList<string> Read(StreamReader reader)
return line == null ? null : Split(line, Options.Delimiter, Options.Qualifier);
}

public async Task<IList<string>> ReadAsync(StreamReader reader)
public async Task<IList<string>?> ReadAsync(StreamReader reader)
{
if (reader == null)
{
Expand Down
2 changes: 1 addition & 1 deletion PutridParrot.Delimited.Data/DelimitedSerializeOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class DelimitedSerializeOptions
/// <summary>
/// Gets/Sets the mappings for how data should be serialized
/// </summary>
public IList<FieldWriteProperty> Mappings { get; set; }
public IList<FieldWriteProperty>? Mappings { get; set; }
/// <summary>
/// Gets/Sets whether we should include headers in the serialized data
/// </summary>
Expand Down
Loading

0 comments on commit fde5e63

Please sign in to comment.