Skip to content

Commit efcc72e

Browse files
authored
Merge pull request #99 from nblumhardt/list-enumeration-index
Supply the list element index in the second #each parameter when enumerating
2 parents 18ee4be + 9303294 commit efcc72e

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

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);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ 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
2829
{#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

0 commit comments

Comments
 (0)