From afe83104c6b1e106a8cfc78b45ed1731a227eb38 Mon Sep 17 00:00:00 2001 From: Stefan Stanciulescu <71919805+onthebridgetonowhere@users.noreply.github.com> Date: Sat, 22 Jan 2022 15:19:40 +0100 Subject: [PATCH] Fix flatten's dropping column issue #756 (#805) * Fix flatten's dropping column issue, and do some cleanup - better variable naming. * Fix failing test * Fix failing tests --- crates/nu-command/src/filters/flatten.rs | 105 ++++++++++++++++------- 1 file changed, 74 insertions(+), 31 deletions(-) diff --git a/crates/nu-command/src/filters/flatten.rs b/crates/nu-command/src/filters/flatten.rs index d6dc85ccd2..31d65c77c2 100644 --- a/crates/nu-command/src/filters/flatten.rs +++ b/crates/nu-command/src/filters/flatten.rs @@ -44,22 +44,61 @@ impl Command for Flatten { Example { description: "flatten a table", example: "[[N, u, s, h, e, l, l]] | flatten ", - result: None + result: Some(Value::List { + vals: vec![ + Value::test_string("N"), + Value::test_string("u"), + Value::test_string("s"), + Value::test_string("h"), + Value::test_string("e"), + Value::test_string("l"), + Value::test_string("l")], + span: Span::test_data() + }) }, Example { description: "flatten a table, get the first item", example: "[[N, u, s, h, e, l, l]] | flatten | first", - result: None, + result: None,//Some(Value::test_string("N")), }, Example { description: "flatten a column having a nested table", example: "[[origin, people]; [Ecuador, ([[name, meal]; ['Andres', 'arepa']])]] | flatten | get meal", - result: None, + result: None,//Some(Value::test_string("arepa")), }, Example { description: "restrict the flattening by passing column names", example: "[[origin, crate, versions]; [World, ([[name]; ['nu-cli']]), ['0.21', '0.22']]] | flatten versions | last | get versions", - result: None, + result: None, //Some(Value::test_string("0.22")), + }, + Example { + description: "Flatten inner table", + example: "{ a: b, d: [ 1 2 3 4 ], e: [ 4 3 ] } | flatten", + result: Some(Value::List{ + vals: vec![ + Value::Record{ + cols: vec!["a".to_string(), "d".to_string(), "e".to_string()], + vals: vec![Value::test_string("b"), Value::test_int(1), Value::List{vals: vec![Value::test_int(4), Value::test_int(3)], span: Span::test_data()} ], + span: Span::test_data() + }, + Value::Record{ + cols: vec!["a".to_string(), "d".to_string(), "e".to_string()], + vals: vec![Value::test_string("b"), Value::test_int(2), Value::List{vals: vec![Value::test_int(4), Value::test_int(3)], span: Span::test_data()} ], + span: Span::test_data() + }, + Value::Record{ + cols: vec!["a".to_string(), "d".to_string(), "e".to_string()], + vals: vec![Value::test_string("b"), Value::test_int(3), Value::List{vals: vec![Value::test_int(4), Value::test_int(3)], span: Span::test_data()} ], + span: Span::test_data() + }, + Value::Record{ + cols: vec!["a".to_string(), "d".to_string(), "e".to_string()], + vals: vec![Value::test_string("b"), Value::test_int(4), Value::List{vals: vec![Value::test_int(4), Value::test_int(3)], span: Span::test_data()} ], + span: Span::test_data() + } + ], + span: Span::test_data(), + }), } ] } @@ -84,13 +123,6 @@ enum TableInside<'a> { Entries(&'a str, &'a Span, Vec<&'a Value>), } -// fn is_table(value: &Value) -> bool { -// match value { -// Value::List { vals, span: _ } => vals.iter().all(|f| f.as_record().is_ok()), -// _ => false, -// } -// } - fn flat_value(columns: &[CellPath], item: &Value, _name_tag: Span) -> Vec { let tag = match item.span() { Ok(x) => x, @@ -100,7 +132,7 @@ fn flat_value(columns: &[CellPath], item: &Value, _name_tag: Span) -> Vec let res = { if item.as_record().is_ok() { let mut out = IndexMap::::new(); - let mut a_table = None; + let mut inner_table = None; let mut tables_explicitly_flattened = 0; let records = match item { @@ -124,7 +156,17 @@ fn flat_value(columns: &[CellPath], item: &Value, _name_tag: Span) -> Vec Err(e) => return vec![Value::Error { error: e }], }; - for (column, value) in records.0.iter().zip(records.1.iter()) { + let records_iterator = { + let cols = records.0; + let vals = records.1; + + let mut pairs = vec![]; + for i in 0..cols.len() { + pairs.push((cols[i].as_str(), &vals[i])); + } + pairs + }; + for (column, value) in records_iterator { let column_requested = columns.iter().find(|c| c.into_string() == *column); match value { @@ -159,13 +201,10 @@ fn flat_value(columns: &[CellPath], item: &Value, _name_tag: Span) -> Vec } } } - Value::List { vals: _, span: _ } => { - let vals = if let Value::List { vals, span: _ } = value { - vals.iter().collect::>() - } else { - vec![] - }; - + Value::List { + vals: values, + span: _, + } => { if tables_explicitly_flattened >= 1 && column_requested.is_some() { return vec![Value::Error{ error: ShellError::UnsupportedInput( "can only flatten one inner table at the same time. tried flattening more than one column with inner tables... but is flattened already".to_string(), @@ -186,10 +225,10 @@ fn flat_value(columns: &[CellPath], item: &Value, _name_tag: Span) -> Vec if let Some(r) = cell_path { if !columns.is_empty() { - a_table = Some(TableInside::Entries( + inner_table = Some(TableInside::Entries( r, &s, - vals.into_iter().collect::>(), + values.iter().collect::>(), )); tables_explicitly_flattened += 1; @@ -197,12 +236,15 @@ fn flat_value(columns: &[CellPath], item: &Value, _name_tag: Span) -> Vec } else { out.insert(column.to_string(), value.clone()); } - } else if a_table.is_none() { - a_table = Some(TableInside::Entries( + } else if inner_table.is_none() { + inner_table = Some(TableInside::Entries( column, &s, - vals.into_iter().collect::>(), - )) + values.iter().collect::>(), + )); + out.insert(column.to_string(), value.clone()); + } else { + out.insert(column.to_string(), value.clone()); } } _ => { @@ -213,24 +255,25 @@ fn flat_value(columns: &[CellPath], item: &Value, _name_tag: Span) -> Vec let mut expanded = vec![]; - if let Some(TableInside::Entries(column, _, entries)) = a_table { + if let Some(TableInside::Entries(column, _, entries)) = inner_table { for entry in entries { let mut base = out.clone(); + base.insert(column.to_string(), entry.clone()); - let r = Value::Record { + let record = Value::Record { cols: base.keys().map(|f| f.to_string()).collect::>(), vals: base.values().cloned().collect(), span: tag, }; - expanded.push(r); + expanded.push(record); } } else { - let r = Value::Record { + let record = Value::Record { cols: out.keys().map(|f| f.to_string()).collect::>(), vals: out.values().cloned().collect(), span: tag, }; - expanded.push(r); + expanded.push(record); } expanded } else if item.as_list().is_ok() {