Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cmd] fix format table result for vec types #1605

Merged
merged 3 commits into from
Nov 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions commons/scmd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
71 changes: 38 additions & 33 deletions commons/scmd/src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,30 +61,35 @@ pub fn print_json(value: Value) -> Result<()> {
Ok(())
}

fn head_row(first_value: &Value) -> Result<(Row, Box<dyn RowBuilder>)> {
fn build_rows(values: &[Value]) -> Result<(Vec<Row>, Box<dyn RowBuilder>)> {
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)))
}
}

Expand All @@ -110,14 +115,13 @@ fn print_vec_table(values: Vec<Value>) -> 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)?;
for (i, row) in rows.into_iter().enumerate() {
print_rows.push(row);
print_rows.push(row_builder.build_row(&values[i])?);
}
let table = Table::new(rows, Default::default())?;
let table = Table::new(print_rows, Default::default())?;
table.print_stdout()?;
Ok(())
}
Expand All @@ -131,7 +135,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![];
Expand Down Expand Up @@ -180,13 +184,14 @@ struct ObjectRowBuilder {
impl RowBuilder for ObjectRowBuilder {
fn build_row(&self, value: &Value) -> Result<Row> {
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))
}
Expand Down