Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the rest() function aware of @p.X and @p['X'] (breaking change) #108

Merged
merged 1 commit into from
Jun 4, 2024
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
2 changes: 1 addition & 1 deletion src/Serilog.Expressions/Serilog.Expressions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<Description>An embeddable mini-language for filtering, enriching, and formatting Serilog
events, ideal for use with JSON or XML configuration.</Description>
<VersionPrefix>4.0.1</VersionPrefix>
<VersionPrefix>5.0.0</VersionPrefix>
<Authors>Serilog Contributors</Authors>
<TargetFrameworks>netstandard2.1;netstandard2.0;net5.0;net6.0;net7.0</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using Serilog.Expressions;
using Serilog.Expressions.Ast;
using Serilog.Expressions.Compilation;
using Serilog.Expressions.Compilation.Transformations;

namespace Serilog.Templates.Compilation.UnreferencedProperties;
Expand Down Expand Up @@ -47,7 +49,11 @@ protected override IEnumerable<string> Transform(LocalNameExpression nlx)

protected override IEnumerable<string> Transform(AccessorExpression spx)
{
return Transform(spx.Receiver);
if (Pattern.IsAmbientProperty(spx.Receiver, BuiltInProperty.Properties, true))
yield return spx.MemberName;

foreach (var nested in Transform(spx.Receiver))
yield return nested;
}

protected override IEnumerable<string> Transform(LambdaExpression lmx)
Expand Down Expand Up @@ -79,7 +85,18 @@ protected override IEnumerable<string> Transform(ObjectExpression ox)

protected override IEnumerable<string> Transform(IndexerExpression ix)
{
return Transform(ix.Index).Concat(Transform(ix.Receiver));
if (Pattern.IsAmbientProperty(ix.Receiver, BuiltInProperty.Properties, true) &&
Pattern.IsStringConstant(ix.Index, out var name))
{
yield return name;
}
else
{
foreach (var nested in Transform(ix.Index).Concat(Transform(ix.Receiver)))
{
yield return nested;
}
}
}

protected override IEnumerable<string> Transform(IndexOfMatchExpression mx)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,34 @@ public void UnreferencedPropertiesFunctionIsNamedRest()
[Fact]
public void UnreferencedPropertiesExcludeThoseInMessageAndTemplate()
{
Assert.True(new TemplateParser().TryParse("{@m}{A + 1}{#if true}{B}{#end}", out var template, out _));
Assert.True(new TemplateParser().TryParse("{@m}{A + 1}{#if true}{B}{@p.C}{@p['D']}{#end}", out var template, out _));

var function = new UnreferencedPropertiesFunction(template!);
var function = new UnreferencedPropertiesFunction(template);

var evt = new LogEvent(
DateTimeOffset.Now,
LogEventLevel.Debug,
null,
new(new[] {new PropertyToken("C", "{C}")}),
new(new[] {new PropertyToken("E", "{E}")}),
new[]
{
new LogEventProperty("A", new ScalarValue(null)),
new LogEventProperty("B", new ScalarValue(null)),
new LogEventProperty("C", new ScalarValue(null)),
new LogEventProperty("D", new ScalarValue(null)),
new LogEventProperty("E", new ScalarValue(null)),
new LogEventProperty("F", new ScalarValue(null)),
});

var deep = UnreferencedPropertiesFunction.Implementation(function, evt, new ScalarValue(true));

var sv = Assert.IsType<StructureValue>(deep);
var included = Assert.Single(sv.Properties);
Assert.Equal("D", included!.Name);
Assert.Equal("F", included.Name);

var shallow = UnreferencedPropertiesFunction.Implementation(function, evt);
sv = Assert.IsType<StructureValue>(shallow);
Assert.Contains(sv.Properties, p => p.Name == "C");
Assert.Contains(sv.Properties, p => p.Name == "D");
Assert.Contains(sv.Properties, p => p.Name == "E");
Assert.Contains(sv.Properties, p => p.Name == "F");
}
}