Skip to content

Commit 472616b

Browse files
committed
Add explicit support for .net standard 2.1. Add methods working with IAsyncEnumerable.
1 parent f8c2fb9 commit 472616b

File tree

6 files changed

+219
-83
lines changed

6 files changed

+219
-83
lines changed

FlatFiles.Benchmark/AsyncVsSyncTest.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,8 @@ public async Task<string> AsyncTest()
111111
using (var response = await http.GetResponseAsync().ConfigureAwait(false))
112112
using (var textReader = new StreamReader(response.GetResponseStream()))
113113
{
114-
var reader = mapper.GetReader(textReader, new SeparatedValueOptions() { IsFirstRecordSchema = true });
115-
var writer = mapper.GetWriter(textWriter, new SeparatedValueOptions() { IsFirstRecordSchema = true });
116-
while (await reader.ReadAsync().ConfigureAwait(false))
117-
{
118-
await writer.WriteAsync(reader.Current).ConfigureAwait(false);
119-
}
114+
var entities = mapper.ReadAsync(textReader, new SeparatedValueOptions() { IsFirstRecordSchema = true });
115+
await mapper.WriteAsync(textWriter, entities, new SeparatedValueOptions() { IsFirstRecordSchema = true }).ConfigureAwait(false);
120116
}
121117
return textWriter.ToString();
122118

FlatFiles/FlatFiles.csproj

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>netcoreapp3.0;netstandard2.0;netstandard1.6;net451</TargetFrameworks>
4+
<TargetFrameworks>netcoreapp3.0;netstandard2.1;netstandard2.0;netstandard1.6;net451</TargetFrameworks>
55
<NeutralLanguage>en-US</NeutralLanguage>
66
<Description>Reads and writes CSV, fixed-length and other flat file formats with a focus on schema definition, configuration and speed. Supports mapping directly between files and classes.</Description>
77
<Copyright>Copyright @ 2021</Copyright>
@@ -10,19 +10,17 @@
1010
<RepositoryUrl>https://github.com/jehugaleahsa/FlatFiles.git</RepositoryUrl>
1111
<RepositoryType>git</RepositoryType>
1212
<PackageTags>csv;comma;tab;separated;value;delimited;flat;file;fixed;width;fixed-width;length;fixed-length;parser;parsing;parse</PackageTags>
13-
<PackageReleaseNotes>Recent changes to write out headers when writing multiple records only applied to the extension method WriteAll. However, the mapper class's Write methods have a similar semantic and behaved the same as before. This commit actually changes the mapper methods to call WriteAll under the hood, so now the same behavior will be exhibited.
14-
15-
I discovered a bug I introduced with the previous version where I wrote the schema out no matter what. I only want to do this if the writer is configured to write out the schema, which is false by default.</PackageReleaseNotes>
13+
<PackageReleaseNotes>Add explicit support for .net standard 2.1. Add explicit support for methods working with IAsyncEnumerable.</PackageReleaseNotes>
1614
<SignAssembly>true</SignAssembly>
1715
<AssemblyOriginatorKeyFile>FlatFiles.snk</AssemblyOriginatorKeyFile>
18-
<Version>4.15.0</Version>
16+
<Version>4.16.0</Version>
1917
</PropertyGroup>
2018

2119
<PropertyGroup>
2220
<LangVersion>8.0</LangVersion>
2321
<PackageIconUrl></PackageIconUrl>
24-
<AssemblyVersion>4.15.0.0</AssemblyVersion>
25-
<FileVersion>4.15.0.0</FileVersion>
22+
<AssemblyVersion>4.16.0.0</AssemblyVersion>
23+
<FileVersion>4.16.0.0</FileVersion>
2624
<PackageLicenseFile>UNLICENSE.txt</PackageLicenseFile>
2725
<PackageIcon>icon.png</PackageIcon>
2826
</PropertyGroup>
@@ -43,6 +41,10 @@ I discovered a bug I introduced with the previous version where I wrote the sche
4341
<DocumentationFile>bin\Release\netstandard2.0\FlatFiles.xml</DocumentationFile>
4442
</PropertyGroup>
4543

44+
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|netstandard2.1|AnyCPU'">
45+
<DocumentationFile>bin\Release\netstandard2.1\FlatFiles.xml</DocumentationFile>
46+
</PropertyGroup>
47+
4648
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|netcoreapp3.0|AnyCPU'">
4749
<DocumentationFile>bin\Debug\netcoreapp3.0\FlatFiles.xml</DocumentationFile>
4850
</PropertyGroup>
@@ -88,6 +90,11 @@ I discovered a bug I introduced with the previous version where I wrote the sche
8890
<PackageReference Include="System.Data.Common" Version="4.3.0" />
8991
</ItemGroup>
9092

93+
<ItemGroup Condition="'$(TargetFramework)'=='netstandard2.1'">
94+
<PackageReference Include="System.Reflection.Emit" Version="4.6.0" />
95+
<PackageReference Include="System.Data.Common" Version="4.3.0" />
96+
</ItemGroup>
97+
9198
<ItemGroup Condition="'$(TargetFramework)'=='netcoreapp3.0'">
9299
<PackageReference Include="System.Reflection.Emit" Version="4.6.0" />
93100
<PackageReference Include="System.Data.Common" Version="4.3.0" />

FlatFiles/TypeMapping/FixedLengthTypeMapper.cs

Lines changed: 88 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,16 @@ public interface IFixedLengthTypeMapper<TEntity> : IFixedLengthTypeConfiguration
470470
/// <returns>The entities that are extracted from the file.</returns>
471471
IEnumerable<TEntity> Read(TextReader reader, FixedLengthOptions options = null);
472472

473+
#if !NET451 && !NETSTANDARD1_6 && !NETSTANDARD2_0
474+
/// <summary>
475+
/// Reads the entities from the given reader.
476+
/// </summary>
477+
/// <param name="reader">A reader over the fixed-length document.</param>
478+
/// <param name="options">The options controlling how the fixed-length document is read.</param>
479+
/// <returns>An asynchronous enumerable over the entities.</returns>
480+
IAsyncEnumerable<TEntity> ReadAsync(TextReader reader, FixedLengthOptions options = null);
481+
#endif
482+
473483
/// <summary>
474484
/// Gets a typed reader to read entities from the underlying document.
475485
/// </summary>
@@ -494,6 +504,16 @@ public interface IFixedLengthTypeMapper<TEntity> : IFixedLengthTypeConfiguration
494504
/// <param name="options">The options controlling how the separated value document is written.</param>
495505
Task WriteAsync(TextWriter writer, IEnumerable<TEntity> entities, FixedLengthOptions options = null);
496506

507+
#if !NET451 && !NETSTANDARD1_6 && !NETSTANDARD2_0
508+
/// <summary>
509+
/// Writes the given entities to the given writer.
510+
/// </summary>
511+
/// <param name="writer">A writer over the fixed-length document.</param>
512+
/// <param name="entities">The entities to write to the document.</param>
513+
/// <param name="options">The options controlling how the separated value document is written.</param>
514+
Task WriteAsync(TextWriter writer, IAsyncEnumerable<TEntity> entities, FixedLengthOptions options = null);
515+
#endif
516+
497517
/// <summary>
498518
/// Gets a typed writer to write entities to the underlying document.
499519
/// </summary>
@@ -750,6 +770,16 @@ public interface IDynamicFixedLengthTypeMapper : IDynamicFixedLengthTypeConfigur
750770
/// <returns>The entities that are extracted from the file.</returns>
751771
IEnumerable<object> Read(TextReader reader, FixedLengthOptions options = null);
752772

773+
#if !NET451 && !NETSTANDARD1_6 && !NETSTANDARD2_0
774+
/// <summary>
775+
/// Reads the entities from the given reader.
776+
/// </summary>
777+
/// <param name="reader">A reader over the fixed-length document.</param>
778+
/// <param name="options">The options controlling how the fixed-length document is read.</param>
779+
/// <returns>An asynchronous enumerable over the entities.</returns>
780+
IAsyncEnumerable<object> ReadAsync(TextReader reader, FixedLengthOptions options = null);
781+
#endif
782+
753783
/// <summary>
754784
/// Gets a typed reader to read entities from the underlying document.
755785
/// </summary>
@@ -774,6 +804,16 @@ public interface IDynamicFixedLengthTypeMapper : IDynamicFixedLengthTypeConfigur
774804
/// <param name="options">The options used to format the output.</param>
775805
Task WriteAsync(TextWriter writer, IEnumerable<object> entities, FixedLengthOptions options = null);
776806

807+
#if !NET451 && !NETSTANDARD1_6 && !NETSTANDARD2_0
808+
/// <summary>
809+
/// Writes the given entities to the given stream.
810+
/// </summary>
811+
/// <param name="writer">A writer over the separated value document.</param>
812+
/// <param name="entities">The entities to write to the stream.</param>
813+
/// <param name="options">The options used to format the output.</param>
814+
Task WriteAsync(TextWriter writer, IAsyncEnumerable<object> entities, FixedLengthOptions options = null);
815+
#endif
816+
777817
/// <summary>
778818
/// Gets a typed writer to write entities to the underlying document.
779819
/// </summary>
@@ -1325,19 +1365,17 @@ private static IMemberAccessor GetMember<TProp>(Expression<Func<TEntity, TProp>>
13251365

13261366
public IEnumerable<TEntity> Read(TextReader reader, FixedLengthOptions options = null)
13271367
{
1328-
var schema = getSchema();
1329-
var fixedLengthReader = new FixedLengthReader(reader, schema, options);
1330-
return Read(fixedLengthReader);
1368+
var typedReader = GetReader(reader, options);
1369+
return typedReader.ReadAll();
13311370
}
13321371

1333-
private IEnumerable<TEntity> Read(FixedLengthReader reader)
1372+
#if !NET451 && !NETSTANDARD1_6 && !NETSTANDARD2_0
1373+
public IAsyncEnumerable<TEntity> ReadAsync(TextReader reader, FixedLengthOptions options = null)
13341374
{
1335-
var typedReader = GetTypedReader(reader);
1336-
while (typedReader.Read())
1337-
{
1338-
yield return typedReader.Current;
1339-
}
1375+
var typedReader = GetReader(reader, options);
1376+
return typedReader.ReadAllAsync();
13401377
}
1378+
#endif
13411379

13421380
public IFixedLengthTypedReader<TEntity> GetReader(TextReader reader, FixedLengthOptions options = null)
13431381
{
@@ -1358,33 +1396,31 @@ public void Write(TextWriter writer, IEnumerable<TEntity> entities, FixedLengthO
13581396
{
13591397
throw new ArgumentNullException(nameof(entities));
13601398
}
1361-
var schema = getSchema();
1362-
var fixedLengthWriter = new FixedLengthWriter(writer, schema, options);
1363-
Write(fixedLengthWriter, entities);
1364-
}
1365-
1366-
private void Write(IWriterWithMetadata writer, IEnumerable<TEntity> entities)
1367-
{
1368-
var typedWriter = GetTypedWriter(writer);
1399+
var typedWriter = GetWriter(writer, options);
13691400
typedWriter.WriteAll(entities);
13701401
}
13711402

1372-
public async Task WriteAsync(TextWriter writer, IEnumerable<TEntity> entities, FixedLengthOptions options = null)
1403+
public Task WriteAsync(TextWriter writer, IEnumerable<TEntity> entities, FixedLengthOptions options = null)
13731404
{
13741405
if (entities == null)
13751406
{
13761407
throw new ArgumentNullException(nameof(entities));
13771408
}
1378-
var schema = getSchema();
1379-
var fixedLengthWriter = new FixedLengthWriter(writer, schema, options);
1380-
await WriteAsync(fixedLengthWriter, entities).ConfigureAwait(false);
1409+
var typedWriter = GetWriter(writer, options);
1410+
return typedWriter.WriteAllAsync(entities);
13811411
}
13821412

1383-
private async Task WriteAsync(IWriterWithMetadata writer, IEnumerable<TEntity> entities)
1413+
#if !NET451 && !NETSTANDARD1_6 && !NETSTANDARD2_0
1414+
public Task WriteAsync(TextWriter writer, IAsyncEnumerable<TEntity> entities, FixedLengthOptions options = null)
13841415
{
1385-
var typedWriter = GetTypedWriter(writer);
1386-
await typedWriter.WriteAllAsync(entities).ConfigureAwait(false);
1416+
if (entities == null)
1417+
{
1418+
throw new ArgumentNullException(nameof(entities));
1419+
}
1420+
var typedWriter = GetWriter(writer, options);
1421+
return typedWriter.WriteAllAsync(entities);
13871422
}
1423+
#endif
13881424

13891425
public ITypedWriter<TEntity> GetWriter(TextWriter writer, FixedLengthOptions options = null)
13901426
{
@@ -1588,25 +1624,47 @@ private static bool IsNullable(IMemberAccessor accessor)
15881624

15891625
IEnumerable<object> IDynamicFixedLengthTypeMapper.Read(TextReader reader, FixedLengthOptions options)
15901626
{
1591-
return (IEnumerable<object>)Read(reader, options);
1627+
IDynamicFixedLengthTypeMapper untypedMapper = this;
1628+
var untypedReader = untypedMapper.GetReader(reader, options);
1629+
return untypedReader.ReadAll();
15921630
}
15931631

1632+
#if !NET451 && !NETSTANDARD1_6 && !NETSTANDARD2_0
1633+
IAsyncEnumerable<object> IDynamicFixedLengthTypeMapper.ReadAsync(TextReader reader, FixedLengthOptions options)
1634+
{
1635+
IDynamicFixedLengthTypeMapper untypedMapper = this;
1636+
var untypedReader = untypedMapper.GetReader(reader, options);
1637+
return untypedReader.ReadAllAsync();
1638+
}
1639+
#endif
1640+
15941641
IFixedLengthTypedReader<object> IDynamicFixedLengthTypeMapper.GetReader(TextReader reader, FixedLengthOptions options)
15951642
{
15961643
return (IFixedLengthTypedReader<object>)GetReader(reader, options);
15971644
}
15981645

15991646
void IDynamicFixedLengthTypeMapper.Write(TextWriter writer, IEnumerable<object> entities, FixedLengthOptions options)
16001647
{
1601-
var converted = entities.Cast<TEntity>();
1602-
Write(writer, converted, options);
1648+
IDynamicFixedLengthTypeMapper untypedMapper = this;
1649+
var untypedWriter = untypedMapper.GetWriter(writer, options);
1650+
untypedWriter.WriteAll(entities);
1651+
}
1652+
1653+
Task IDynamicFixedLengthTypeMapper.WriteAsync(TextWriter writer, IEnumerable<object> entities, FixedLengthOptions options)
1654+
{
1655+
IDynamicFixedLengthTypeMapper untypedMapper = this;
1656+
var untypedWriter = untypedMapper.GetWriter(writer, options);
1657+
return untypedWriter.WriteAllAsync(entities);
16031658
}
16041659

1605-
async Task IDynamicFixedLengthTypeMapper.WriteAsync(TextWriter writer, IEnumerable<object> entities, FixedLengthOptions options)
1660+
#if !NET451 && !NETSTANDARD1_6 && !NETSTANDARD2_0
1661+
Task IDynamicFixedLengthTypeMapper.WriteAsync(TextWriter writer, IAsyncEnumerable<object> entities, FixedLengthOptions options)
16061662
{
1607-
var converted = entities.Cast<TEntity>();
1608-
await WriteAsync(writer, converted, options).ConfigureAwait(false);
1663+
IDynamicFixedLengthTypeMapper untypedMapper = this;
1664+
var untypedWriter = untypedMapper.GetWriter(writer, options);
1665+
return untypedWriter.WriteAllAsync(entities);
16091666
}
1667+
#endif
16101668

16111669
ITypedWriter<object> IDynamicFixedLengthTypeMapper.GetWriter(TextWriter writer, FixedLengthOptions options)
16121670
{

0 commit comments

Comments
 (0)