1+ using System ;
12using System . Collections . Generic ;
23using System . Linq . Expressions ;
34using FluentResults ;
@@ -18,21 +19,27 @@ public static Result<Expression> Evaluate(QueryExpression expression, ParameterE
1819
1920 var property = Expression . Property ( parameterExpression , propertyName ) ;
2021
21- ConstantExpression value = null ;
22+ ConstantExpression value ;
2223
2324 switch ( exp . Right )
2425 {
2526 case GuidLiteral literal :
2627 value = Expression . Constant ( literal . Value , property . Type ) ;
2728 break ;
2829 case IntegerLiteral literal :
29- value = Expression . Constant ( literal . Value , property . Type ) ;
30+ var integerConstant = GetIntegerExpressionConstant ( literal . Value , property . Type ) ;
31+ if ( integerConstant . IsFailed )
32+ {
33+ return Result . Fail ( integerConstant . Errors ) ;
34+ }
35+
36+ value = integerConstant . Value ;
3037 break ;
3138 case StringLiteral literal :
3239 value = Expression . Constant ( literal . Value , property . Type ) ;
3340 break ;
3441 default :
35- break ;
42+ return Result . Fail ( $ "Unsupported literal type: { exp . Right . GetType ( ) . Name } " ) ;
3643 }
3744
3845 switch ( exp . Operator )
@@ -47,6 +54,8 @@ public static Result<Expression> Evaluate(QueryExpression expression, ParameterE
4754 var method = identifier . Value . GetType ( ) . GetMethod ( "Contains" , new [ ] { value ? . Value . GetType ( ) } ) ;
4855
4956 return Expression . Call ( property , method , value ) ;
57+ default :
58+ return Result . Fail ( $ "Unsupported operator: { exp . Operator } ") ;
5059 }
5160 }
5261
@@ -75,4 +84,37 @@ public static Result<Expression> Evaluate(QueryExpression expression, ParameterE
7584
7685 return null ;
7786 }
87+
88+ private static Result < ConstantExpression > GetIntegerExpressionConstant ( int value , Type targetType )
89+ {
90+ try
91+ {
92+ // Fetch the underlying type if it's nullable.
93+ var underlyingType = Nullable . GetUnderlyingType ( targetType ) ;
94+ var type = underlyingType ?? targetType ;
95+
96+ object convertedValue = type switch
97+ {
98+ Type t when t == typeof ( int ) => value ,
99+ Type t when t == typeof ( long ) => Convert . ToInt64 ( value ) ,
100+ Type t when t == typeof ( short ) => Convert . ToInt16 ( value ) ,
101+ Type t when t == typeof ( byte ) => Convert . ToByte ( value ) ,
102+ Type t when t == typeof ( uint ) => Convert . ToUInt32 ( value ) ,
103+ Type t when t == typeof ( ulong ) => Convert . ToUInt64 ( value ) ,
104+ Type t when t == typeof ( ushort ) => Convert . ToUInt16 ( value ) ,
105+ Type t when t == typeof ( sbyte ) => Convert . ToSByte ( value ) ,
106+ _ => throw new NotSupportedException ( $ "Unsupported numeric type: { targetType . Name } ")
107+ } ;
108+
109+ return Expression . Constant ( convertedValue , targetType ) ;
110+ }
111+ catch ( OverflowException )
112+ {
113+ return Result . Fail ( $ "Value { value } is too large for type { targetType . Name } ") ;
114+ }
115+ catch ( Exception ex )
116+ {
117+ return Result . Fail ( $ "Error converting { value } to { targetType . Name } : { ex . Message } ") ;
118+ }
119+ }
78120}
0 commit comments