Skip to content

Commit

Permalink
prepare the ground for Lambda filters
Browse files Browse the repository at this point in the history
  • Loading branch information
NeVeSpl committed Feb 11, 2024
1 parent af22f92 commit 8209818
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 10 deletions.
4 changes: 3 additions & 1 deletion NTypewriter.Runtime/Rendering/TemplateRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
using System.Threading.Tasks;
using NTypewriter.CodeModel;
using NTypewriter.Editor.Config;
using NTypewriter.Ports;
using NTypewriter.Runtime.Rendering.Internals;
using NTypewriter.Runtime.Scripting;

namespace NTypewriter.Runtime.Rendering
{
Expand Down Expand Up @@ -38,7 +40,7 @@ public async Task<IEnumerable<RenderingResult>> RenderAsync(string templateFileP
dataModels[VariableNames.Config] = configAdapter;
dataModels[VariableNames.Env] = environmentVariables ?? new EnvironmentVariables();

var result = await NTypeWriter.Render(template, dataModels, configuration, new ExternalOutputAdapter(output));
var result = await NTypeWriter.Render(template, dataModels, configuration, new ExternalOutputAdapter(output), new ExpressionCompiler());

output.Info("Used configuration : " + editorConfig.ToString());

Expand Down
15 changes: 15 additions & 0 deletions NTypewriter.Runtime/Scripting/ExpressionCompiler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Text;
using NTypewriter.Ports;

namespace NTypewriter.Runtime.Scripting
{
internal class ExpressionCompiler : IExpressionCompiler
{
public Func<object, bool> CompilePredicate(string predicate)
{
return x => true;
}
}
}
18 changes: 17 additions & 1 deletion NTypewriter/Internals/BuiltinFunctionsScriptObject.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using NTypewriter.Internals.Functions;
using Scriban;
using Scriban.Functions;
using Scriban.Runtime;

Expand Down Expand Up @@ -30,7 +31,22 @@ private BuiltinFunctionsScriptObject()
this["Symbols"] = CreateScriptObject(typeof(global::NTypewriter.CodeModel.Functions.SymbolsFunctions));
this["Debug"] = CreateScriptObject(typeof(global::NTypewriter.Internals.Functions.DebugFunctions));
this.Import(typeof(SaveFunction), renamer: MemberRenamer);
}
this.Import(typeof(LINQFunctions), renamer: MemberRenamer);
}

public override bool TryGetValue(TemplateContext context, Scriban.Parsing.SourceSpan span, string member, out object value)
{
return base.TryGetValue(context, span, member, out value);
}

public override bool TrySetValue(TemplateContext context, Scriban.Parsing.SourceSpan span, string member, object value, bool readOnly)
{
return base.TrySetValue(context, span, member, value, readOnly);
}





private ScriptObject CreateScriptObject(params Type[] types)
{
Expand Down
17 changes: 17 additions & 0 deletions NTypewriter/Internals/Functions/LINQFunctions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NTypewriter.Internals.Functions
{
internal class LINQFunctions
{
public static IEnumerable<object> Where(MainTemplateContext context, IEnumerable<object> source, string predicate)
{
var func = context.CompileExpression(predicate);
return source.Where(func);
}
}
}
44 changes: 42 additions & 2 deletions NTypewriter/Internals/MainTemplateContext.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using NTypewriter.Ports;
using Scriban;
using Scriban.Syntax;

namespace NTypewriter.Internals
{
internal sealed class MainTemplateContext : TemplateContext
{
private readonly List<RenderedItem> renderedItems = new List<RenderedItem>();
private readonly IExternalOutput externalOutput;
private readonly IExpressionCompiler expressionCompiler;

public MainTemplateContext(DataScriptObject dataScriptObject, CustomFunctionsScriptObject customScriptObject, IExternalOutput externalOutput) : base(BuiltinFunctionsScriptObject.Singleton)

public MainTemplateContext(DataScriptObject dataScriptObject, CustomFunctionsScriptObject customScriptObject, IExternalOutput externalOutput, IExpressionCompiler expressionCompiler) : base(BuiltinFunctionsScriptObject.Singleton)
{
LoopLimit = 66_666;
MemberRenamer = member => member.Name;
Expand All @@ -19,6 +25,21 @@ public MainTemplateContext(DataScriptObject dataScriptObject, CustomFunctionsScr
PushGlobal(customScriptObject);
PushGlobal(dataScriptObject);
this.externalOutput = externalOutput;
this.expressionCompiler = expressionCompiler;

TryGetMember = TryGetMemberImp;
}


private bool TryGetMemberImp(TemplateContext context, Scriban.Parsing.SourceSpan span, object target, string member, out object value)
{
if (target is IEnumerable<object> symbold)
{

}

value = null;
return false;
}


Expand All @@ -30,10 +51,29 @@ public void WriteOnExternalOutput(string text)
{
externalOutput?.Write(text);
}
public Func<object, bool> CompileExpression(string predicate)
{
if (expressionCompiler == null)
{
throw new Exception("Expression compiler is unavailable");
}
return expressionCompiler.CompilePredicate(predicate);
}


public List<RenderedItem> GetRenderedItems()
{
return renderedItems;
}


public override object Evaluate(ScriptNode scriptNode, bool aliasReturnedFunction)
{
return base.Evaluate(scriptNode, aliasReturnedFunction);
}
public override ValueTask<object> EvaluateAsync(ScriptNode scriptNode, bool aliasReturnedFunction)
{
return base.EvaluateAsync(scriptNode, aliasReturnedFunction);
}
}
}
10 changes: 5 additions & 5 deletions NTypewriter/NTypeWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ namespace NTypewriter
{
public class NTypeWriter
{
public static async Task<Result> Render(string template, object dataModel, Configuration configuration = null, IExternalOutput externalOutput = null)
public static async Task<Result> Render(string template, object dataModel, Configuration configuration = null, IExternalOutput externalOutput = null, IExpressionCompiler expressionCompiler = null)
{
return await Render(template, new Dictionary<string, object>() { [VariableNames.Data] = dataModel }, configuration, externalOutput);
return await Render(template, new Dictionary<string, object>() { [VariableNames.Data] = dataModel }, configuration, externalOutput, expressionCompiler);
}


public static async Task<Result> Render(string template, Dictionary<string, object> dataModels, Configuration configuration = null, IExternalOutput externalOutput = null)
public static async Task<Result> Render(string template, Dictionary<string, object> dataModels, Configuration configuration = null, IExternalOutput externalOutput = null, IExpressionCompiler expressionCompiler = null)
{
var result = new Result();
var scribanTemplate = Template.Parse(template);
Expand All @@ -29,11 +29,11 @@ public static async Task<Result> Render(string template, Dictionary<string, obje

var dataScriptObject = new DataScriptObject(dataModels);
var userScriptObject = new CustomFunctionsScriptObject(configuration?.GetTypesWithCustomFuntions());
var context = new MainTemplateContext(dataScriptObject, userScriptObject, externalOutput);
var context = new MainTemplateContext(dataScriptObject, userScriptObject, externalOutput, expressionCompiler);

try
{
result.SetOutput(await scribanTemplate.RenderAsync(context));
result.SetOutput(scribanTemplate.Render(context));
}
catch (ScriptRuntimeException exception)
{
Expand Down
11 changes: 11 additions & 0 deletions NTypewriter/Ports/IExpressionCompiler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace NTypewriter.Ports
{
public interface IExpressionCompiler
{
Func<object, bool> CompilePredicate(string predicate);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{ capture output
for class in data.Classes | Array.Sort "FullName"
for class in data.Classes | Where "x => true" | Array.Sort "FullName"
class.FullName | String.Append "\r\n"
end
end
Expand Down

0 comments on commit 8209818

Please sign in to comment.