Skip to content

Commit c621685

Browse files
committed
Handle unrecognized rows using schema selectors.
1 parent a0d9760 commit c621685

File tree

8 files changed

+74
-8
lines changed

8 files changed

+74
-8
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 4.11.0 (2020-10-09)
2+
**Summary* - Allow handling unrecognized rows when using schema selectors.
3+
4+
Previously, if a record was encountered that could be handled by any of the configured schemas, the selector would throw a generic `FlatFilesException`. Now, a `RecordProcessingException`is thrown instead, which can be ignored causing the record to be skipped.
5+
16
## 4.10.0 (2020-10-06)
27
**Summary** - Add the ability to explicitly write the schema using typed writers.
38

FlatFiles.Test/FixedLengthMultipleSchemaTester.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,27 @@ 46.9 23.45 True
155155
Assert.IsFalse(reader.Read());
156156
}
157157

158+
[TestMethod]
159+
[ExpectedException(typeof(RecordProcessingException))]
160+
public void TestReader_UnknownType()
161+
{
162+
var stringReader = new StringReader("What's this weird thing?");
163+
var selector = getSchemaSelector();
164+
var reader = new FixedLengthReader(stringReader, selector);
165+
166+
reader.Read();
167+
}
168+
169+
[TestMethod]
170+
public void TestReader_UnknownType_IgnoreUnknown_SkipsRecord()
171+
{
172+
var stringReader = new StringReader("What's this weird thing?");
173+
var selector = getSchemaSelector();
174+
var reader = new FixedLengthReader(stringReader, selector);
175+
reader.RecordError += (o, e) => e.IsHandled = true;
176+
Assert.IsFalse(reader.Read());
177+
}
178+
158179
private FixedLengthTypeMapperSelector getTypeMapperSelector()
159180
{
160181
var selector = new FixedLengthTypeMapperSelector();

FlatFiles.Test/SeparatedValueMultipleSchemaTester.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,27 @@ public void TestTypeMapper_ReadThreeTypes_WithMetadataRecord()
201201
Assert.IsFalse(reader.Read());
202202
}
203203

204+
[TestMethod]
205+
[ExpectedException(typeof(RecordProcessingException))]
206+
public void TestReader_UnknownType()
207+
{
208+
var stringReader = new StringReader("What's this weird thing?");
209+
var selector = getSchemaSelector();
210+
var reader = new SeparatedValueReader(stringReader, selector);
211+
212+
reader.Read();
213+
}
214+
215+
[TestMethod]
216+
public void TestReader_UnknownType_IgnoreUnknown_SkipsRecord()
217+
{
218+
var stringReader = new StringReader("What's this weird thing?");
219+
var selector = getSchemaSelector();
220+
var reader = new SeparatedValueReader(stringReader, selector);
221+
reader.RecordError += (o, e) => e.IsHandled = true;
222+
Assert.IsFalse(reader.Read());
223+
}
224+
204225
private SeparatedValueTypeMapperSelector getTypeMapperSelector(bool hasMetadata = false)
205226
{
206227
var selector = new SeparatedValueTypeMapperSelector();

FlatFiles/FixedLengthReader.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,13 @@ private FixedLengthSchema GetSchema(string record)
413413
{
414414
return metadata.ExecutionContext.Schema;
415415
}
416-
return schemaSelector.GetSchema(record);
416+
FixedLengthSchema schema = schemaSelector.GetSchema(record);
417+
if (schema != null)
418+
{
419+
return schema;
420+
}
421+
ProcessError(new RecordProcessingException(metadata, Resources.MissingMatcher));
422+
return null;
417423
}
418424

419425
private string ReadNextRecord()

FlatFiles/FixedLengthSchemaSelector.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ internal FixedLengthSchema GetSchema(string record)
9292
defaultMatcher.Action?.Invoke();
9393
return defaultMatcher.Schema;
9494
}
95-
throw new FlatFileException(Resources.MissingMatcher);
95+
return null;
9696
}
9797

9898
private class SchemaMatcher

FlatFiles/FlatFiles.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +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>Allow explicitly writing the schema using typed mappers.</PackageReleaseNotes>
13+
<PackageReleaseNotes>Allow handling unrecognized rows when using schema selectors.</PackageReleaseNotes>
1414
<SignAssembly>true</SignAssembly>
1515
<AssemblyOriginatorKeyFile>FlatFiles.snk</AssemblyOriginatorKeyFile>
16-
<Version>4.10.0</Version>
16+
<Version>4.11.0</Version>
1717
</PropertyGroup>
1818

1919
<PropertyGroup>
2020
<LangVersion>8.0</LangVersion>
2121
<PackageIconUrl></PackageIconUrl>
22-
<AssemblyVersion>4.10.0.0</AssemblyVersion>
23-
<FileVersion>4.10.0.0</FileVersion>
22+
<AssemblyVersion>4.11.0.0</AssemblyVersion>
23+
<FileVersion>4.11.0.0</FileVersion>
2424
<PackageLicenseFile>UNLICENSE.txt</PackageLicenseFile>
2525
<PackageIcon>icon.png</PackageIcon>
2626
</PropertyGroup>

FlatFiles/SeparatedValueReader.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,11 +360,24 @@ private SeparatedValueSchema GetSchema(string[] rawValues)
360360
{
361361
return metadata.ExecutionContext.Schema;
362362
}
363-
return schemaSelector.GetSchema(rawValues);
363+
SeparatedValueSchema schema = schemaSelector.GetSchema(rawValues);
364+
if (schema != null)
365+
{
366+
return schema;
367+
}
368+
ProcessError(new RecordProcessingException(metadata, Resources.MissingMatcher));
369+
return null;
364370
}
365371

366372
private bool IsSkipped(string[] values)
367373
{
374+
if (metadata.ExecutionContext.Schema == null && schemaSelector != null)
375+
{
376+
// A schema was not found by the selector for the given record.
377+
// If we got here then we know the raised exception was handled and suppressed.
378+
// Therefore we skip the record and go on to the next one.
379+
return true;
380+
}
368381
if (RecordRead == null)
369382
{
370383
return false;

FlatFiles/SeparatedValueSchemaSelector.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ internal SeparatedValueSchema GetSchema(string[] values)
9999
defaultMatcher.Action?.Invoke();
100100
return defaultMatcher.Schema;
101101
}
102-
throw new FlatFileException(Resources.MissingMatcher);
102+
return null;
103103
}
104104

105105
private class SchemaMatcher

0 commit comments

Comments
 (0)