1- using ExpressionEngine . Functions . Base ;
1+ using System ;
2+ using ExpressionEngine . Functions . Base ;
3+ using ExpressionEngine . Functions . CustomException ;
24using ExpressionEngine . Functions . Implementations . CollectionFunctions ;
35using ExpressionEngine . Functions . Implementations . ConversionFunctions ;
46using ExpressionEngine . Functions . Implementations . LogicalComparisonFunctions ;
810
911namespace ExpressionEngine
1012{
13+ /// <summary>
14+ /// Selection of extension methods
15+ /// </summary>
1116 public static class FlowRunnerDependencyExtension
1217 {
18+ /// <summary>
19+ /// Add necessary dependencies inorder to use expression engine.
20+ /// </summary>
21+ /// <param name="services"><see cref="IServiceCollection"/> to add dependencies</param>
1322 public static void AddExpressionEngine ( this IServiceCollection services )
1423 {
1524 services . AddScoped < IExpressionEngine , ExpressionEngine > ( ) ;
@@ -21,81 +30,138 @@ public static void AddExpressionEngine(this IServiceCollection services)
2130 AddLogicalComparisonFunctions ( services ) ;
2231 AddMathFunctions ( services ) ;
2332
24- services . AddTransient < IFunction , LengthFunction > ( ) ;
25- services . AddTransient < IFunction , GreaterFunction > ( ) ;
33+ services . RegisterTransientFunctionAlias < LengthFunction > ( "length" ) ;
34+ services . RegisterTransientFunctionAlias < GreaterFunction > ( "greater" ) ;
35+ }
36+
37+ /// <summary>
38+ /// Register function to be used in expression, function implementation must implement <see cref="IFunction"/>.
39+ /// </summary>
40+ /// <param name="services">Services which to add function metadata</param>
41+ /// <param name="functionName">name of function used to invoke it</param>
42+ /// <typeparam name="T">Function implementation</typeparam>
43+ public static void RegisterTransientFunctionAlias < T > ( this IServiceCollection services , string functionName )
44+ where T : class , IFunction
45+ {
46+ services . AddTransient < T > ( ) ;
47+ services . AddSingleton ( new FunctionMetadata ( typeof ( T ) , functionName ) ) ;
48+ }
49+
50+ /// <summary>
51+ /// Register function to be used in expression, function implementation must implement <see cref="IFunction"/>.
52+ /// </summary>
53+ /// <param name="services">Services which to add function metadata</param>
54+ /// <param name="functionName">name of function used to invoke it</param>
55+ /// <typeparam name="T">Function implementation</typeparam>
56+ public static void RegisterScopedFunctionAlias < T > ( this IServiceCollection services , string functionName )
57+ where T : class , IFunction
58+ {
59+ services . AddScoped < T > ( ) ;
60+ services . AddSingleton ( new FunctionMetadata ( typeof ( T ) , functionName ) ) ;
61+ }
62+
63+ /// <summary>
64+ /// Register function to be used in expression, function implementation must implement <see cref="IFunction"/>.
65+ /// </summary>
66+ /// <param name="services">Services which to add function metadata</param>
67+ /// <param name="functionName">name of function used to invoke it</param>
68+ /// <param name="implementationFactory"></param>
69+ /// <typeparam name="T">Function implementation</typeparam>
70+ public static void RegisterScopedFunctionAlias < T > ( this IServiceCollection services , string functionName ,
71+ Func < IServiceProvider , T > implementationFactory )
72+ where T : class , IFunction
73+ {
74+ services . AddScoped ( implementationFactory ) ;
75+ services . AddSingleton ( new FunctionMetadata ( typeof ( T ) , functionName ) ) ;
76+ }
77+
78+ /// <summary>
79+ /// Added FunctionDefinition to service collection.
80+ /// </summary>
81+ /// <param name="services"></param>
82+ /// <param name="fromFunctionName">The name of the function, without function parenthesis</param>
83+ /// <param name="toExpression">The full expression which is inserted</param>
84+ public static void AddFunctionDefinition ( this IServiceCollection services , string fromFunctionName , string toExpression )
85+ {
86+ if ( fromFunctionName . EndsWith ( "()" ) )
87+ {
88+ throw new ArgumentError ( $ "{ nameof ( fromFunctionName ) } cannot end in ()") ;
89+ }
90+
91+ services . AddSingleton < IFunctionDefinition > ( new FunctionDefinition { From = fromFunctionName + "()" , To = toExpression } ) ;
2692 }
2793
2894 private static void AddStringFunctions ( IServiceCollection services )
2995 {
30- services . AddTransient < IFunction , ConcatFunction > ( ) ;
31- services . AddTransient < IFunction , EndsWithFunction > ( ) ;
32- services . AddTransient < IFunction , FormatNumberFunction > ( ) ;
33- services . AddTransient < IFunction , GuidFunction > ( ) ;
34- services . AddTransient < IFunction , IndexOfFunction > ( ) ;
35- services . AddTransient < IFunction , LastIndexOfFunction > ( ) ;
36- services . AddTransient < IFunction , LengthFunction > ( ) ;
37- services . AddTransient < IFunction , ReplaceFunction > ( ) ;
38- services . AddTransient < IFunction , SplitFunction > ( ) ;
39- services . AddTransient < IFunction , StartsWithFunction > ( ) ;
40- services . AddTransient < IFunction , SubstringFunction > ( ) ;
41- services . AddTransient < IFunction , ToLowerFunction > ( ) ;
42- services . AddTransient < IFunction , ToUpperFunction > ( ) ;
43- services . AddTransient < IFunction , TrimFunction > ( ) ;
96+ services . RegisterTransientFunctionAlias < ConcatFunction > ( "concat" ) ;
97+ services . RegisterTransientFunctionAlias < EndsWithFunction > ( "endsWith" ) ;
98+ services . RegisterTransientFunctionAlias < FormatNumberFunction > ( "formatNumber" ) ;
99+ services . RegisterTransientFunctionAlias < GuidFunction > ( "guid" ) ;
100+ services . RegisterTransientFunctionAlias < IndexOfFunction > ( "indexOf" ) ;
101+ services . RegisterTransientFunctionAlias < LastIndexOfFunction > ( "lastIndexOf" ) ;
102+ services . RegisterTransientFunctionAlias < LengthFunction > ( "length" ) ;
103+ services . RegisterTransientFunctionAlias < ReplaceFunction > ( "replace" ) ;
104+ services . RegisterTransientFunctionAlias < SplitFunction > ( "split" ) ;
105+ services . RegisterTransientFunctionAlias < StartsWithFunction > ( "startsWith" ) ;
106+ services . RegisterTransientFunctionAlias < SubstringFunction > ( "substring" ) ;
107+ services . RegisterTransientFunctionAlias < ToLowerFunction > ( "toLower" ) ;
108+ services . RegisterTransientFunctionAlias < ToUpperFunction > ( "toUpper" ) ;
109+ services . RegisterTransientFunctionAlias < TrimFunction > ( "trim" ) ;
44110 }
45111
46112 private static void AddCollectionFunction ( IServiceCollection services )
47113 {
48- services . AddTransient < IFunction , ContainsFunction > ( ) ;
49- services . AddTransient < IFunction , EmptyFunction > ( ) ;
50- services . AddTransient < IFunction , FirstFunction > ( ) ;
51- services . AddTransient < IFunction , InterSectionFunction > ( ) ;
52- services . AddTransient < IFunction , JoinFunction > ( ) ;
53- services . AddTransient < IFunction , LastFunction > ( ) ;
54- services . AddTransient < IFunction , LengthFunction > ( ) ;
55- services . AddTransient < IFunction , SkipFunction > ( ) ;
56- services . AddTransient < IFunction , TakeFunction > ( ) ;
57- services . AddTransient < IFunction , UnionFunction > ( ) ;
114+ services . RegisterTransientFunctionAlias < ContainsFunction > ( "contains" ) ;
115+ services . RegisterTransientFunctionAlias < EmptyFunction > ( "empty" ) ;
116+ services . RegisterTransientFunctionAlias < FirstFunction > ( "first" ) ;
117+ services . RegisterTransientFunctionAlias < InterSectionFunction > ( "intersection" ) ;
118+ services . RegisterTransientFunctionAlias < JoinFunction > ( "join" ) ;
119+ services . RegisterTransientFunctionAlias < LastFunction > ( "last" ) ;
120+ services . RegisterTransientFunctionAlias < LengthFunction > ( "length" ) ;
121+ services . RegisterTransientFunctionAlias < SkipFunction > ( "skip" ) ;
122+ services . RegisterTransientFunctionAlias < TakeFunction > ( "take" ) ;
123+ services . RegisterTransientFunctionAlias < UnionFunction > ( "union" ) ;
58124 }
59125
60126 private static void AddConversionFunction ( IServiceCollection services )
61127 {
62- services . AddTransient < IFunction , ArrayFunction > ( ) ;
63- services . AddTransient < IFunction , Base64Function > ( ) ;
64- services . AddTransient < IFunction , Base64ToBinaryFunction > ( ) ;
65- services . AddTransient < IFunction , Base64ToStringFunction > ( ) ;
66- services . AddTransient < IFunction , BinaryFunction > ( ) ;
67- services . AddTransient < IFunction , BoolFunction > ( ) ;
68- services . AddTransient < IFunction , CreateArrayFunction > ( ) ;
69- services . AddTransient < IFunction , DataUriFunction > ( ) ;
70- services . AddTransient < IFunction , DataUriToBinaryFunction > ( ) ;
71- services . AddTransient < IFunction , FloatFunction > ( ) ;
72- services . AddTransient < IFunction , IntFunction > ( ) ;
128+ services . RegisterTransientFunctionAlias < ArrayFunction > ( "array" ) ;
129+ services . RegisterTransientFunctionAlias < Base64Function > ( "base64" ) ;
130+ services . RegisterTransientFunctionAlias < Base64ToBinaryFunction > ( "base64ToBinary" ) ;
131+ services . RegisterTransientFunctionAlias < Base64ToStringFunction > ( "base64ToString" ) ;
132+ services . RegisterTransientFunctionAlias < BinaryFunction > ( "binary" ) ;
133+ services . RegisterTransientFunctionAlias < BoolFunction > ( "bool" ) ;
134+ services . RegisterTransientFunctionAlias < CreateArrayFunction > ( "createArray" ) ;
135+ services . RegisterTransientFunctionAlias < DataUriFunction > ( "dataUri" ) ;
136+ services . RegisterTransientFunctionAlias < DataUriToBinaryFunction > ( "dataUriToBinary" ) ;
137+ services . RegisterTransientFunctionAlias < FloatFunction > ( "float" ) ;
138+ services . RegisterTransientFunctionAlias < IntFunction > ( "int" ) ;
73139 }
74140
75141 private static void AddLogicalComparisonFunctions ( IServiceCollection services )
76142 {
77- services . AddTransient < IFunction , AndFunction > ( ) ;
78- services . AddTransient < IFunction , EqualFunction > ( ) ;
79- services . AddTransient < IFunction , GreaterFunction > ( ) ;
80- services . AddTransient < IFunction , GreaterOrEqualsFunction > ( ) ;
81- services . AddTransient < IFunction , IfFunction > ( ) ;
82- services . AddTransient < IFunction , LessFunction > ( ) ;
83- services . AddTransient < IFunction , LessOrEqualsFunction > ( ) ;
84- services . AddTransient < IFunction , NotFunction > ( ) ;
85- services . AddTransient < IFunction , OrFunction > ( ) ;
143+ services . RegisterTransientFunctionAlias < AndFunction > ( "and" ) ;
144+ services . RegisterTransientFunctionAlias < EqualFunction > ( "equal" ) ;
145+ services . RegisterTransientFunctionAlias < GreaterFunction > ( "greater" ) ;
146+ services . RegisterTransientFunctionAlias < GreaterOrEqualsFunction > ( "greaterOrEquals" ) ;
147+ services . RegisterTransientFunctionAlias < IfFunction > ( "if" ) ;
148+ services . RegisterTransientFunctionAlias < LessFunction > ( "less" ) ;
149+ services . RegisterTransientFunctionAlias < LessOrEqualsFunction > ( "lessOrEquals" ) ;
150+ services . RegisterTransientFunctionAlias < NotFunction > ( "not" ) ;
151+ services . RegisterTransientFunctionAlias < OrFunction > ( "or" ) ;
86152 }
87153
88154 private static void AddMathFunctions ( IServiceCollection services )
89155 {
90- services . AddTransient < IFunction , AddFunction > ( ) ;
91- services . AddTransient < IFunction , DivFunction > ( ) ;
92- services . AddTransient < IFunction , MaxFunction > ( ) ;
93- services . AddTransient < IFunction , MinFunction > ( ) ;
94- services . AddTransient < IFunction , ModFunction > ( ) ;
95- services . AddTransient < IFunction , MulFunction > ( ) ;
96- services . AddTransient < IFunction , RandFunction > ( ) ;
97- services . AddTransient < IFunction , RangeFunction > ( ) ;
98- services . AddTransient < IFunction , SubFunction > ( ) ;
156+ services . RegisterTransientFunctionAlias < AddFunction > ( "add" ) ;
157+ services . RegisterTransientFunctionAlias < DivFunction > ( "div" ) ;
158+ services . RegisterTransientFunctionAlias < MaxFunction > ( "max" ) ;
159+ services . RegisterTransientFunctionAlias < MinFunction > ( "min" ) ;
160+ services . RegisterTransientFunctionAlias < ModFunction > ( "mod" ) ;
161+ services . RegisterTransientFunctionAlias < MulFunction > ( "mul" ) ;
162+ services . RegisterTransientFunctionAlias < RandFunction > ( "rand" ) ;
163+ services . RegisterTransientFunctionAlias < RangeFunction > ( "range" ) ;
164+ services . RegisterTransientFunctionAlias < SubFunction > ( "sub" ) ;
99165 }
100166 }
101167}
0 commit comments