From 491fafa22714c45a850c6fc159412d38d9e106eb Mon Sep 17 00:00:00 2001 From: Maxim Biryukov Date: Mon, 29 Nov 2021 00:28:12 +0300 Subject: [PATCH] json CommandOutput::Table: {"headers": [...], "values": [[...]]} -> [{"header": "value"}] resolve #906 --- core/serv-api/src/lib.rs | 43 ++++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/core/serv-api/src/lib.rs b/core/serv-api/src/lib.rs index e45403934c..c2f135ac0e 100644 --- a/core/serv-api/src/lib.rs +++ b/core/serv-api/src/lib.rs @@ -1,6 +1,7 @@ use anyhow::Result; use prettytable::{color, format, format::TableFormat, Attr, Cell, Row, Table}; use serde::Serialize; +use std::collections::HashMap; use std::path::PathBuf; #[derive(Clone, Debug, Default)] @@ -55,14 +56,40 @@ impl CommandOutput { header, } => { if json_output { - println!( - "{}", - serde_json::to_string_pretty(&serde_json::json!({ - "headers": columns, - "values": values - })) - .unwrap() - ) + let columns_size_eq_values_size = values.iter().all(|row| match row { + serde_json::Value::Array(values) => values.len() == columns.len(), + _ => false, + }); + if columns_size_eq_values_size { + let kvs: Vec> = values + .iter() + .map(|row| match row { + serde_json::Value::Array(row_values) + if columns.len() == row_values.len() => + { + columns + .into_iter() + .enumerate() + .map(|(idx, key)| (key, &row_values[idx])) + .collect() + } + _ => unreachable!(), + }) + .collect(); + println!( + "{}", + serde_json::to_string_pretty(&serde_json::json!(kvs)).unwrap() + ) + } else { + println!( + "{}", + serde_json::to_string_pretty(&serde_json::json!({ + "headers": columns, + "values": values + })) + .unwrap() + ) + } } else { if let Some(txt) = header { println!("{}", txt);