mirror of
https://github.com/nushell/nushell.git
synced 2024-12-30 10:59:11 +01:00
* Fix flatten's dropping column issue, and do some cleanup - better variable naming. * Fix failing test * Fix failing tests
This commit is contained in:
parent
b58aad5eb0
commit
afe83104c6
@ -44,22 +44,61 @@ impl Command for Flatten {
|
|||||||
Example {
|
Example {
|
||||||
description: "flatten a table",
|
description: "flatten a table",
|
||||||
example: "[[N, u, s, h, e, l, l]] | flatten ",
|
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 {
|
Example {
|
||||||
description: "flatten a table, get the first item",
|
description: "flatten a table, get the first item",
|
||||||
example: "[[N, u, s, h, e, l, l]] | flatten | first",
|
example: "[[N, u, s, h, e, l, l]] | flatten | first",
|
||||||
result: None,
|
result: None,//Some(Value::test_string("N")),
|
||||||
},
|
},
|
||||||
Example {
|
Example {
|
||||||
description: "flatten a column having a nested table",
|
description: "flatten a column having a nested table",
|
||||||
example: "[[origin, people]; [Ecuador, ([[name, meal]; ['Andres', 'arepa']])]] | flatten | get meal",
|
example: "[[origin, people]; [Ecuador, ([[name, meal]; ['Andres', 'arepa']])]] | flatten | get meal",
|
||||||
result: None,
|
result: None,//Some(Value::test_string("arepa")),
|
||||||
},
|
},
|
||||||
Example {
|
Example {
|
||||||
description: "restrict the flattening by passing column names",
|
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",
|
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>),
|
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<Value> {
|
fn flat_value(columns: &[CellPath], item: &Value, _name_tag: Span) -> Vec<Value> {
|
||||||
let tag = match item.span() {
|
let tag = match item.span() {
|
||||||
Ok(x) => x,
|
Ok(x) => x,
|
||||||
@ -100,7 +132,7 @@ fn flat_value(columns: &[CellPath], item: &Value, _name_tag: Span) -> Vec<Value>
|
|||||||
let res = {
|
let res = {
|
||||||
if item.as_record().is_ok() {
|
if item.as_record().is_ok() {
|
||||||
let mut out = IndexMap::<String, Value>::new();
|
let mut out = IndexMap::<String, Value>::new();
|
||||||
let mut a_table = None;
|
let mut inner_table = None;
|
||||||
let mut tables_explicitly_flattened = 0;
|
let mut tables_explicitly_flattened = 0;
|
||||||
|
|
||||||
let records = match item {
|
let records = match item {
|
||||||
@ -124,7 +156,17 @@ fn flat_value(columns: &[CellPath], item: &Value, _name_tag: Span) -> Vec<Value>
|
|||||||
Err(e) => return vec![Value::Error { error: e }],
|
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);
|
let column_requested = columns.iter().find(|c| c.into_string() == *column);
|
||||||
|
|
||||||
match value {
|
match value {
|
||||||
@ -159,13 +201,10 @@ fn flat_value(columns: &[CellPath], item: &Value, _name_tag: Span) -> Vec<Value>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Value::List { vals: _, span: _ } => {
|
Value::List {
|
||||||
let vals = if let Value::List { vals, span: _ } = value {
|
vals: values,
|
||||||
vals.iter().collect::<Vec<_>>()
|
span: _,
|
||||||
} else {
|
} => {
|
||||||
vec![]
|
|
||||||
};
|
|
||||||
|
|
||||||
if tables_explicitly_flattened >= 1 && column_requested.is_some() {
|
if tables_explicitly_flattened >= 1 && column_requested.is_some() {
|
||||||
return vec![Value::Error{ error: ShellError::UnsupportedInput(
|
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(),
|
"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<Value>
|
|||||||
|
|
||||||
if let Some(r) = cell_path {
|
if let Some(r) = cell_path {
|
||||||
if !columns.is_empty() {
|
if !columns.is_empty() {
|
||||||
a_table = Some(TableInside::Entries(
|
inner_table = Some(TableInside::Entries(
|
||||||
r,
|
r,
|
||||||
&s,
|
&s,
|
||||||
vals.into_iter().collect::<Vec<_>>(),
|
values.iter().collect::<Vec<_>>(),
|
||||||
));
|
));
|
||||||
|
|
||||||
tables_explicitly_flattened += 1;
|
tables_explicitly_flattened += 1;
|
||||||
@ -197,12 +236,15 @@ fn flat_value(columns: &[CellPath], item: &Value, _name_tag: Span) -> Vec<Value>
|
|||||||
} else {
|
} else {
|
||||||
out.insert(column.to_string(), value.clone());
|
out.insert(column.to_string(), value.clone());
|
||||||
}
|
}
|
||||||
} else if a_table.is_none() {
|
} else if inner_table.is_none() {
|
||||||
a_table = Some(TableInside::Entries(
|
inner_table = Some(TableInside::Entries(
|
||||||
column,
|
column,
|
||||||
&s,
|
&s,
|
||||||
vals.into_iter().collect::<Vec<_>>(),
|
values.iter().collect::<Vec<_>>(),
|
||||||
))
|
));
|
||||||
|
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<Value>
|
|||||||
|
|
||||||
let mut expanded = 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 {
|
for entry in entries {
|
||||||
let mut base = out.clone();
|
let mut base = out.clone();
|
||||||
|
|
||||||
base.insert(column.to_string(), entry.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::<Vec<_>>(),
|
cols: base.keys().map(|f| f.to_string()).collect::<Vec<_>>(),
|
||||||
vals: base.values().cloned().collect(),
|
vals: base.values().cloned().collect(),
|
||||||
span: tag,
|
span: tag,
|
||||||
};
|
};
|
||||||
expanded.push(r);
|
expanded.push(record);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let r = Value::Record {
|
let record = Value::Record {
|
||||||
cols: out.keys().map(|f| f.to_string()).collect::<Vec<_>>(),
|
cols: out.keys().map(|f| f.to_string()).collect::<Vec<_>>(),
|
||||||
vals: out.values().cloned().collect(),
|
vals: out.values().cloned().collect(),
|
||||||
span: tag,
|
span: tag,
|
||||||
};
|
};
|
||||||
expanded.push(r);
|
expanded.push(record);
|
||||||
}
|
}
|
||||||
expanded
|
expanded
|
||||||
} else if item.as_list().is_ok() {
|
} else if item.as_list().is_ok() {
|
||||||
|
Loading…
Reference in New Issue
Block a user