@@ -71,19 +71,25 @@ std::string handleBasicExpression(const nlohmann::json& expression)
71
71
std::string handleOperatorExpression (const std::string& operation, const std::string& leftStr,
72
72
const std::string& rightStr)
73
73
{
74
- static const std::unordered_set<std::string> validOperators = { " +" , " -" , " *" , " /" };
74
+ if (operation.size () > 1 )
75
+ {
76
+ throw std::logic_error (" Invalid operator length!" );
77
+ }
75
78
76
- if (validOperators. find ( operation) == validOperators. end () )
79
+ switch ( operation[ 0 ] )
77
80
{
81
+ case ' +' :
82
+ case ' -' :
83
+ case ' *' :
84
+ case ' /' :
85
+ return " (" + leftStr + " " + operation + " " + rightStr + " )" ;
86
+ default :
78
87
throw std::runtime_error (" Invalid operator: " + operation);
79
88
}
80
-
81
- return operation + (leftStr.empty () ? " " : " " + leftStr) +
82
- (rightStr.empty () ? " " : " " + rightStr);
83
89
}
84
90
85
- std::string handleAggregateExpression (const std::string& operation,
86
- const std::vector<std::string>& inputs)
91
+ std::string handleCombinationExpression (const std::string& operation,
92
+ const std::vector<std::string>& inputs)
87
93
{
88
94
static const std::unordered_set<std::string> validAggregates = { " sum" , " min" , " max" };
89
95
@@ -100,10 +106,10 @@ std::string handleAggregateExpression(const std::string& operation,
100
106
auto input = std::accumulate (std::next (inputs.begin ()), inputs.end (), inputs[0 ],
101
107
[](std::string a, const std::string& b) { return a + " , " + b; });
102
108
103
- return operation + " : [" + input + " ]" ;
109
+ return operation + " [" + input + " ]" ;
104
110
}
105
111
106
- std::string displayExpression (const nlohmann::json& expression)
112
+ std::string Combinator:: displayExpression (const nlohmann::json& expression)
107
113
{
108
114
if (expression.is_number () || expression.is_string ())
109
115
{
@@ -119,15 +125,17 @@ std::string displayExpression(const nlohmann::json& expression)
119
125
120
126
if (operation == " throttle" )
121
127
{
122
- return operation;
128
+ if (!expression.contains (" input" ))
129
+ {
130
+ throw std::logic_error (" Throttle does not contain a input" );
131
+ }
132
+ return handleBasicExpression (expression[" input" ]);
123
133
}
124
134
125
- if (expression.contains (" left" ) || expression.contains (" right" ))
135
+ if (expression.contains (" left" ) && expression.contains (" right" ))
126
136
{
127
- std::string leftStr =
128
- expression.contains (" left" ) ? displayExpression (expression[" left" ]) : " " ;
129
- std::string rightStr =
130
- expression.contains (" right" ) ? displayExpression (expression[" right" ]) : " " ;
137
+ std::string leftStr = displayExpression (expression[" left" ]);
138
+ std::string rightStr = displayExpression (expression[" right" ]);
131
139
return handleOperatorExpression (operation, leftStr, rightStr);
132
140
}
133
141
@@ -143,7 +151,7 @@ std::string displayExpression(const nlohmann::json& expression)
143
151
{
144
152
inputStrings.push_back (displayExpression (input));
145
153
}
146
- return handleAggregateExpression (operation, inputStrings);
154
+ return handleCombinationExpression (operation, inputStrings);
147
155
}
148
156
149
157
throw std::runtime_error (" Unsupported operation type: " + operation);
0 commit comments