8
8
9
9
namespace RedditSharp . Search
10
10
{
11
- public class DefaultSearchFormatter : ISearchFormatter
11
+ public class DefaultSearchFormatter : IAdvancedSearchFormatter
12
12
{
13
- string ISearchFormatter . Format ( Expression < Func < AdvancedSearchFilter , bool > > search )
13
+ #region Constants
14
+ private const string BOOL_PROPERTY_PREFIX = "Is" ;
15
+ #endregion Constants
16
+
17
+
18
+ string IAdvancedSearchFormatter . Format ( Expression < Func < AdvancedSearchFilter , bool > > search )
14
19
{
15
20
Expression expression = null ;
16
21
Stack < Expression > expressionStack = new Stack < Expression > ( ) ;
@@ -33,30 +38,39 @@ string ISearchFormatter.Format(Expression<Func<AdvancedSearchFilter, bool>> sear
33
38
case ConstantExpression constantExpresssion :
34
39
searchStack . Push ( ConstantExpressionHelper ( constantExpresssion ) ) ;
35
40
break ;
41
+ case MethodCallExpression methodCallExpression :
42
+ searchStack . Push ( MethodCallExpressionHelper ( methodCallExpression ) ) ;
43
+ break ;
36
44
default :
37
45
throw new NotImplementedException ( expression . ToString ( ) ) ;
38
46
}
39
47
}
40
48
41
49
Stack < string > compoundSearchStack = new Stack < string > ( ) ;
42
- while ( formatInfoStack . Count > 0 )
50
+ while ( formatInfoStack . Count > 0 )
43
51
{
44
52
FormatInfo current = formatInfoStack . Pop ( ) ;
45
53
string [ ] formatParameters = new string [ current . ParameterCount ] ;
46
54
int currentCount = current . ParameterCount ;
47
- while ( currentCount > 0 )
55
+ while ( currentCount > 0 )
48
56
{
49
- formatParameters [ formatParameters . Length - currentCount ] = current . IsCompound ? compoundSearchStack . Pop ( ) : searchStack . Pop ( ) ;
57
+ formatParameters [ formatParameters . Length - currentCount ] = current . IsCompound ? compoundSearchStack . Pop ( ) : searchStack . Pop ( ) ;
50
58
currentCount -- ;
51
59
}
52
-
60
+
53
61
compoundSearchStack . Push ( string . Format ( current . Pattern , formatParameters ) ) ;
54
-
62
+
55
63
}
56
64
57
65
return compoundSearchStack . Pop ( ) ;
58
66
}
59
67
68
+ private string MethodCallExpressionHelper ( MethodCallExpression expression )
69
+ {
70
+ var o = InvokeGetExpression ( expression ) ;
71
+ return o . ToString ( ) ;
72
+ }
73
+
60
74
private string ConstantExpressionHelper ( ConstantExpression constantExpresssion )
61
75
{
62
76
return constantExpresssion . ToString ( ) . Replace ( "\" " , "" ) ;
@@ -79,29 +93,23 @@ private void BinaryExpressionHelper(BinaryExpression expression, Stack<Expressio
79
93
expressionStack . Push ( expression . Left ) ;
80
94
}
81
95
82
-
83
96
if ( expression . NodeType != ExpressionType . Equal )
84
97
{
85
98
formatInfoStack . Push ( expression . ToFormatInfo ( ) ) ;
86
- //searchStack.Push("NOT(+{0}+)");
87
99
}
88
-
89
-
90
100
}
91
101
92
102
93
103
private void UnaryExpressionHelper ( UnaryExpression expression , Stack < Expression > expressionStack , Stack < FormatInfo > formatInfoStack )
94
104
{
95
- formatInfoStack . Push ( expression . ToFormatInfo ( ) ) ;
105
+ formatInfoStack . Push ( expression . ToFormatInfo ( ) ) ;
96
106
expressionStack . Push ( expression . Operand ) ;
97
- //return expressionOperator;
98
107
}
99
108
100
109
private void MemberExpressionHelper ( MemberExpression expression , Stack < string > searchStack , Stack < FormatInfo > formatInfoStack )
101
110
{
102
111
MemberInfo member = expression . Member ;
103
112
104
-
105
113
if ( member . DeclaringType == typeof ( AdvancedSearchFilter ) )
106
114
{
107
115
string result = member . Name . Replace ( BOOL_PROPERTY_PREFIX , string . Empty ) . ToLower ( ) ;
@@ -110,7 +118,6 @@ private void MemberExpressionHelper(MemberExpression expression, Stack<string> s
110
118
if ( expression . Type == typeof ( bool ) )
111
119
{
112
120
searchStack . Push ( "1" ) ;
113
-
114
121
}
115
122
}
116
123
else
@@ -130,30 +137,6 @@ private static object InvokeGetExpression(Expression expression)
130
137
return getter ( ) ;
131
138
}
132
139
133
-
134
-
135
-
136
- private const string BOOL_PROPERTY_PREFIX = "Is" ;
137
-
138
-
139
- private static readonly List < ExpressionType > conditionalTypes = new List < ExpressionType > ( )
140
- {
141
- ExpressionType . AndAlso ,
142
- ExpressionType . And ,
143
- ExpressionType . OrElse ,
144
- ExpressionType . Or
145
- } ;
146
-
147
- private static readonly List < ExpressionType > evaluateExpressions = new List < ExpressionType > ( )
148
- {
149
- ExpressionType . Add ,
150
- ExpressionType . Subtract ,
151
- ExpressionType . Multiply ,
152
- ExpressionType . Divide ,
153
- ExpressionType . Coalesce ,
154
- ExpressionType . Conditional
155
- } ;
156
-
157
140
private static bool IsAdvancedSearchMemberExpression ( Expression expression )
158
141
{
159
142
MemberExpression memberExpression = expression as MemberExpression ;
@@ -180,28 +163,4 @@ public FormatInfo(string pattern, int parameterCount = 0, bool isCompound = fals
180
163
internal static FormatInfo MemberAccess = new FormatInfo ( "{1}:{0}" , 2 ) ;
181
164
}
182
165
}
183
-
184
- public static class Extensions
185
- {
186
- internal static DefaultSearchFormatter . FormatInfo ToFormatInfo ( this Expression expression )
187
- {
188
- ExpressionType ? type = expression ? . NodeType ;
189
- switch ( type )
190
- {
191
- case ExpressionType . Not :
192
- case ExpressionType . NotEqual :
193
- return DefaultSearchFormatter . FormatInfo . Not ;
194
- case ExpressionType . Equal :
195
- throw new NotImplementedException ( "Currently not supporting Equal expression." ) ;
196
- case ExpressionType . AndAlso :
197
- return DefaultSearchFormatter . FormatInfo . AndAlso ;
198
- case ExpressionType . MemberAccess :
199
- return DefaultSearchFormatter . FormatInfo . MemberAccess ;
200
- case ExpressionType . OrElse :
201
- return DefaultSearchFormatter . FormatInfo . OrElse ;
202
- }
203
- throw new NotImplementedException ( $ "{ type . ToString ( ) } is not implemented.") ;
204
- }
205
-
206
- }
207
166
}
0 commit comments