Skip to content

Commit 96cd117

Browse files
authored
Merge branch 'dev' into output-encoding
2 parents a21e3f6 + 609f497 commit 96cd117

File tree

5 files changed

+30
-20
lines changed

5 files changed

+30
-20
lines changed

src/Serilog.Expressions/Expressions/Compilation/Linq/LinqExpressionCompiler.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,8 @@ static ExpressionBody CompileLogical(Func<ExpressionBody, ExpressionBody, Expres
186186
protected override ExpressionBody Transform(AccessorExpression spx)
187187
{
188188
var receiver = Transform(spx.Receiver);
189-
return LX.Call(TryGetStructurePropertyValueMethod, LX.Constant(StringComparison.OrdinalIgnoreCase), receiver, LX.Constant(spx.MemberName, typeof(string)));
189+
return LX.Call(TryGetStructurePropertyValueMethod, LX.Constant(StringComparison.Ordinal), receiver, LX.Constant(spx.MemberName, typeof(string)));
190190
}
191-
192191
protected override ExpressionBody Transform(ConstantExpression cx)
193192
{
194193
return LX.Constant(cx.Constant);

src/Serilog.Expressions/Templates/Compilation/CompiledRepetition.cs

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,22 @@ class CompiledRepetition : CompiledTemplate
2222
{
2323
readonly Evaluatable _enumerable;
2424
readonly string? _keyOrElementName;
25-
readonly string? _valueName;
25+
readonly string? _valueOrIndexName;
2626
readonly CompiledTemplate _body;
2727
readonly CompiledTemplate? _delimiter;
2828
readonly CompiledTemplate? _alternative;
2929

3030
public CompiledRepetition(
3131
Evaluatable enumerable,
3232
string? keyOrElementName,
33-
string? valueName,
33+
string? valueOrIndexName,
3434
CompiledTemplate body,
3535
CompiledTemplate? delimiter,
3636
CompiledTemplate? alternative)
3737
{
3838
_enumerable = enumerable;
3939
_keyOrElementName = keyOrElementName;
40-
_valueName = valueName;
40+
_valueOrIndexName = valueOrIndexName;
4141
_body = body;
4242
_delimiter = delimiter;
4343
_alternative = alternative;
@@ -52,29 +52,32 @@ public override void Evaluate(EvaluationContext ctx, TextWriter output)
5252
return;
5353
}
5454

55-
if (enumerable is SequenceValue sv)
55+
if (enumerable is SequenceValue sequence)
5656
{
57-
if (sv.Elements.Count == 0)
57+
if (sequence.Elements.Count == 0)
5858
{
5959
_alternative?.Evaluate(ctx, output);
6060
return;
6161
}
6262

63-
var first = true;
64-
foreach (var element in sv.Elements)
63+
for (var i = 0; i < sequence.Elements.Count; ++i)
6564
{
66-
if (element == null)
67-
continue; // Should have been invalid but Serilog didn't check and so this does occur in the wild.
65+
// Null elements should have been invalid but Serilog didn't check, and so this does occur in the wild.
66+
var element = sequence.Elements[i] ?? new ScalarValue(null);
6867

69-
if (first)
70-
first = false;
71-
else
68+
if (i != 0)
69+
{
7270
_delimiter?.Evaluate(ctx, output);
71+
}
7372

7473
var local = _keyOrElementName != null
75-
? new(ctx.LogEvent, Locals.Set(ctx.Locals, _keyOrElementName, element))
74+
? new EvaluationContext(ctx.LogEvent, Locals.Set(ctx.Locals, _keyOrElementName, element))
7675
: ctx;
7776

77+
local = _valueOrIndexName != null
78+
? new EvaluationContext(local.LogEvent, Locals.Set(local.Locals, _valueOrIndexName, new ScalarValue(i)))
79+
: local;
80+
7881
_body.Evaluate(local, output);
7982
}
8083

@@ -101,8 +104,8 @@ public override void Evaluate(EvaluationContext ctx, TextWriter output)
101104
? new(ctx.LogEvent, Locals.Set(ctx.Locals, _keyOrElementName, new ScalarValue(member.Name)))
102105
: ctx;
103106

104-
local = _valueName != null
105-
? new(local.LogEvent, Locals.Set(local.Locals, _valueName, member.Value))
107+
local = _valueOrIndexName != null
108+
? new(local.LogEvent, Locals.Set(local.Locals, _valueOrIndexName, member.Value))
106109
: local;
107110

108111
_body.Evaluate(local, output);
@@ -129,8 +132,8 @@ public override void Evaluate(EvaluationContext ctx, TextWriter output)
129132
? new(ctx.LogEvent, Locals.Set(ctx.Locals, _keyOrElementName, element.Key))
130133
: ctx;
131134

132-
local = _valueName != null
133-
? new(local.LogEvent, Locals.Set(local.Locals, _valueName, element.Value))
135+
local = _valueOrIndexName != null
136+
? new(local.LogEvent, Locals.Set(local.Locals, _valueOrIndexName, element.Value))
134137
: local;
135138

136139
_body.Evaluate(local, output);

src/Serilog.Expressions/Templates/Compilation/NameResolution/TemplateLocalNameResolver.cs renamed to src/Serilog.Expressions/Templates/Compilation/NameResolution/TemplateLocalNameBinder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ Template Transform(Repetition rep, Stack<string> locals)
7676
locals.Pop();
7777

7878
return new Repetition(
79-
rep.Enumerable,
79+
ExpressionLocalNameBinder.BindLocalValueNames(rep.Enumerable, locals),
8080
rep.BindingNames,
8181
body,
8282
rep.Delimiter != null ? Transform(rep.Delimiter, locals) : null,

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ false ⇶ false
2424
{a: 1, ..{b: 2}, b: undefined()} ⇶ {a: 1}
2525
{a: 1, ..{a: undefined()}} ⇶ {a: 1}
2626
{..{a: 1}, ..{b: 2, c: 3}} ⇶ {a: 1, b: 2, c: 3}
27+
{a: 1}['a'] ⇶ 1
28+
{a: 1}['A'] ⇶ undefined()
29+
{a: 1}.a ⇶ 1
30+
{a: 1}.A ⇶ undefined()
31+
ElementAt({a: 1}, 'A') ⇶ undefined()
32+
ElementAt({a: 1}, 'A') ci ⇶ 1
2733

2834
// Strings
2935
'' ⇶ ''

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ A{#if false}B{#else if false}C{#else if true}D{#else}E{#end} ⇶ AD
2323
A{#if false}B{#else if true}C{#end} ⇶ AC
2424
{#if true}A{#if false}B{#else}C{#end}D{#end} ⇶ ACD
2525
{#each a in [1,2,3]}<{a}>{#delimit},{#end} ⇶ <1>,<2>,<3>
26+
{#each a, i in [1,2,3]}<{a}>({i}){#delimit},{#end} ⇶ <1>(0),<2>(1),<3>(2)
2627
{#each a in {x: 1, y: 2}}{a}{#end} ⇶ xy
2728
{#each a, b in {x: 1, y: 2}}{a}.{b}{#end} ⇶ x.1y.2
29+
{#each a, b in {x: {y: 'z'}}}{#each c, d in b}A: {a}, C: {c}, D: {d}{#end}{#end} ⇶ A: x, C: y, D: z
2830
{#if true}A{#each a in [1]}B{a}{#end}C{#end}D ⇶ AB1CD
2931
{#each a in []}{a}!{#else}none{#end} ⇶ none
3032
Culture-specific {42.34} ⇶ Culture-specific 42,34

0 commit comments

Comments
 (0)