Skip to content

Commit 66c5b39

Browse files
Florian KroenertFlorian Kroenert
authored andcommitted
Allow for customizing function handlers
1 parent 8f260b4 commit 66c5b39

4 files changed

Lines changed: 53 additions & 8 deletions

File tree

.devcontainer/Dockerfile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Base image: Node 18 + .NET SDK 6
2+
FROM mcr.microsoft.com/devcontainers/dotnet:0-6.0
3+
4+
# Install Node.js 18 (overwrite if not already present)
5+
RUN curl -fsSL https://deb.nodesource.com/setup_18.x | bash - \
6+
&& apt-get update && apt-get install -y nodejs
7+
8+
# Set user to vscode (standard user in devcontainers)
9+
USER vscode

.devcontainer/devcontainer.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "Xrm-Templating-Language",
3+
"build": {
4+
"dockerfile": "Dockerfile"
5+
},
6+
"settings": {
7+
"terminal.integrated.defaultProfile.linux": "bash"
8+
},
9+
"customizations": {
10+
"vscode": {
11+
"extensions": [
12+
"ms-dotnettools.csharp",
13+
"dbaeumer.vscode-eslint",
14+
"esbenp.prettier-vscode",
15+
"ms-dotnettools.csdevkit",
16+
"microsoft-isvexptools.powerplatform-vscode"
17+
]
18+
}
19+
},
20+
"forwardPorts": [5000, 5001, 4280],
21+
"remoteUser": "vscode"
22+
}

src/lib/Xrm.Oss.XTL.Interpreter/InterpreterConfig.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Runtime.Serialization.Json;
77
using System.Text;
88
using System.Threading.Tasks;
9+
using static Xrm.Oss.XTL.Interpreter.XTLInterpreter;
910

1011
namespace Xrm.Oss.XTL.Interpreter
1112
{
@@ -39,6 +40,8 @@ public class InterpreterConfig : IConfig
3940
[DataMember(Name = "snippetConfig")]
4041
public SnippetConfig SnippetConfig { get; set; }
4142

43+
public Dictionary<string, FunctionHandler> CustomHandlers { get; set; }
44+
4245
public static InterpreterConfig Parse(string json)
4346
{
4447
if (string.IsNullOrEmpty(json))

src/lib/Xrm.Oss.XTL.Interpreter/XTLInterpreter.cs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
namespace Xrm.Oss.XTL.Interpreter
99
{
10+
public delegate ValueExpression FunctionHandler(Entity primary, IOrganizationService service, ITracingService tracing, InterpreterConfig interpreterConfig, List<ValueExpression> parameters);
11+
1012
public class XTLInterpreter
1113
{
1214
private int _position;
@@ -19,8 +21,6 @@ public class XTLInterpreter
1921
private ITracingService _tracing;
2022
private InterpreterConfig _interpreterConfig;
2123

22-
public delegate ValueExpression FunctionHandler(Entity primary, IOrganizationService service, ITracingService tracing, InterpreterConfig interpreterConfig, List<ValueExpression> parameters);
23-
2424
private Dictionary<string, FunctionHandler> _handlers = new Dictionary<string, FunctionHandler>
2525
{
2626
{ "And", FunctionHandlers.And },
@@ -255,7 +255,7 @@ private List<ValueExpression> Expression(char[] terminators, Dictionary<string,
255255

256256
private ValueExpression ApplyExpression (string name, List<ValueExpression> parameters, Dictionary<string, ValueExpression> formulaArgs = null)
257257
{
258-
if (!_handlers.ContainsKey(name)) {
258+
if (!_handlers.ContainsKey(name) && !(_interpreterConfig?.CustomHandlers?.ContainsKey(name) ?? false)) {
259259
throw new InvalidPluginExecutionException($"Function {name} is not known!");
260260
}
261261

@@ -267,11 +267,22 @@ private ValueExpression ApplyExpression (string name, List<ValueExpression> para
267267

268268
var lazyExecution = new Lazy<ValueExpression>(() =>
269269
{
270-
_tracing.Trace($"Processing handler {name}");
271-
var result = _handlers[name](_primary, _service, _tracing, _interpreterConfig, parameters);
272-
_tracing.Trace($"Successfully processed handler {name}");
270+
if (_interpreterConfig?.CustomHandlers?.ContainsKey(name) ?? false)
271+
{
272+
_tracing.Trace($"Processing custom handler {name}");
273+
var result = _handlers[name](_primary, _service, _tracing, _interpreterConfig, parameters);
274+
_tracing.Trace($"Successfully processed custom handler {name}");
275+
276+
return result;
277+
}
278+
else
279+
{
280+
_tracing.Trace($"Processing default handler {name}");
281+
var result = _handlers[name](_primary, _service, _tracing, _interpreterConfig, parameters);
282+
_tracing.Trace($"Successfully processed default handler {name}");
273283

274-
return result;
284+
return result;
285+
}
275286
});
276287

277288
return new ValueExpression(lazyExecution);
@@ -312,7 +323,7 @@ private ValueExpression Formula(Dictionary<string, ValueExpression> args)
312323
Match(')');
313324

314325
var usedReservedWords = variableNames
315-
.Where(n => new List<string> { "true", "false", "null" }.Concat(_handlers.Keys).Contains(n))
326+
.Where(n => new List<string> { "true", "false", "null" }.Concat(_handlers.Keys).Concat(_interpreterConfig?.CustomHandlers?.Keys ?? new List<string>()).Contains(n))
316327
.ToList();
317328

318329
if (usedReservedWords.Count > 0)

0 commit comments

Comments
 (0)