-
Notifications
You must be signed in to change notification settings - Fork 1
/
EqualityExpression
135 lines (118 loc) · 4.5 KB
/
EqualityExpression
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
using System;
using System.Runtime.Serialization;
namespace CustomAttributes
{
public class ExpressionParser
{
public string AndOperator = "&&";
public string OrOperator = "||";
public bool Parse(object ob, string text)
{
bool result = true;
var andExpressions = GetEqualityExpressions(text);
foreach (var andExpression in andExpressions)
{
var _res = false;
foreach (var orExpression in andExpression)
{
if (orExpression.Execute(ob))
{
_res = true;
break;
}
}
if (!_res)
{
result = false;
break;
}
}
return result;
}
public EqualityExpression[][] GetEqualityExpressions(string text)
{
var andParts = text.Split(AndOperator.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
var andExpressions = new EqualityExpression[andParts.Length][];
for (var i = 0; i < andParts.Length; i++)
{
var andPart = andParts[i];
var orParts = andPart.Split(OrOperator.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
andExpressions[i] = new EqualityExpression[orParts.Length];
for (var j = 0; j < orParts.Length; j++)
{
var orPart = orParts[j];
andExpressions[i][j] = new EqualityExpression(orPart);
}
}
return andExpressions;
}
}
[DataContract(Name = "EqualityExpression")]
public class EqualityExpression
{
[DataMember]
public EqualityOperator Operator;
[DataMember]
public string Property;
[DataMember]
public dynamic Value;
private readonly EqualityOperator[] _operators = new[]
{
new EqualityOperator("==", (a, b) => a == b),
new EqualityOperator("!=", (a, b) => a != b),
new EqualityOperator(">", (a, b) => a > b),
new EqualityOperator("<", (a, b) => a < b),
new EqualityOperator(">=", (a, b) => a >= b),
new EqualityOperator("<=", (a, b) => a <= b)
};
public EqualityExpression(string text)
{
Operator = null;
foreach (var op in _operators)
{
if(text.Contains(op.Text))
{
Operator = op;
break;
}
}
if (Operator == null)
throw new Exception("Operator is missing");
var parts = text.Split(Operator.Text.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
if (parts.Length != 2)
throw new Exception("Syntax error");
Property = parts[0].Trim();
Value = _GetValue(parts[1]);
}
private dynamic _GetValue(string valueString)
{
valueString = valueString.Trim();
if (valueString.StartsWith("'") && valueString.EndsWith("'"))
return valueString.Replace("'", string.Empty);
bool b;
if (bool.TryParse(valueString, out b))
return b;
double num;
if (double.TryParse(valueString, out num))
return num;
throw new Exception("Unknown value type");
}
public bool Execute(object ob)
{
var value = ob.GetType().GetProperty(Property).GetValue(ob, null);
return Operator.Method(value, Value);
}
[DataContract(Name = "EqualityOperator")]
public class EqualityOperator
{
[DataMember]
public string Text;
public Func<dynamic, dynamic, bool> Method;
public EqualityOperator(string text, Func<dynamic, dynamic, bool> method)
{
Text = text;
Method = method;
}
}
}
}