Skip to content

Commit beffc34

Browse files
committed
change(combinator): display as inline
1 parent e83c0f3 commit beffc34

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

src/combinator.cpp

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,25 @@ std::string handleBasicExpression(const nlohmann::json& expression)
7171
std::string handleOperatorExpression(const std::string& operation, const std::string& leftStr,
7272
const std::string& rightStr)
7373
{
74-
static const std::unordered_set<std::string> validOperators = { "+", "-", "*", "/" };
74+
if (operation.size() > 1)
75+
{
76+
throw std::logic_error("Invalid operator length!");
77+
}
7578

76-
if (validOperators.find(operation) == validOperators.end())
79+
switch (operation[0])
7780
{
81+
case '+':
82+
case '-':
83+
case '*':
84+
case '/':
85+
return "(" + leftStr + " " + operation + " " + rightStr + ")";
86+
default:
7887
throw std::runtime_error("Invalid operator: " + operation);
7988
}
80-
81-
return operation + (leftStr.empty() ? "" : " " + leftStr) +
82-
(rightStr.empty() ? "" : " " + rightStr);
8389
}
8490

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)
8793
{
8894
static const std::unordered_set<std::string> validAggregates = { "sum", "min", "max" };
8995

@@ -100,10 +106,10 @@ std::string handleAggregateExpression(const std::string& operation,
100106
auto input = std::accumulate(std::next(inputs.begin()), inputs.end(), inputs[0],
101107
[](std::string a, const std::string& b) { return a + ", " + b; });
102108

103-
return operation + ": [" + input + "]";
109+
return operation + "[" + input + "]";
104110
}
105111

106-
std::string displayExpression(const nlohmann::json& expression)
112+
std::string Combinator::displayExpression(const nlohmann::json& expression)
107113
{
108114
if (expression.is_number() || expression.is_string())
109115
{
@@ -119,15 +125,17 @@ std::string displayExpression(const nlohmann::json& expression)
119125

120126
if (operation == "throttle")
121127
{
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"]);
123133
}
124134

125-
if (expression.contains("left") || expression.contains("right"))
135+
if (expression.contains("left") && expression.contains("right"))
126136
{
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"]);
131139
return handleOperatorExpression(operation, leftStr, rightStr);
132140
}
133141

@@ -143,7 +151,7 @@ std::string displayExpression(const nlohmann::json& expression)
143151
{
144152
inputStrings.push_back(displayExpression(input));
145153
}
146-
return handleAggregateExpression(operation, inputStrings);
154+
return handleCombinationExpression(operation, inputStrings);
147155
}
148156

149157
throw std::runtime_error("Unsupported operation type: " + operation);

src/combinator.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class Combinator : public metricq::Transformer
3333
public:
3434
Combinator(const std::string& manager_host, const std::string& token);
3535
~Combinator();
36+
static std::string displayExpression(const nlohmann::json& expression);
3637

3738
private:
3839
void on_transformer_config(const metricq::json& config) override;

0 commit comments

Comments
 (0)