From 9382dd6d55d389ff9fc6da9e0e3a86767c9d57a3 Mon Sep 17 00:00:00 2001 From: Access Date: Sat, 31 Dec 2022 19:19:10 +0800 Subject: [PATCH] fix: empty cell in select (#7639) --- crates/nu-command/src/filters/select.rs | 22 ++++++++++++++-------- crates/nu-command/tests/commands/select.rs | 6 +++--- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/crates/nu-command/src/filters/select.rs b/crates/nu-command/src/filters/select.rs index cf7772676..d48e6d27d 100644 --- a/crates/nu-command/src/filters/select.rs +++ b/crates/nu-command/src/filters/select.rs @@ -149,7 +149,7 @@ fn select( ) => { let mut output = vec![]; let mut columns_with_value = Vec::new(); - + let mut allempty = true; for input_val in input_vals { if !columns.is_empty() { let mut cols = vec![]; @@ -158,6 +158,7 @@ fn select( //FIXME: improve implementation to not clone match input_val.clone().follow_cell_path(&path.members, false) { Ok(fetcher) => { + allempty = false; cols.push(path.into_string().replace('.', "_")); vals.push(fetcher); if !columns_with_value.contains(&path) { @@ -166,9 +167,11 @@ fn select( } Err(e) => { if ignore_errors { - return Ok(Value::nothing(call_span).into_pipeline_data()); + cols.push(path.into_string().replace('.', "_")); + vals.push(Value::Nothing { span }) + } else { + return Err(e); } - return Err(e); } } } @@ -178,11 +181,14 @@ fn select( output.push(input_val) } } - - Ok(output - .into_iter() - .into_pipeline_data(engine_state.ctrlc.clone()) - .set_metadata(metadata)) + if allempty { + Ok(Value::nothing(call_span).into_pipeline_data()) + } else { + Ok(output + .into_iter() + .into_pipeline_data(engine_state.ctrlc.clone()) + .set_metadata(metadata)) + } } PipelineData::ListStream(stream, metadata, ..) => { let mut values = vec![]; diff --git a/crates/nu-command/tests/commands/select.rs b/crates/nu-command/tests/commands/select.rs index 1f20f24d6..957fb5748 100644 --- a/crates/nu-command/tests/commands/select.rs +++ b/crates/nu-command/tests/commands/select.rs @@ -165,11 +165,11 @@ fn select_ignores_errors_succesfully1() { let actual = nu!( cwd: ".", pipeline( r#" - [{a: 1, b: 2} {a: 3, b: 5} {a: 3}] | select -i b - "# + [{a: 1, b: 2} {a: 3, b: 5} {a: 3}] | select -i b | length + "# )); - assert!(actual.out.is_empty()); + assert_eq!(actual.out, "3".to_string()); assert!(actual.err.is_empty()); }