Skip to content

Commit

Permalink
Quick multiplexing metadata example.
Browse files Browse the repository at this point in the history
  • Loading branch information
jehugaleahsa committed May 7, 2019
1 parent eb37279 commit 7447d66
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 5 deletions.
2 changes: 1 addition & 1 deletion FlatFiles.Benchmark/FlatFiles.Benchmark.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.11.4" />
<PackageReference Include="BenchmarkDotNet" Version="0.11.5" />
<PackageReference Include="CsvHelper" Version="12.1.2" />
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion FlatFiles.Benchmark/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ private static void runBenchmarks()
{
var configuration = new ManualConfig()
{
KeepBenchmarkFiles = false
Options = ConfigOptions.KeepBenchmarkFiles
};
configuration.Add(StatisticColumn.Min);
configuration.Add(StatisticColumn.Max);
Expand Down
65 changes: 62 additions & 3 deletions FlatFiles.Test/SeparatedValueMultipleSchemaTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,59 @@ public void TestTypeMapper_ReadThreeTypes()
Assert.IsFalse(reader.Read());
}

private SeparatedValueTypeMapperSelector getTypeMapperSelector()
[TestMethod]
public void TestTypeMapper_ReadThreeTypes_WithMetadataRecord()
{
StringWriter stringWriter = new StringWriter();
var injector = getTypeMapperInjector();
var writer = injector.GetWriter(stringWriter);
writer.Write(new HeaderRecord { BatchName = "First Batch", RecordCount = 2 });
writer.Write(new DataRecord { Id = 1, Name = "Bob Smith", CreatedOn = new DateTime(2018, 06, 04), TotalAmount = 12.34m });
writer.Write(new DataRecord { Id = 2, Name = "Jane Doe", CreatedOn = new DateTime(2018, 06, 05), TotalAmount = 34.56m });
writer.Write(new FooterRecord { TotalAmount = 46.9m, AverageAmount = 23.45m, IsCriteriaMet = true });
string output = stringWriter.ToString();
Assert.AreEqual(@"First Batch,2
1,Bob Smith,20180604,12.34
2,Jane Doe,20180605,34.56
46.9,23.45,True
", output);

var selector = getTypeMapperSelector(true);
var stringReader = new StringReader(output);
var reader = selector.GetReader(stringReader);

Assert.IsTrue(reader.Read(), "The header record could not be read.");
Assert.IsInstanceOfType(reader.Current, typeof(HeaderRecord));
Assert.AreEqual("First Batch", ((HeaderRecord)reader.Current).BatchName);
Assert.AreEqual(2, ((HeaderRecord)reader.Current).RecordCount);

Assert.IsTrue(reader.Read(), "The first data record could not be read.");
Assert.IsInstanceOfType(reader.Current, typeof(DataRecord));
Assert.AreEqual(1, ((DataRecord)reader.Current).Id);
Assert.AreEqual("Bob Smith", ((DataRecord)reader.Current).Name);
Assert.AreEqual(new DateTime(2018, 6, 4), ((DataRecord)reader.Current).CreatedOn);
Assert.AreEqual(12.34m, ((DataRecord)reader.Current).TotalAmount);

Assert.IsTrue(reader.Read(), "The second data record could not be read.");
Assert.IsInstanceOfType(reader.Current, typeof(DataRecord));
Assert.AreEqual(2, ((DataRecord)reader.Current).Id);
Assert.AreEqual("Jane Doe", ((DataRecord)reader.Current).Name);
Assert.AreEqual(new DateTime(2018, 6, 5), ((DataRecord)reader.Current).CreatedOn);
Assert.AreEqual(34.56m, ((DataRecord)reader.Current).TotalAmount);

Assert.IsTrue(reader.Read(), "The footer record could not be read.");
Assert.IsInstanceOfType(reader.Current, typeof(FooterRecord));
Assert.AreEqual(46.9m, ((FooterRecord)reader.Current).TotalAmount);
Assert.AreEqual(23.45m, ((FooterRecord)reader.Current).AverageAmount);
Assert.IsTrue(((FooterRecord)reader.Current).IsCriteriaMet);

Assert.IsFalse(reader.Read());
}

private SeparatedValueTypeMapperSelector getTypeMapperSelector(bool hasMetadata = false)
{
var selector = new SeparatedValueTypeMapperSelector();
selector.WithDefault(getRecordTypeMapper());
selector.WithDefault(getRecordTypeMapper(hasMetadata));
selector.When(x => x.Length == 2).Use(getHeaderTypeMapper());
selector.When(x => x.Length == 3).Use(getFooterTypeMapper());
return selector;
Expand All @@ -178,13 +227,21 @@ private static ISeparatedValueTypeMapper<HeaderRecord> getHeaderTypeMapper()
return mapper;
}

private static ISeparatedValueTypeMapper<DataRecord> getRecordTypeMapper()
private static ISeparatedValueTypeMapper<DataRecord> getRecordTypeMapper(bool hasMetadata = false)
{
var mapper = SeparatedValueTypeMapper.Define(() => new DataRecord());
mapper.Property(x => x.Id);
mapper.Property(x => x.Name);
mapper.Property(x => x.CreatedOn).InputFormat("yyyyMMdd").OutputFormat("yyyyMMdd");
mapper.Property(x => x.TotalAmount);
if (hasMetadata)
{
mapper.CustomMapping(new RecordNumberColumn("row_num")
{
IncludeSchema = true,
IncludeSkippedRecords = true
}).WithReader(r => r.RecordNumber);
}
return mapper;
}

Expand Down Expand Up @@ -222,6 +279,8 @@ public class DataRecord
public DateTime? CreatedOn { get; set; }

public decimal TotalAmount { get; set; }

public int? RecordNumber { get; set; }
}
}
}

0 comments on commit 7447d66

Please sign in to comment.