-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgenquery_jsonify.cpp
More file actions
194 lines (171 loc) · 5.55 KB
/
Copy pathgenquery_jsonify.cpp
File metadata and controls
194 lines (171 loc) · 5.55 KB
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
#ifndef GENQUERY_JSONIFY_HPP
#define GENQUERY_JSONIFY_HPP
#include "genquery_ast_types.hpp"
#include "json/json.h"
namespace Genquery {
template <typename Iterable>
Json::Value
iterableToJson(Iterable const& iterable) {
Json::Value v{Json::arrayValue};
for (auto&& element: iterable) {
v.append(element);
}
return v;
}
struct JsonifyVisitor: public boost::static_visitor<Json::Value> {
template <typename T>
Json::Value
operator()(const T& arg) const {
return jsonify(arg);
}
};
Json::Value
jsonify(const Column& column) {
Json::Value ret{Json::objectValue};
ret["type"] = "column";
ret["name"] = column.name;
return ret;
}
Json::Value
jsonify(const SelectFunction& select_function) {
Json::Value ret{Json::objectValue};
ret["type"] = "select_function";
ret["name"] = select_function.name;
ret["column"] = jsonify(select_function.column);
return ret;
}
Json::Value
jsonify(const Selections& selections) {
Json::Value ret{Json::arrayValue};
for (auto&& selection: selections) {
ret.append(boost::apply_visitor(JsonifyVisitor{}, selection));
}
return ret;
}
Json::Value
jsonify(const ConditionOperator_Or& op_or) {
Json::Value ret{Json::objectValue};
ret["type"] = "or";
ret["left"] = boost::apply_visitor(JsonifyVisitor(), op_or.left);
ret["right"] = boost::apply_visitor(JsonifyVisitor(), op_or.right);
return ret;
}
Json::Value
jsonify(const ConditionOperator_And& op_and) {
Json::Value ret{Json::objectValue};
ret["type"] = "and";
ret["left"] = boost::apply_visitor(JsonifyVisitor(), op_and.left);
ret["right"] = boost::apply_visitor(JsonifyVisitor(), op_and.right);
return ret;
}
Json::Value
jsonify(const ConditionOperator_Not& op_not) {
Json::Value ret{Json::objectValue};
ret["type"] = "not";
ret["expression"] = boost::apply_visitor(JsonifyVisitor(), op_not.expression);
return ret;
}
Json::Value
jsonify(const ConditionNotEqual& not_equal) {
Json::Value ret{Json::objectValue};
ret["type"] = "not_equal";
ret["string_literal"] = not_equal.string_literal;
return ret;
}
Json::Value
jsonify(const ConditionEqual& equal) {
Json::Value ret{Json::objectValue};
ret["type"] = "equal";
ret["string_literal"] = equal.string_literal;
return ret;
}
Json::Value
jsonify(const ConditionLessThan& less_than) {
Json::Value ret{Json::objectValue};
ret["type"] = "less_than";
ret["string_literal"] = less_than.string_literal;
return ret;
}
Json::Value
jsonify(const ConditionLessThanOrEqualTo& less_than_or_equal_to) {
Json::Value ret{Json::objectValue};
ret["type"] = "less_than_or_equal_to";
ret["string_literal"] = less_than_or_equal_to.string_literal;
return ret;
}
Json::Value
jsonify(const ConditionGreaterThan& greater_than) {
Json::Value ret{Json::objectValue};
ret["type"] = "greater_than";
ret["string_literal"] = greater_than.string_literal;
return ret;
}
Json::Value
jsonify(const ConditionGreaterThanOrEqualTo& greater_than_or_equal_to) {
Json::Value ret{Json::objectValue};
ret["type"] = "greater_than_or_equal_to";
ret["string_literal"] = greater_than_or_equal_to.string_literal;
return ret;
}
Json::Value
jsonify(const ConditionBetween& between) {
Json::Value ret{Json::objectValue};
ret["type"] = "between";
ret["low"] = between.low;
ret["high"] = between.high;
return ret;
}
Json::Value
jsonify(const ConditionIn& in) {
Json::Value ret{Json::objectValue};
ret["type"] = "in";
ret["list_of_string_literals"] = iterableToJson(in.list_of_string_literals);
return ret;
}
Json::Value
jsonify(const ConditionLike& like) {
Json::Value ret{Json::objectValue};
ret["type"] = "like";
ret["string_literal"] = like.string_literal;
return ret;
}
Json::Value
jsonify(const ConditionParentOf& parent_of) {
Json::Value ret{Json::objectValue};
ret["type"] = "parent_of";
ret["string_literal"] = parent_of.string_literal;
return ret;
}
Json::Value
jsonify(const ConditionBeginningOf& beginning_of) {
Json::Value ret{Json::objectValue};
ret["type"] = "beginning_of";
ret["string_literal"] = beginning_of.string_literal;
return ret;
}
Json::Value
jsonify(const Condition& condition) {
Json::Value ret{Json::objectValue};
ret["type"] = "condition";
ret["column"] = jsonify(condition.column);
ret["expression"] = boost::apply_visitor(JsonifyVisitor(), condition.expression);
return ret;
}
Json::Value
jsonify(const Conditions& conditions) {
Json::Value ret{Json::arrayValue};
for (auto&& condition: conditions) {
ret.append(jsonify(condition));
}
return ret;
}
Json::Value
jsonify(const Select& select) {
Json::Value root{Json::objectValue};
root["type"] = "select";
root["selections"] = jsonify(select.selections);
root["conditions"] = jsonify(select.conditions);
return root;
}
}
#endif // GENQUERY_JSONIFY_HPP