Skip to content

Commit 550b0be

Browse files
committed
Reduce complexity more in RegressionTest::from_yajl_node
1 parent cf46967 commit 550b0be

1 file changed

Lines changed: 39 additions & 63 deletions

File tree

test/regression/regression_test.cc

Lines changed: 39 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,24 @@ inline std::vector<std::pair<std::string, std::string>>
8585
return vec;
8686
}
8787

88+
static inline void set_int_from_yajl(int &dest, std::string_view want_key, std::string_view key, const yajl_val &val) {
89+
if (key == want_key) {
90+
dest = YAJL_GET_INTEGER(val);
91+
}
92+
}
93+
94+
static inline void set_opt_int_from_yajl(std::optional<int> &dest, std::string_view want_key, std::string_view key, const yajl_val &val) {
95+
if (key == want_key) {
96+
dest = YAJL_GET_INTEGER(val);
97+
}
98+
}
99+
100+
static inline void set_string_from_yajl(std::string &dest, std::string_view want_key, std::string_view key, const yajl_val &val) {
101+
if (key == want_key) {
102+
dest = YAJL_GET_STRING(val);
103+
}
104+
}
105+
88106
std::unique_ptr<RegressionTest> RegressionTest::from_yajl_node(const yajl_val &node) {
89107
size_t nelem = node->u.object.len;
90108
auto u = std::make_unique<RegressionTest>();
@@ -94,27 +112,13 @@ std::unique_ptr<RegressionTest> RegressionTest::from_yajl_node(const yajl_val &n
94112
const char *key = node->u.object.keys[ i ];
95113
yajl_val val = node->u.object.values[ i ];
96114

97-
if (strcmp(key, "enabled") == 0) {
98-
u->enabled = YAJL_GET_INTEGER(val);
99-
}
100-
if (strcmp(key, "version_min") == 0) {
101-
u->version_min = YAJL_GET_INTEGER(val);
102-
}
103-
if (strcmp(key, "version_max") == 0) {
104-
u->version_max = YAJL_GET_INTEGER(val);
105-
}
106-
if (strcmp(key, "title") == 0) {
107-
u->title = YAJL_GET_STRING(val);
108-
}
109-
if (strcmp(key, "url") == 0) {
110-
u->url = YAJL_GET_STRING(val);
111-
}
112-
if (strcmp(key, "resource") == 0) {
113-
u->resource = YAJL_GET_STRING(val);
114-
}
115-
if (strcmp(key, "github_issue") == 0) {
116-
u->github_issue = YAJL_GET_INTEGER(val);
117-
}
115+
set_int_from_yajl(u->enabled, "enabled", key, val);
116+
set_int_from_yajl(u->version_min, "version_min", key, val);
117+
set_opt_int_from_yajl(u->version_max, "version_max", key, val);
118+
set_string_from_yajl(u->title, "title", key, val);
119+
set_string_from_yajl(u->url, "url", key, val);
120+
set_string_from_yajl(u->resource, "resource", key, val);
121+
set_opt_int_from_yajl(u->github_issue, "github_issue", key, val);
118122
if (strcmp(key, "client") == 0) {
119123
u->update_client_from_yajl_node(val);
120124
}
@@ -145,12 +149,8 @@ void RegressionTest::update_client_from_yajl_node(const yajl_val &val) {
145149
const char *key2 = val->u.object.keys[j];
146150
yajl_val val2 = val->u.object.values[j];
147151

148-
if (strcmp(key2, "ip") == 0) {
149-
clientIp = YAJL_GET_STRING(val2);
150-
}
151-
if (strcmp(key2, "port") == 0) {
152-
clientPort = YAJL_GET_INTEGER(val2);
153-
}
152+
set_string_from_yajl(clientIp, "ip", key2, val2);
153+
set_int_from_yajl(clientPort, "port", key2, val2);
154154
}
155155
}
156156

@@ -159,15 +159,9 @@ void RegressionTest::update_server_from_yajl_node(const yajl_val &val) {
159159
const char *key2 = val->u.object.keys[j];
160160
yajl_val val2 = val->u.object.values[j];
161161

162-
if (strcmp(key2, "ip") == 0) {
163-
serverIp = YAJL_GET_STRING(val2);
164-
}
165-
if (strcmp(key2, "port") == 0) {
166-
serverPort = YAJL_GET_INTEGER(val2);
167-
}
168-
if (strcmp(key2, "hostname") == 0) {
169-
hostname = YAJL_GET_STRING(val2);
170-
}
162+
set_string_from_yajl(serverIp, "ip", key2, val2);
163+
set_int_from_yajl(serverPort, "port", key2, val2);
164+
set_string_from_yajl(hostname, "hostname", key2, val2);
171165
}
172166
}
173167

@@ -176,12 +170,8 @@ void RegressionTest::update_request_from_yajl_node(const yajl_val &val) {
176170
const char *key2 = val->u.object.keys[j];
177171
yajl_val val2 = val->u.object.values[j];
178172

179-
if (strcmp(key2, "uri") == 0) {
180-
uri = YAJL_GET_STRING(val2);
181-
}
182-
if (strcmp(key2, "method") == 0) {
183-
method = YAJL_GET_STRING(val2);
184-
}
173+
set_string_from_yajl(uri, "uri", key2, val2);
174+
set_string_from_yajl(method, "method", key2, val2);
185175
if (strcmp(key2, "http_version") == 0) {
186176
httpVersion = YAJL_GET_NUMBER(val2);
187177
}
@@ -207,9 +197,7 @@ void RegressionTest::update_response_from_yajl_node(const yajl_val &val) {
207197
response_body = yajl_array_to_str(val2);
208198
response_body_lines = yajl_array_to_vec_str(val2);
209199
}
210-
if (strcmp(key2, "protocol") == 0) {
211-
response_protocol = YAJL_GET_STRING(val2);
212-
}
200+
set_string_from_yajl(response_protocol, "protocol", key2, val2);
213201
}
214202
}
215203

@@ -218,24 +206,12 @@ void RegressionTest::update_expected_from_yajl_node(const yajl_val &val) {
218206
const char *key2 = val->u.object.keys[j];
219207
yajl_val val2 = val->u.object.values[j];
220208

221-
if (strcmp(key2, "audit_log") == 0) {
222-
audit_log = YAJL_GET_STRING(val2);
223-
}
224-
if (strcmp(key2, "debug_log") == 0) {
225-
debug_log = YAJL_GET_STRING(val2);
226-
}
227-
if (strcmp(key2, "error_log") == 0) {
228-
error_log = YAJL_GET_STRING(val2);
229-
}
230-
if (strcmp(key2, "http_code") == 0) {
231-
http_code = YAJL_GET_INTEGER(val2);
232-
}
233-
if (strcmp(key2, "redirect_url") == 0) {
234-
redirect_url = YAJL_GET_STRING(val2);
235-
}
236-
if (strcmp(key2, "parser_error") == 0) {
237-
parser_error = YAJL_GET_STRING(val2);
238-
}
209+
set_string_from_yajl(audit_log, "audit_log", key2, val2);
210+
set_string_from_yajl(debug_log, "debug_log", key2, val2);
211+
set_string_from_yajl(error_log, "error_log", key2, val2);
212+
set_int_from_yajl(http_code, "http_code", key2, val2);
213+
set_string_from_yajl(redirect_url, "redirect_url", key2, val2);
214+
set_string_from_yajl(parser_error, "parser_error", key2, val2);
239215
}
240216
}
241217

0 commit comments

Comments
 (0)