public override async IAsyncEnumerable <Path> ComputeSelect(EvaluationContext ctx) { if (ListCandidate != null) { // Expression select var select = ListCandidate.ComputeSelect(ctx); await foreach (var atom in select) { yield return(atom); } // Expression paths var paths = ListCandidate.ComputePaths(ctx); await foreach (var path in paths) { yield return(path.Append("Id")); } // Inner template select var scopedCtx = ctx.Clone(); scopedCtx.SetLocalVariable(IteratorVariableName, new TemplateVariable( eval: TemplateUtil.VariableThatThrows(IteratorVariableName), selectResolver: () => select, pathsResolver: () => paths )); await foreach (var atom in Template.ComputeSelect(scopedCtx)) { yield return(atom); } } }
/// <summary> /// Clones the <see cref="EvaluationContext"/> and returns a new one that contains the new variable /// </summary> private EvaluationContext GetScopeLocalContext(EvaluationContext ctx) { TemplateVariable variable; if (Value == null) { variable = new TemplateVariable( value: null, selectResolver: () => Value.ComputeSelect(ctx), pathsResolver: () => Value.ComputePaths(ctx)); } else { variable = new TemplateVariable( evalAsync: () => Value.Evaluate(ctx), selectResolver: () => Value.ComputeSelect(ctx), pathsResolver: () => Value.ComputePaths(ctx)); } var clone = ctx.Clone(); clone.SetLocalVariable(VariableName, variable); return(clone); }
public override async Task GenerateOutput(StringBuilder builder, EvaluationContext ctx, Func <string, string> encodeFunc = null) { if (ListCandidate != null) { var listObj = (await ListCandidate.Evaluate(ctx)) ?? new List <object>(); if (listObj is IList list) { int index = 0; foreach (var listItem in list) { // Initialize new evaluation context with the new variable in it var scopedCtx = ctx.Clone(); scopedCtx.SetLocalVariable(IteratorVariableName, new TemplateVariable( evalAsync: () => Task.FromResult(listItem), selectResolver: () => AsyncUtil.Empty <Path>(), // It doesn't matter when generating output pathsResolver: () => AsyncUtil.Empty <Path>() // It doesn't matter when generating output )); // Index, useful for setting line numbers scopedCtx.SetLocalVariable(IteratorVariableName + "_index", new TemplateVariable( evalAsync: () => Task.FromResult <object>(index), selectResolver: () => AsyncUtil.Empty <Path>(), // It doesn't matter when generating output pathsResolver: () => AsyncUtil.Empty <Path>() // It doesn't matter when generating output )); // Run the template again on that context await Template.GenerateOutput(builder, scopedCtx, encodeFunc); index++; } } else { throw new TemplateException($"Foreach expression could not be applied. Expression ({ListCandidate}) does not evaluate to a list"); } } }