77
88namespace 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