Skip to content

Supply the list element index in the second #each parameter when enumerating #99

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

Merged
merged 1 commit into from
Sep 18, 2023
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
37 changes: 20 additions & 17 deletions src/Serilog.Expressions/Templates/Compilation/CompiledRepetition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,22 @@ class CompiledRepetition : CompiledTemplate
{
readonly Evaluatable _enumerable;
readonly string? _keyOrElementName;
readonly string? _valueName;
readonly string? _valueOrIndexName;
readonly CompiledTemplate _body;
readonly CompiledTemplate? _delimiter;
readonly CompiledTemplate? _alternative;

public CompiledRepetition(
Evaluatable enumerable,
string? keyOrElementName,
string? valueName,
string? valueOrIndexName,
CompiledTemplate body,
CompiledTemplate? delimiter,
CompiledTemplate? alternative)
{
_enumerable = enumerable;
_keyOrElementName = keyOrElementName;
_valueName = valueName;
_valueOrIndexName = valueOrIndexName;
_body = body;
_delimiter = delimiter;
_alternative = alternative;
Expand All @@ -52,29 +52,32 @@ public override void Evaluate(EvaluationContext ctx, TextWriter output)
return;
}

if (enumerable is SequenceValue sv)
if (enumerable is SequenceValue sequence)
{
if (sv.Elements.Count == 0)
if (sequence.Elements.Count == 0)
{
_alternative?.Evaluate(ctx, output);
return;
}

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

if (first)
first = false;
else
if (i != 0)
{
_delimiter?.Evaluate(ctx, output);
}

var local = _keyOrElementName != null
? new(ctx.LogEvent, Locals.Set(ctx.Locals, _keyOrElementName, element))
? new EvaluationContext(ctx.LogEvent, Locals.Set(ctx.Locals, _keyOrElementName, element))
: ctx;

local = _valueOrIndexName != null
? new EvaluationContext(local.LogEvent, Locals.Set(local.Locals, _valueOrIndexName, new ScalarValue(i)))
: local;

_body.Evaluate(local, output);
}

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

local = _valueName != null
? new(local.LogEvent, Locals.Set(local.Locals, _valueName, member.Value))
local = _valueOrIndexName != null
? new(local.LogEvent, Locals.Set(local.Locals, _valueOrIndexName, member.Value))
: local;

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

local = _valueName != null
? new(local.LogEvent, Locals.Set(local.Locals, _valueName, element.Value))
local = _valueOrIndexName != null
? new(local.LogEvent, Locals.Set(local.Locals, _valueOrIndexName, element.Value))
: local;

_body.Evaluate(local, output);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ A{#if false}B{#else if false}C{#else if true}D{#else}E{#end} ⇶ AD
A{#if false}B{#else if true}C{#end} ⇶ AC
{#if true}A{#if false}B{#else}C{#end}D{#end} ⇶ ACD
{#each a in [1,2,3]}<{a}>{#delimit},{#end} ⇶ <1>,<2>,<3>
{#each a, i in [1,2,3]}<{a}>({i}){#delimit},{#end} ⇶ <1>(0),<2>(1),<3>(2)
{#each a in {x: 1, y: 2}}{a}{#end} ⇶ xy
{#each a, b in {x: 1, y: 2}}{a}.{b}{#end} ⇶ x.1y.2
{#if true}A{#each a in [1]}B{a}{#end}C{#end}D ⇶ AB1CD
Expand Down