Skip to content

Commit

Permalink
Remove null values and skip empty documents.
Browse files Browse the repository at this point in the history
  • Loading branch information
drasmart committed Jun 10, 2024
1 parent cbe9dbf commit 3ac6517
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions Examples/ExampleBase/ExampleBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,36 @@ protected static IEnumerable<Dictionary<string, object>> GetEvidence(
// Consume the stream start event.
yamlReader.Consume<StreamStart>();
int records = 0;
int skipped = 0;
// Keep going as long as we have more document records.
while (yamlReader.TryConsume<DocumentStart>(out _))
{
// Output progress.
records++;
if (logger != null && records % 1000 == 0)
{
logger.LogInformation($"Processed {records} records");
logger.LogInformation($"Processed {records} records ({skipped} skipped)");
}

// Deserialize the record
var data = deserializer.Deserialize<Dictionary<string, object>>(yamlReader);
yield return data;

// Remove null values
foreach(var keyWithNullValue in data.Where(kvp => kvp.Value is null).Select(kvp => kvp.Key).ToList())
{
logger.LogWarning($"Document at offset {records-1} contains null value for key: '{keyWithNullValue}'!");
data.Remove(keyWithNullValue);
}

if (data.Count > 0)
{
yield return data;
}
else
{
logger.LogWarning($"Document at offset {records - 1} contains no usable evidence!");
++skipped;
}

// Required to move to the start of the next record.
yamlReader.TryConsume<DocumentEnd>(out _);
Expand Down

0 comments on commit 3ac6517

Please sign in to comment.