Skip to content

Commit

Permalink
Handle unrecognized rows using schema selectors.
Browse files Browse the repository at this point in the history
  • Loading branch information
jehugaleahsa committed Oct 9, 2020
1 parent a0d9760 commit c621685
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 8 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 4.11.0 (2020-10-09)
**Summary* - Allow handling unrecognized rows when using schema selectors.

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.

## 4.10.0 (2020-10-06)
**Summary** - Add the ability to explicitly write the schema using typed writers.

Expand Down
21 changes: 21 additions & 0 deletions FlatFiles.Test/FixedLengthMultipleSchemaTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,27 @@ public void TestTypeMapper_ReadThreeTypes()
Assert.IsFalse(reader.Read());
}

[TestMethod]
[ExpectedException(typeof(RecordProcessingException))]
public void TestReader_UnknownType()
{
var stringReader = new StringReader("What's this weird thing?");
var selector = getSchemaSelector();
var reader = new FixedLengthReader(stringReader, selector);

reader.Read();
}

[TestMethod]
public void TestReader_UnknownType_IgnoreUnknown_SkipsRecord()
{
var stringReader = new StringReader("What's this weird thing?");
var selector = getSchemaSelector();
var reader = new FixedLengthReader(stringReader, selector);
reader.RecordError += (o, e) => e.IsHandled = true;
Assert.IsFalse(reader.Read());
}

private FixedLengthTypeMapperSelector getTypeMapperSelector()
{
var selector = new FixedLengthTypeMapperSelector();
Expand Down
21 changes: 21 additions & 0 deletions FlatFiles.Test/SeparatedValueMultipleSchemaTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,27 @@ public void TestTypeMapper_ReadThreeTypes_WithMetadataRecord()
Assert.IsFalse(reader.Read());
}

[TestMethod]
[ExpectedException(typeof(RecordProcessingException))]
public void TestReader_UnknownType()
{
var stringReader = new StringReader("What's this weird thing?");
var selector = getSchemaSelector();
var reader = new SeparatedValueReader(stringReader, selector);

reader.Read();
}

[TestMethod]
public void TestReader_UnknownType_IgnoreUnknown_SkipsRecord()
{
var stringReader = new StringReader("What's this weird thing?");
var selector = getSchemaSelector();
var reader = new SeparatedValueReader(stringReader, selector);
reader.RecordError += (o, e) => e.IsHandled = true;
Assert.IsFalse(reader.Read());
}

private SeparatedValueTypeMapperSelector getTypeMapperSelector(bool hasMetadata = false)
{
var selector = new SeparatedValueTypeMapperSelector();
Expand Down
8 changes: 7 additions & 1 deletion FlatFiles/FixedLengthReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,13 @@ private FixedLengthSchema GetSchema(string record)
{
return metadata.ExecutionContext.Schema;
}
return schemaSelector.GetSchema(record);
FixedLengthSchema schema = schemaSelector.GetSchema(record);
if (schema != null)
{
return schema;
}
ProcessError(new RecordProcessingException(metadata, Resources.MissingMatcher));
return null;
}

private string ReadNextRecord()
Expand Down
2 changes: 1 addition & 1 deletion FlatFiles/FixedLengthSchemaSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ internal FixedLengthSchema GetSchema(string record)
defaultMatcher.Action?.Invoke();
return defaultMatcher.Schema;
}
throw new FlatFileException(Resources.MissingMatcher);
return null;
}

private class SchemaMatcher
Expand Down
8 changes: 4 additions & 4 deletions FlatFiles/FlatFiles.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@
<RepositoryUrl>https://github.com/jehugaleahsa/FlatFiles.git</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>csv;comma;tab;separated;value;delimited;flat;file;fixed;width;fixed-width;length;fixed-length;parser;parsing;parse</PackageTags>
<PackageReleaseNotes>Allow explicitly writing the schema using typed mappers.</PackageReleaseNotes>
<PackageReleaseNotes>Allow handling unrecognized rows when using schema selectors.</PackageReleaseNotes>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>FlatFiles.snk</AssemblyOriginatorKeyFile>
<Version>4.10.0</Version>
<Version>4.11.0</Version>
</PropertyGroup>

<PropertyGroup>
<LangVersion>8.0</LangVersion>
<PackageIconUrl></PackageIconUrl>
<AssemblyVersion>4.10.0.0</AssemblyVersion>
<FileVersion>4.10.0.0</FileVersion>
<AssemblyVersion>4.11.0.0</AssemblyVersion>
<FileVersion>4.11.0.0</FileVersion>
<PackageLicenseFile>UNLICENSE.txt</PackageLicenseFile>
<PackageIcon>icon.png</PackageIcon>
</PropertyGroup>
Expand Down
15 changes: 14 additions & 1 deletion FlatFiles/SeparatedValueReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -360,11 +360,24 @@ private SeparatedValueSchema GetSchema(string[] rawValues)
{
return metadata.ExecutionContext.Schema;
}
return schemaSelector.GetSchema(rawValues);
SeparatedValueSchema schema = schemaSelector.GetSchema(rawValues);
if (schema != null)
{
return schema;
}
ProcessError(new RecordProcessingException(metadata, Resources.MissingMatcher));
return null;
}

private bool IsSkipped(string[] values)
{
if (metadata.ExecutionContext.Schema == null && schemaSelector != null)
{
// A schema was not found by the selector for the given record.
// If we got here then we know the raised exception was handled and suppressed.
// Therefore we skip the record and go on to the next one.
return true;
}
if (RecordRead == null)
{
return false;
Expand Down
2 changes: 1 addition & 1 deletion FlatFiles/SeparatedValueSchemaSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ internal SeparatedValueSchema GetSchema(string[] values)
defaultMatcher.Action?.Invoke();
return defaultMatcher.Schema;
}
throw new FlatFileException(Resources.MissingMatcher);
return null;
}

private class SchemaMatcher
Expand Down

0 comments on commit c621685

Please sign in to comment.