Skip to content
This repository was archived by the owner on Feb 27, 2022. It is now read-only.

Fix evaluation of filters that return structure or sequence values #32

Merged
merged 1 commit into from
Apr 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ public static object Expose(object internalValue)

var sequence = internalValue as SequenceValue;
if (sequence != null)
return sequence.Elements.Select(Expose).ToArray();
return sequence.Elements.Select(ExposeOrRepresent).ToArray();

var structure = internalValue as StructureValue;
if (structure != null)
{
var r = structure.Properties.ToDictionary(p => p.Name, p => Expose(p.Value));
var r = structure.Properties.ToDictionary(p => p.Name, p => ExposeOrRepresent(p.Value));
if (structure.TypeTag != null)
r["$type"] = structure.TypeTag;
return r;
Expand All @@ -58,12 +58,23 @@ public static object Expose(object internalValue)
var dictionary = internalValue as DictionaryValue;
if (dictionary != null)
{
return dictionary.Elements.ToDictionary(p => Expose(p.Key), p => Expose(p.Value));
return dictionary.Elements.ToDictionary(p => ExposeOrRepresent(p.Key), p => ExposeOrRepresent(p.Value));
}

return internalValue;
}

static object ExposeOrRepresent(object internalValue)
{
if (internalValue is Undefined)
return null;

if (internalValue is ScalarValue sv)
return Represent(sv);

return Expose(internalValue);
}

public static LogEventPropertyValue Recapture(object value)
{
return value is LogEventPropertyValue lepv ? lepv : new ScalarValue(value);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Serilog.Events;
using System.Collections.Generic;
using Serilog.Events;
using Serilog.Filters.Expressions.Tests.Support;
using System.Linq;
using Xunit;
Expand Down Expand Up @@ -150,5 +151,15 @@ static void AssertFiltering(string expression, LogEvent match, params LogEvent[]
Assert.Single(sink.Events);
Assert.Same(match, sink.Events.Single());
}

[Fact]
public void StructuresAreExposedAsDictionaries()
{
var evt = Some.InformationEvent("{@Person}", new { Name = "nblumhardt" });
var expr = FilterLanguage.CreateFilter("Person");
var val = expr(evt);
var dict = Assert.IsType<Dictionary<string, object>>(val);
Assert.Equal("nblumhardt", dict["Name"]);
}
}
}