From ae53ec09d7eef9a096dc4f4e6506bd0ca9d96d9e Mon Sep 17 00:00:00 2001 From: chengsuoyuan Date: Sun, 8 Nov 2020 23:33:35 +0800 Subject: [PATCH 1/2] [cmd] fix format table result for vec types --- Cargo.lock | 4 +-- commons/scmd/Cargo.toml | 4 +-- commons/scmd/src/result.rs | 73 +++++++++++++++++++++----------------- 3 files changed, 44 insertions(+), 37 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cf4a8d36d1..832dccb61d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6033,9 +6033,9 @@ dependencies = [ [[package]] name = "rust-flatten-json" -version = "0.1.0" +version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7d0d62c70781901ff6d68c677e092551ee10223b2149cf8a3d8bd5ab6280894b" +checksum = "264b19b0d73b6ab33a706dc04b2b8148390da5aa16cfe567a8df6135f431e972" dependencies = [ "error-chain 0.12.4", "log 0.4.11", diff --git a/commons/scmd/Cargo.toml b/commons/scmd/Cargo.toml index 9d99463f34..8d2bfaf41a 100644 --- a/commons/scmd/Cargo.toml +++ b/commons/scmd/Cargo.toml @@ -15,8 +15,8 @@ structopt = "0.3.20" clap = "2.33.3" git-version = "0.3.4" serde_json = { version="1.0", features = ["arbitrary_precision"]} -rust-flatten-json = "0.1.0" -cli-table = "0.3.0" +rust-flatten-json = "0.2.0" +cli-table = "0.3.1" once_cell = "1.4.1" [dev-dependencies] diff --git a/commons/scmd/src/result.rs b/commons/scmd/src/result.rs index ca913b3247..e5bc0c3a2a 100644 --- a/commons/scmd/src/result.rs +++ b/commons/scmd/src/result.rs @@ -61,30 +61,35 @@ pub fn print_json(value: Value) -> Result<()> { Ok(()) } -fn head_row(first_value: &Value) -> Result<(Row, Box)> { +fn build_rows(values: &[Value]) -> Result<(Vec, Box)> { let bold = CellFormat::builder().bold(true).build(); - let simple_value = first_value.is_number() - || first_value.is_boolean() - || first_value.is_boolean() - || first_value.is_string(); - if simple_value { - let row = Row::new(vec![Cell::new("Result", bold)]); - Ok((row, Box::new(SimpleRowBuilder))) - } else { - let mut flat = json!({}); - flatten(first_value, &mut flat, None, true) - .map_err(|e| anyhow::Error::msg(e.description().to_string()))?; - let obj = flat.as_object().expect("must be a object"); - let mut cells = vec![]; - let mut field_names = vec![]; - for (k, _v) in obj { - field_names.push(k.to_string()); - } - for field_name in &field_names { - cells.push(Cell::new(field_name, bold)); + let mut rows = vec![]; + let mut field_names = Vec::new(); + let is_simple = |value: &Value| value.is_number() || value.is_boolean() || value.is_string(); + let mut exist_not_simple = false; + for value in values { + if is_simple(value) { + rows.push(Row::new(vec![Cell::new("Result", bold)])); + } else { + exist_not_simple = true; + let mut flat = json!({}); + flatten(value, &mut flat, None, true, None) + .map_err(|e| anyhow::Error::msg(e.description().to_string()))?; + let obj = flat.as_object().expect("must be a object"); + let mut cells = vec![]; + obj.keys().for_each(|key| { + cells.push(Cell::new(key, bold)); + if !field_names.contains(key) { + field_names.push(key.to_string()); + } + }); + rows.push(Row::new(cells)); } - let row = Row::new(cells); - Ok((row, Box::new(ObjectRowBuilder { field_names }))) + } + if exist_not_simple { + Ok((rows, Box::new(ObjectRowBuilder { field_names }))) + } else { + Ok((rows, Box::new(SimpleRowBuilder))) } } @@ -110,14 +115,15 @@ fn print_vec_table(values: Vec) -> Result<()> { if first_value.is_array() { bail!("Not support embed array in Action Result.") } - let (head_row, row_builder) = head_row(&first_value)?; - let mut rows = vec![]; - rows.push(head_row); - rows.push(row_builder.build_row(&first_value)?); - for value in values[1..].iter() { - rows.push(row_builder.build_row(&value)?); + let mut print_rows = vec![]; + let (rows, row_builder) = build_rows(&values)?; + let mut i = 0; + for row in rows { + print_rows.push(row); + print_rows.push(row_builder.build_row(&values[i])?); + i += 1; } - let table = Table::new(rows, Default::default())?; + let table = Table::new(print_rows, Default::default())?; table.print_stdout()?; Ok(()) } @@ -131,7 +137,7 @@ fn print_value_table(value: Value) -> Result<()> { // value must be a object at here. let bold = CellFormat::builder().bold(true).build(); let mut flat = json!({}); - flatten(&value, &mut flat, None, true) + flatten(&value, &mut flat, None, true, None) .map_err(|e| anyhow::Error::msg(e.description().to_string()))?; let obj = flat.as_object().expect("must be a object"); let mut rows = vec![]; @@ -180,13 +186,14 @@ struct ObjectRowBuilder { impl RowBuilder for ObjectRowBuilder { fn build_row(&self, value: &Value) -> Result { let mut flat = json!({}); - flatten(value, &mut flat, None, true) + flatten(value, &mut flat, None, true, None) .map_err(|e| anyhow::Error::msg(e.description().to_string()))?; let obj = flat.as_object().expect("must be a object"); let mut cells = vec![]; for field in &self.field_names { - let v = obj.get(field).unwrap_or(&Value::Null); - cells.push(Cell::new(value_to_string(v).as_str(), Default::default())); + if let Some(v) = obj.get(field) { + cells.push(Cell::new(value_to_string(v).as_str(), Default::default())); + } } Ok(Row::new(cells)) } From 47a5b4b36f313ebf96ff9fb98928c248678d1718 Mon Sep 17 00:00:00 2001 From: chengsuoyuan Date: Mon, 9 Nov 2020 10:54:42 +0800 Subject: [PATCH 2/2] [cmd] fix clippy check --- commons/scmd/src/result.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/commons/scmd/src/result.rs b/commons/scmd/src/result.rs index e5bc0c3a2a..ec7a3dc91f 100644 --- a/commons/scmd/src/result.rs +++ b/commons/scmd/src/result.rs @@ -117,11 +117,9 @@ fn print_vec_table(values: Vec) -> Result<()> { } let mut print_rows = vec![]; let (rows, row_builder) = build_rows(&values)?; - let mut i = 0; - for row in rows { + for (i, row) in rows.into_iter().enumerate() { print_rows.push(row); print_rows.push(row_builder.build_row(&values[i])?); - i += 1; } let table = Table::new(print_rows, Default::default())?; table.print_stdout()?;