Skip to content

Commit ea69030

Browse files
nitishtde-sh
andcommitted
Suggest changes on how to process arrays with json (#132)
Co-authored-by: Devdutt Shenoi <devdutt@outlook.in>
1 parent a5aec8b commit ea69030

File tree

1 file changed

+38
-42
lines changed

1 file changed

+38
-42
lines changed

server/src/handler.rs

Lines changed: 38 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -287,59 +287,55 @@ pub async fn post_event(req: HttpRequest, body: web::Json<serde_json::Value>) ->
287287
}
288288
};
289289

290-
if body.is_array() {
290+
if let Some(array) = body.as_array() {
291291
let mut i = 0;
292-
loop {
293-
if body.get(i).unwrap().is_object() {
294-
let body =
295-
utils::flatten_json_body(web::Json(body[i].clone()), labels.clone()).unwrap();
296-
let schema = utils::read_schema_from_file(&stream_name).unwrap();
297-
let e = event::Event {
298-
body,
299-
path: option::get_opts().local_disk_path,
300-
stream_name: stream_name.clone(),
301-
schema: Bytes::from(schema),
302-
};
303-
304-
if let Err(e) = e.process() {
305-
return response::ServerResponse {
306-
msg: format!("Failed to process event at index {} due to err: {}", i, e),
307-
code: StatusCode::INTERNAL_SERVER_ERROR,
308-
}
309-
.to_http();
292+
293+
for body in array {
294+
let body = utils::flatten_json_body(web::Json(body.clone()), labels.clone()).unwrap();
295+
let schema = utils::read_schema_from_file(&stream_name).unwrap();
296+
let e = event::Event {
297+
body,
298+
path: option::get_opts().local_disk_path,
299+
stream_name: stream_name.clone(),
300+
schema: Bytes::from(schema),
301+
};
302+
303+
if let Err(e) = e.process() {
304+
return response::ServerResponse {
305+
msg: format!("Failed to process event at index {} due to err: {}", i, e),
306+
code: StatusCode::INTERNAL_SERVER_ERROR,
310307
}
311-
i += 1;
312-
} else {
313-
break;
308+
.to_http();
314309
}
310+
311+
i += 1;
315312
}
316313

317-
response::ServerResponse {
314+
return response::ServerResponse {
318315
msg: format!("Successfully posted {} events", i),
319316
code: StatusCode::OK,
320317
}
321-
.to_http()
322-
} else {
323-
let e = event::Event {
324-
body: utils::flatten_json_body(body, labels).unwrap(),
325-
path: option::get_opts().local_disk_path,
326-
stream_name,
327-
schema,
328-
};
329-
if let Err(e) = e.process() {
330-
return response::ServerResponse {
331-
msg: format!("Failed to process event due to err: {}", e),
332-
code: StatusCode::INTERNAL_SERVER_ERROR,
333-
}
334-
.to_http();
318+
.to_http();
319+
}
320+
let e = event::Event {
321+
body: utils::flatten_json_body(body, labels).unwrap(),
322+
path: option::get_opts().local_disk_path,
323+
stream_name,
324+
schema,
325+
};
326+
if let Err(e) = e.process() {
327+
return response::ServerResponse {
328+
msg: format!("Failed to process event due to err: {}", e),
329+
code: StatusCode::INTERNAL_SERVER_ERROR,
335330
}
331+
.to_http();
332+
}
336333

337-
response::ServerResponse {
338-
msg: "Successfully posted event".to_string(),
339-
code: StatusCode::OK,
340-
}
341-
.to_http()
334+
response::ServerResponse {
335+
msg: "Successfully posted event".to_string(),
336+
code: StatusCode::OK,
342337
}
338+
.to_http()
343339
}
344340

345341
/// collect labels passed from http headers

0 commit comments

Comments
 (0)