Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

LuExpressionConverterVisitor.cs 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using Luticate2.Auth.Utils.Dbo;
  6. using Luticate2.Auth.Utils.Dbo.Result;
  7. using Luticate2.Auth.Utils.Exceptions;
  8. using Luticate2.Auth.Utils.Interfaces;
  9. namespace Luticate2.Auth.Utils.Business.ExpressionConverter
  10. {
  11. public interface ILuExpressionConverterVisitorOptions
  12. {
  13. IDictionary<ParameterExpression, Expression> Parameters { get; }
  14. ILuConvertersTypeConverter TypeConverter { get; }
  15. ILuObjectConverterDescriptorOptions DescriptorOptions { get; }
  16. ILuExpressionParamReplaceVisitorOptions VisitorOptions { get; }
  17. }
  18. public class LuExpressionConverterVisitor : ExpressionVisitor
  19. {
  20. public ILuExpressionConverterVisitorOptions Options { get; }
  21. protected IServiceProvider ServiceProvider { get; }
  22. public LuExpressionConverterVisitor(ILuExpressionConverterVisitorOptions options, IServiceProvider serviceProvider)
  23. {
  24. Options = options;
  25. ServiceProvider = serviceProvider;
  26. }
  27. protected LuResult<Stack<Expression>> StackSimpleExpression(Expression expression, Stack<Expression> stack)
  28. {
  29. stack.Push(expression);
  30. if (expression is MemberExpression memberExpression)
  31. {
  32. if (memberExpression.Expression != null)
  33. {
  34. var result = StackSimpleExpression(memberExpression.Expression, stack);
  35. return result;
  36. }
  37. return LuResult<Stack<Expression>>.Ok(stack);
  38. }
  39. else if (expression is MethodCallExpression methodCallExpression)
  40. {
  41. if (methodCallExpression.Object != null)
  42. {
  43. var result = StackSimpleExpression(methodCallExpression.Object, stack);
  44. return result;
  45. }
  46. return LuResult<Stack<Expression>>.Ok(stack);
  47. }
  48. else
  49. {
  50. return LuResult<Stack<Expression>>.Ok(stack);
  51. }
  52. }
  53. protected ILuObjectConverterDescriptor GetConverterDescriptor(Type typeFrom, Type typeTo)
  54. {
  55. var type = typeof(ILuObjectConverterDescriptor<,>);
  56. var gtype = type.MakeGenericType(typeFrom, typeTo);
  57. var descriptor = (ILuObjectConverterDescriptor) ServiceProvider.GetService(gtype);
  58. if (descriptor == null && typeFrom == typeTo)
  59. {
  60. descriptor = (ILuObjectConverterDescriptor) ServiceProvider.GetService(typeof(ILuObjectConverterDescriptorIdentity));
  61. }
  62. return descriptor;
  63. }
  64. protected LuResult<Expression> ConvertMemberExpression(MemberExpression memberExpression, Expression newExpression, Type typeTo)
  65. {
  66. var typeFrom = memberExpression.Expression == null ? memberExpression.Member.DeclaringType : memberExpression.Expression.Type;
  67. var descriptor = GetConverterDescriptor(typeFrom, typeTo);
  68. if (descriptor == null)
  69. {
  70. return LuResult<Expression>.Error(LuStatus.InternalError.ToInt(),
  71. $"Could not find converter descriptor for {typeFrom} => {typeTo}");
  72. }
  73. var valueExpression = descriptor.GetMemberValueExpression(memberExpression.Member, Options.DescriptorOptions);
  74. if (valueExpression == null)
  75. {
  76. return LuResult<Expression>.Error(LuStatus.InternalError.ToInt(),
  77. $"Could not find converter descriptor lambda for {typeFrom} => {typeTo} => {memberExpression.Member}");
  78. }
  79. if (valueExpression.Parameters[0].Type != typeTo)
  80. {
  81. return LuResult<Expression>.Error(LuStatus.InternalError.ToInt(),
  82. $"Invalid conversion lambda for {typeFrom} => {typeTo}");
  83. }
  84. if (newExpression != null)
  85. {
  86. Options.Parameters.Add(valueExpression.Parameters[0], newExpression);
  87. }
  88. var visitor = new LuExpressionParamReplaceVisitor(Options.VisitorOptions);
  89. newExpression = visitor.Visit(valueExpression.Body);
  90. if (newExpression != null)
  91. {
  92. Options.Parameters.Remove(valueExpression.Parameters[0]);
  93. }
  94. return LuResult<Expression>.Ok(newExpression);
  95. }
  96. protected LuResult<Expression> ConvertMethodCallExpression(MethodCallExpression methodCallExpression, Expression newExpression, Type typeTo)
  97. {
  98. var typeFrom = methodCallExpression.Object == null ? methodCallExpression.Method.DeclaringType : methodCallExpression.Object.Type;
  99. var descriptor = GetConverterDescriptor(typeFrom, typeTo);
  100. if (descriptor == null)
  101. {
  102. return LuResult<Expression>.Error(LuStatus.InternalError.ToInt(),
  103. $"Could not find converter descriptor for {typeFrom} => {typeTo}");
  104. }
  105. var valueExpression = descriptor.GetMethodValueExpression(methodCallExpression.Method, Options.DescriptorOptions);
  106. if (valueExpression == null)
  107. {
  108. return LuResult<Expression>.Error(LuStatus.InternalError.ToInt(),
  109. $"Could not find converter descriptor lambda for {typeFrom} => {typeTo} => {methodCallExpression.Method}");
  110. }
  111. if (valueExpression.Parameters.Count != methodCallExpression.Arguments.Count + 1)
  112. {
  113. return LuResult<Expression>.Error(LuStatus.InternalError.ToInt(),
  114. $"Converter descriptor lambda has incorrect number of arguments for {typeFrom} => {typeTo} => {methodCallExpression.Method}");
  115. }
  116. if (valueExpression.Parameters[0].Type != typeTo)
  117. {
  118. return LuResult<Expression>.Error(LuStatus.InternalError.ToInt(),
  119. $"Invalid conversion lambda for {typeFrom} => {typeTo}");
  120. }
  121. var visitorConverter = new LuExpressionConverterVisitor(Options, ServiceProvider);
  122. for (var i = 0; i < methodCallExpression.Arguments.Count; ++i)
  123. {
  124. var convertedArgument = visitorConverter.Visit(methodCallExpression.Arguments[i]);
  125. Options.Parameters.Add(valueExpression.Parameters[i + 1], convertedArgument);
  126. }
  127. Options.Parameters.Add(valueExpression.Parameters[0], newExpression);
  128. var visitor = new LuExpressionParamReplaceVisitor(Options.VisitorOptions);
  129. newExpression = visitor.Visit(valueExpression.Body);
  130. Options.Parameters.Remove(valueExpression.Parameters[0]);
  131. return LuResult<Expression>.Ok(newExpression);
  132. }
  133. protected LuResult<Expression> ReplaceExpression(Expression expression)
  134. {
  135. var stackResult = StackSimpleExpression(expression, new Stack<Expression>());
  136. if (!stackResult)
  137. {
  138. return stackResult.To<Expression>();
  139. }
  140. Expression newExpression;
  141. var currentExp = stackResult.Data.Pop();
  142. if (currentExp is ParameterExpression parameterExpression)
  143. {
  144. if (!Options.Parameters.ContainsKey(parameterExpression))
  145. {
  146. return LuResult<Expression>.Error(LuStatus.InternalError.ToInt(),
  147. $"Could not find a conversion for parameter {parameterExpression}");
  148. }
  149. newExpression = Options.Parameters[parameterExpression];
  150. }
  151. else if (currentExp is MemberExpression memberExpression)
  152. {
  153. var typeFrom = memberExpression.Member.DeclaringType;
  154. var typeTo = Options.TypeConverter.ConvertType(typeFrom);
  155. var convertResult = ConvertMemberExpression(memberExpression, null, typeTo);
  156. if (!convertResult)
  157. {
  158. return convertResult;
  159. }
  160. newExpression = convertResult.Data;
  161. }
  162. else if (currentExp is MethodCallExpression methodCallExpression)
  163. {
  164. var typeFrom = methodCallExpression.Method.DeclaringType;
  165. var typeTo = Options.TypeConverter.ConvertType(typeFrom);
  166. var convertResult = ConvertMethodCallExpression(methodCallExpression, null, typeTo);
  167. if (!convertResult)
  168. {
  169. return convertResult;
  170. }
  171. newExpression = convertResult.Data;
  172. }
  173. else
  174. {
  175. var visitor = new LuExpressionConverterVisitor(Options, ServiceProvider);
  176. newExpression = visitor.Visit(currentExp);
  177. }
  178. if (newExpression == null)
  179. {
  180. return LuResult<Expression>.Error(LuStatus.InternalError.ToInt(),
  181. $"Invalid simple expression first member: {currentExp}", "");
  182. }
  183. while (stackResult.Data.Any())
  184. {
  185. currentExp = stackResult.Data.Pop();
  186. if (currentExp is MemberExpression memberExpression)
  187. {
  188. var typeTo = newExpression.Type;
  189. var convertResult = ConvertMemberExpression(memberExpression, newExpression, typeTo);
  190. if (!convertResult)
  191. {
  192. return convertResult;
  193. }
  194. newExpression = convertResult.Data;
  195. }
  196. else if (currentExp is MethodCallExpression methodCallExpression)
  197. {
  198. var typeTo = newExpression.Type;
  199. var convertResult = ConvertMethodCallExpression(methodCallExpression, newExpression, typeTo);
  200. if (!convertResult)
  201. {
  202. return convertResult;
  203. }
  204. newExpression = convertResult.Data;
  205. }
  206. else
  207. {
  208. return LuResult<Expression>.Error(LuStatus.InternalError.ToInt(),
  209. $"Unknown expression type (should not happen) {currentExp}");
  210. }
  211. }
  212. return LuResult<Expression>.Ok(newExpression);
  213. }
  214. protected override Expression VisitMember(MemberExpression node)
  215. {
  216. return ReplaceExpression(node).ThrowIfNotSuccess().Data;
  217. }
  218. protected override Expression VisitMethodCall(MethodCallExpression node)
  219. {
  220. return ReplaceExpression(node).ThrowIfNotSuccess().Data;
  221. }
  222. protected override Expression VisitParameter(ParameterExpression node)
  223. {
  224. if (!Options.Parameters.ContainsKey(node))
  225. {
  226. LuResult<Expression>.Error(LuStatus.InternalError.ToInt(),
  227. $"Could not find a conversion for parameter {node}").Throw();
  228. }
  229. return Options.Parameters[node];
  230. }
  231. protected override Expression VisitLambda<T>(Expression<T> node)
  232. {
  233. var convertedParams = new List<ParameterExpression>();
  234. foreach (var parameter in node.Parameters)
  235. {
  236. var convertedParam = Expression.Parameter(Options.TypeConverter.ConvertType(parameter.Type));
  237. Options.Parameters.Add(parameter, convertedParam);
  238. convertedParams.Add(convertedParam);
  239. }
  240. var convertedBody = Visit(node.Body);
  241. foreach (var parameter in node.Parameters)
  242. {
  243. Options.Parameters.Remove(parameter);
  244. }
  245. var convertedLambda = Expression.Lambda(convertedBody, convertedParams);
  246. return convertedLambda;
  247. }
  248. }
  249. }