Skip to content

Commit 99c65f8

Browse files
committed
Support wildcard dictionary element accessors
1 parent 2da48ee commit 99c65f8

File tree

4 files changed

+50
-2
lines changed

4 files changed

+50
-2
lines changed

src/Serilog.Expressions/Expressions/Runtime/RuntimeOperators.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,11 @@ public static LogEventPropertyValue IsDefined(LogEventPropertyValue? value)
374374
return ScalarBoolean(structure.Properties.Any(e => Coerce.IsTrue(pred(e.Value))));
375375
}
376376

377+
if (items is DictionaryValue dictionary)
378+
{
379+
return ScalarBoolean(dictionary.Elements.Any(e => Coerce.IsTrue(pred(e.Value))));
380+
}
381+
377382
return null;
378383
}
379384

@@ -391,6 +396,11 @@ public static LogEventPropertyValue IsDefined(LogEventPropertyValue? value)
391396
{
392397
return ScalarBoolean(structure.Properties.All(e => Coerce.IsTrue(pred(e.Value))));
393398
}
399+
400+
if (items is DictionaryValue dictionary)
401+
{
402+
return ScalarBoolean(dictionary.Elements.All(e => Coerce.IsTrue(pred(e.Value))));
403+
}
394404

395405
return null;
396406
}

test/Serilog.Expressions.Tests/Cases/expression-evaluation-cases.asv

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,12 @@ User.Name ⇶ 'nblumhardt'
203203
{k:'test'}[?] like 'TEST' ci ⇶ true
204204
{k:'test'}[?] like '%TES%' ci ⇶ true
205205
{k:'test'}[?] = 'none' ⇶ false
206+
test_dict({k:'test'})[?] = 'test' ⇶ true
207+
test_dict({k:'test'})[?] like 'test' ⇶ true
208+
test_dict({k:'test'})[?] like 'TEST' ⇶ false
209+
test_dict({k:'test'})[?] like 'TEST' ci ⇶ true
210+
test_dict({k:'test'})[?] like '%TES%' ci ⇶ true
211+
test_dict({k:'test'})[?] = 'none' ⇶ false
206212

207213
// Text and regex
208214
ismatch('foo', 'f') ⇶ true

test/Serilog.Expressions.Tests/ExpressionEvaluationTests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ public void ExpressionsAreCorrectlyEvaluated(string expr, string result)
2727
})));
2828

2929
var frFr = CultureInfo.GetCultureInfoByIetfLanguageTag("fr-FR");
30-
var actual = SerilogExpression.Compile(expr, formatProvider: frFr)(evt);
31-
var expected = SerilogExpression.Compile(result)(evt);
30+
var testHelpers = new TestHelperNameResolver();
31+
var actual = SerilogExpression.Compile(expr, formatProvider: frFr, testHelpers)(evt);
32+
var expected = SerilogExpression.Compile(result, nameResolver: testHelpers)(evt);
3233

3334
if (expected is null)
3435
{
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System.Collections.Generic;
2+
using System.Diagnostics.CodeAnalysis;
3+
using System.Linq;
4+
using System.Reflection;
5+
using Serilog.Events;
6+
7+
namespace Serilog.Expressions.Tests.Support;
8+
9+
public class TestHelperNameResolver: NameResolver
10+
{
11+
public override bool TryResolveFunctionName(string name, [MaybeNullWhen(false)] out MethodInfo implementation)
12+
{
13+
if (name == "test_dict")
14+
{
15+
implementation = GetType().GetMethod(nameof(TestDict))!;
16+
return true;
17+
}
18+
19+
implementation = null;
20+
return false;
21+
}
22+
23+
public static LogEventPropertyValue? TestDict(LogEventPropertyValue? value)
24+
{
25+
if (value is not StructureValue sv)
26+
return null;
27+
28+
return new DictionaryValue(sv.Properties.Select(kv =>
29+
KeyValuePair.Create(new ScalarValue(kv.Name), kv.Value)));
30+
}
31+
}

0 commit comments

Comments
 (0)