123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq.Expressions;
- using Luticate2.Auth.Business.Fields.DMEC;
- using Luticate2.Auth.Dbo;
- using Luticate2.Auth.Dbo.Result;
- using Luticate2.Auth.Interfaces;
- using Microsoft.EntityFrameworkCore.Internal;
-
- namespace Luticate2.Auth.Business.Fields
- {
- public class LuExpressionConverter<TTypeFrom, TTypeTo> : ILuExpressionConverter<TTypeFrom, TTypeTo>
- {
- protected IServiceProvider ServiceProvider { get; }
-
- public LuExpressionConverter(IServiceProvider serviceProvider)
- {
- ServiceProvider = serviceProvider;
- }
-
- public virtual LuResult<Expression> ConvertExpression(Stack<Expression> expressions, Expression baseExpression, LuExpressionConverterOptions options)
- {
- if (!expressions.Any())
- {
- return LuResult<Expression>.Ok(baseExpression);
- }
-
- var expression = expressions.Peek();
- if (expression is ParameterExpression parameterExpression)
- {
- if (parameterExpression.Type != typeof(TTypeFrom))
- {
- return LuResult<Expression>.Error(LuStatus.InputError.ToInt(), "Bad parameter expression type");
- }
-
- expressions.Pop();
- return ConvertExpression(expressions, options.Parameter, options);
- }
- else if (expression is MemberExpression memberExpression)
- {
- if (memberExpression.Member.DeclaringType != typeof(TTypeFrom))
- {
- return LuResult<Expression>.Error(LuStatus.InputError.ToInt(), "Bad member expression type");
- }
- }
- else if (expression is MethodCallExpression methodCallExpression)
- {
- if (methodCallExpression.Object != null && methodCallExpression.Object.Type != typeof(TTypeFrom))
- {
- return LuResult<Expression>.Error(LuStatus.InputError.ToInt(), "Bad method call expression type");
- }
- }
-
- var convertResult = ConvertInternal(expressions, baseExpression, options);
- return convertResult;
- }
-
- protected LuResult<Expression> ConvertOther(Type typeFrom, Stack<Expression> expressions, Expression baseExpression, LuExpressionConverterOptions options)
- {
- var type = typeof(ILuExpressionConverter<>);
- var gtype = type.MakeGenericType(typeFrom);
- var converter = (ILuExpressionConverter) ServiceProvider.GetService(gtype);
- if (converter == null)
- {
- return LuResult<Expression>.Error(LuStatus.InternalError.ToInt(), $"Could not get service: {gtype}>");
- }
-
- return converter.ConvertExpression(expressions, baseExpression, options);
- }
-
- protected bool MemberMatch<TTypeFromProperty>(Expression<Func<TTypeFrom, TTypeFromProperty>> member, MemberExpression memberExpression)
- {
- return LuExpressionUtils.GetSingleMemberFromExpression(member) == memberExpression.Member;
- }
-
- protected LuResult<Expression> MakeMemberAccess<TTypeToProperty>(Expression<Func<TTypeTo, TTypeToProperty>> expression, Stack<Expression> expressions, Expression baseExpression, LuExpressionConverterOptions options)
- {
- var exp = expressions.Pop();
- var memberInfo = LuExpressionUtils.GetSingleMemberFromExpression(expression);
- var newExpression = Expression.MakeMemberAccess(baseExpression, memberInfo);
- var convertResult = ConvertOther(exp.Type, expressions, newExpression, options);
- return convertResult;
- }
-
- // protected LuResult<Expression> MakeMethodCall<TTypeToProperty>(Expression<Func<TTypeTo, TTypeToProperty>> expression, Stack<Expression> expressions, Expression baseExpression, LuExpressionConverterOptions options)
- // {
- // var exp = expressions.Pop();
- // }
-
- protected virtual LuResult<Expression> ConvertInternal(Stack<Expression> expressions, Expression baseExpression, LuExpressionConverterOptions options)
- {
- return LuResult<Expression>.Error(LuStatus.InternalError.ToInt(), "Not supported expression");
- }
- }
- }
|