-
Notifications
You must be signed in to change notification settings - Fork 0
/
ICondition.cs
253 lines (211 loc) · 8.62 KB
/
ICondition.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace FSS.Omnius.Modules
{
/// <summary>
/// Resolving conditions
/// format: ((A == B)&(C isEmpty))|(D isNotEmpty)
/// </summary>
public interface ICondition
{
string GetCondition();
}
public static class IConditionExtend
{
public static void CheckCondition(this ICondition conditionItem, Dictionary<string, object> variables)
{
string condition = conditionItem.GetCondition();
if (!resolveSet(condition, variables))
throw new UnauthorizedAccessException();
}
#region Resolve
private static bool resolveSet(string condition, Dictionary<string, object> variables)
{
//// is not set anymore
if (!condition.StartsWith("("))
return resolve(condition, variables);
//// set
bool result = true;
char operation = '&';
while (operation == '&' || operation == '|')
{
// resolve condition
int rightBracketIndex = findRightBracket(condition);
bool currentResult = resolveSet(condition.Substring(1, rightBracketIndex - 1), variables);
result = operation == '&'
? result && currentResult
: result || currentResult;
// end
if (rightBracketIndex + 1 >= condition.Length)
break;
// prepare next step
operation = condition[rightBracketIndex + 1];
condition = condition.Substring(rightBracketIndex + 2);
}
return result;
}
private static bool resolve(string condition, Dictionary<string, object> variables)
{
string[] singleOperator = new string[] { "isempty", "isnotempty" };
string[] dualOperator = new string[] { "==", "!=", ">", ">=", "<", "<=", "contains", };
if (string.IsNullOrWhiteSpace(condition))
return true;
string[] splitted = condition.Split(' ');
string operation = splitted[1].ToLower();
if (!((singleOperator.Contains(operation) && splitted.Length == 2) || (dualOperator.Contains(operation) && splitted.Length == 3)))
throw new FormatException();
int compare;
switch (splitted[1].ToLower())
{
case "==":
return Equals(parseValue(splitted[0], variables), parseValue(splitted[2], variables));
case "!=":
return !Equals(parseValue(splitted[0], variables), parseValue(splitted[2], variables));
case ">":
compare = (parseValue(splitted[0], variables) as IComparable).CompareTo(parseValue(splitted[2], variables));
return compare > 0;
case ">=":
compare = (parseValue(splitted[0], variables) as IComparable).CompareTo(parseValue(splitted[2], variables));
return compare > 0 || compare == 0;
case "<":
compare = (parseValue(splitted[0], variables) as IComparable).CompareTo(parseValue(splitted[2], variables));
return compare < 0;
case "<=":
compare = (parseValue(splitted[0], variables) as IComparable).CompareTo(parseValue(splitted[2], variables));
return compare < 0 || compare == 0;
case "isempty":
return parseValue(splitted[0], variables) == null;
case "isnotempty":
return parseValue(splitted[0], variables) != null;
case "contains":
if (splitted.Length == 2)
throw new FormatException();
return (parseValue(splitted[0], variables) as string).Contains((string)parseValue(splitted[2], variables));
default:
throw new FormatException();
}
}
private static object parseValue(string input, Dictionary<string, object> vars)
{
// value
if (input.Length > 1 && input[1] == '$')
return Convertor.convert(input[0], input.Substring(2));
// variable
return GetChainedProperty(input, vars);
}
private static int findRightBracket(string item, int startIndex = 0)
{
int count = 0;
for (int i = startIndex; i < item.Length; i++)
{
switch (item[i])
{
case '(':
count++;
break;
case ')':
count--;
break;
}
if (count == 0)
return i;
}
return -1;
}
#endregion
#region chained
private static object GetChainedProperty(string chainedKey, Dictionary<string, object> vars)
{
int index = chainedKey.IndexOfAny(new char[] { '.', '[' });
if (index == -1)
{
if (vars.ContainsKey(chainedKey))
return vars[chainedKey];
else
throw new MissingFieldException($"Missing key '{chainedKey}' on list [{vars.ToString()}].");
}
string key = chainedKey.Substring(0, index);
if (key == "__Model" || key == "__Result")
return vars[chainedKey];
return GetChained(vars[key], chainedKey.Substring(index));
}
private static object GetChained(object o, string calling)
{
string[] propertyCallings = calling.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string propertyCalling in propertyCallings)
{
o = resolveProperty(o, propertyCalling);
}
return o;
}
private static object resolveProperty(object o, string calling)
{
int iStart = calling.IndexOf('[');
// no array
if (iStart < 0)
return resolveSingleProperty(o, calling);
// array with property
if (iStart > 0)
{
o = resolveSingleProperty(o, calling.Substring(0, iStart));
calling = calling.Substring(iStart);
iStart = 0;
}
int iEnd = calling.IndexOf(']', iStart);
// single array
if (iEnd == calling.Length - 1)
return resolveSingleArray(o, calling.Substring(1, calling.Length - 2));
// multiple arrays
while (iStart >= 0)
{
o = resolveSingleArray(o, calling.Substring(iStart + 1, iEnd - iStart - 1));
iStart = calling.IndexOf('[', iEnd);
iEnd = iStart >= 0 ? calling.IndexOf(']', iStart) : -1;
}
return o;
}
private static object resolveSingleArray(object o, string calling)
{
var prop = o.GetType().GetMethod("get_Item", new Type[] { typeof(string) });
return prop.Invoke(o, new object[] { calling });
}
private static object resolveSingleProperty(object o, string calling)
{
//if (o is DBItem && ((DBItem)o).getColumnNames().Contains(calling))
// return ((DBItem)o)[calling];
PropertyInfo property = o.GetType().GetProperty(calling);
if (property == null)
throw new MissingFieldException($"Missing field '{calling}' on item [{o.ToString()}].");
return property.GetValue(o);
}
#endregion
}
public class Convertor
{
public static object convert(char shortcut, object input)
{
switch (shortcut)
{
case 'i':
return Convert.ToInt32(input);
case 'b':
return Convert.ToBoolean(input);
case 's':
return Convert.ToString(input);
case 'f':
return Convert.ToDouble(input);
case 'd':
return Convert.ToDateTime(input);
case 'l':
return input;
// none
default:
throw new KeyNotFoundException($"Cannot indentify data type '{shortcut}'");
}
}
}
}