mirror of
https://github.com/nushell/nushell.git
synced 2025-04-25 21:58:18 +02:00
Flatten rows containing same sub-table columns with distinct column names. (#2684)
This commit is contained in:
parent
b6d19cc9fa
commit
5725e55abb
@ -100,12 +100,20 @@ fn flat_value(
|
|||||||
} = value
|
} = value
|
||||||
{
|
{
|
||||||
if column_requested.is_none() && !columns.is_empty() {
|
if column_requested.is_none() && !columns.is_empty() {
|
||||||
out.insert_value(column, value.clone());
|
if out.contains_key(&column) {
|
||||||
|
out.insert_value(format!("{}_{}", column, column), value.clone());
|
||||||
|
} else {
|
||||||
|
out.insert_value(column, value.clone());
|
||||||
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (k, v) in mapa.into_iter() {
|
for (k, v) in mapa.into_iter() {
|
||||||
out.insert_value(k, v.clone());
|
if out.contains_key(k) {
|
||||||
|
out.insert_value(format!("{}_{}", column, k), v.clone());
|
||||||
|
} else {
|
||||||
|
out.insert_value(k, v.clone());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else if value.is_table() {
|
} else if value.is_table() {
|
||||||
if tables_explicitly_flattened >= 1 && column_requested.is_some() {
|
if tables_explicitly_flattened >= 1 && column_requested.is_some() {
|
||||||
|
@ -54,24 +54,16 @@ fn flatten_row_column_explictly() {
|
|||||||
r#"
|
r#"
|
||||||
[
|
[
|
||||||
{
|
{
|
||||||
"origin": "Ecuador",
|
|
||||||
"people": {
|
"people": {
|
||||||
"name": "Andres",
|
"name": "Andres",
|
||||||
"meal": "arepa"
|
"meal": "arepa"
|
||||||
},
|
}
|
||||||
"code": { "id": 1, "references": 2},
|
|
||||||
"tags": ["carbohydrate", "corn", "maiz"],
|
|
||||||
"city": ["Guayaquil", "Samborondón"]
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"origin": "USA",
|
|
||||||
"people": {
|
"people": {
|
||||||
"name": "Katz",
|
"name": "Katz",
|
||||||
"meal": "nurepa"
|
"meal": "nurepa"
|
||||||
},
|
}
|
||||||
"code": { "id": 2, "references": 1},
|
|
||||||
"tags": ["carbohydrate", "shell food", "amigos flavor"],
|
|
||||||
"city": ["Oregon", "Brooklin"]
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
"#,
|
"#,
|
||||||
@ -87,30 +79,58 @@ fn flatten_row_column_explictly() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn flatten_table_columns_explictly() {
|
fn flatten_row_columns_having_same_column_names_flats_separately() {
|
||||||
Playground::setup("flatten_test_2", |dirs, sandbox| {
|
Playground::setup("flatten_test_2", |dirs, sandbox| {
|
||||||
sandbox.with_files(vec![FileWithContentToBeTrimmed(
|
sandbox.with_files(vec![FileWithContentToBeTrimmed(
|
||||||
"katz.json",
|
"katz.json",
|
||||||
r#"
|
r#"
|
||||||
[
|
[
|
||||||
{
|
{
|
||||||
"origin": "Ecuador",
|
|
||||||
"people": {
|
"people": {
|
||||||
"name": "Andres",
|
"name": "Andres",
|
||||||
"meal": "arepa"
|
"meal": "arepa"
|
||||||
},
|
},
|
||||||
"code": { "id": 1, "references": 2},
|
"city": [{"name": "Guayaquil"}, {"name": "Samborondón"}]
|
||||||
"tags": ["carbohydrate", "corn", "maiz"],
|
},
|
||||||
"city": ["Guayaquil", "Samborondón"]
|
{
|
||||||
|
"people": {
|
||||||
|
"name": "Katz",
|
||||||
|
"meal": "nurepa"
|
||||||
|
},
|
||||||
|
"city": [{"name": "Oregon"}, {"name": "Brooklin"}]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
"#,
|
||||||
|
)]);
|
||||||
|
|
||||||
|
let actual = nu!(
|
||||||
|
cwd: dirs.test(),
|
||||||
|
"open katz.json | flatten | flatten people city | get city_name | count"
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(actual.out, "4");
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn flatten_table_columns_explictly() {
|
||||||
|
Playground::setup("flatten_test_3", |dirs, sandbox| {
|
||||||
|
sandbox.with_files(vec![FileWithContentToBeTrimmed(
|
||||||
|
"katz.json",
|
||||||
|
r#"
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"people": {
|
||||||
|
"name": "Andres",
|
||||||
|
"meal": "arepa"
|
||||||
|
},
|
||||||
|
"city": ["Guayaquil", "Samborondón"]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"origin": "USA",
|
|
||||||
"people": {
|
"people": {
|
||||||
"name": "Katz",
|
"name": "Katz",
|
||||||
"meal": "nurepa"
|
"meal": "nurepa"
|
||||||
},
|
},
|
||||||
"code": { "id": 2, "references": 1},
|
|
||||||
"tags": ["carbohydrate", "shell food", "amigos flavor"],
|
|
||||||
"city": ["Oregon", "Brooklin"]
|
"city": ["Oregon", "Brooklin"]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
@ -128,28 +148,24 @@ fn flatten_table_columns_explictly() {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn flatten_more_than_one_column_that_are_subtables_not_supported() {
|
fn flatten_more_than_one_column_that_are_subtables_not_supported() {
|
||||||
Playground::setup("flatten_test_3", |dirs, sandbox| {
|
Playground::setup("flatten_test_4", |dirs, sandbox| {
|
||||||
sandbox.with_files(vec![FileWithContentToBeTrimmed(
|
sandbox.with_files(vec![FileWithContentToBeTrimmed(
|
||||||
"katz.json",
|
"katz.json",
|
||||||
r#"
|
r#"
|
||||||
[
|
[
|
||||||
{
|
{
|
||||||
"origin": "Ecuador",
|
|
||||||
"people": {
|
"people": {
|
||||||
"name": "Andres",
|
"name": "Andres",
|
||||||
"meal": "arepa"
|
"meal": "arepa"
|
||||||
},
|
}
|
||||||
"code": { "id": 1, "references": 2},
|
|
||||||
"tags": ["carbohydrate", "corn", "maiz"],
|
"tags": ["carbohydrate", "corn", "maiz"],
|
||||||
"city": ["Guayaquil", "Samborondón"]
|
"city": ["Guayaquil", "Samborondón"]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"origin": "USA",
|
|
||||||
"people": {
|
"people": {
|
||||||
"name": "Katz",
|
"name": "Katz",
|
||||||
"meal": "nurepa"
|
"meal": "nurepa"
|
||||||
},
|
},
|
||||||
"code": { "id": 2, "references": 1},
|
|
||||||
"tags": ["carbohydrate", "shell food", "amigos flavor"],
|
"tags": ["carbohydrate", "shell food", "amigos flavor"],
|
||||||
"city": ["Oregon", "Brooklin"]
|
"city": ["Oregon", "Brooklin"]
|
||||||
}
|
}
|
||||||
|
@ -252,6 +252,11 @@ impl TaggedDictBuilder {
|
|||||||
pub fn is_empty(&self) -> bool {
|
pub fn is_empty(&self) -> bool {
|
||||||
self.dict.is_empty()
|
self.dict.is_empty()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Checks if given key exists
|
||||||
|
pub fn contains_key(&self, key: &str) -> bool {
|
||||||
|
self.dict.contains_key(key)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<TaggedDictBuilder> for Value {
|
impl From<TaggedDictBuilder> for Value {
|
||||||
|
Loading…
Reference in New Issue
Block a user