mirror of
https://github.com/nushell/nushell.git
synced 2025-06-20 18:08:36 +02:00
Add default --empty
to handle empty values (#15223)
# Description Adds a new `--empty/-e` flag to the `default` command. # User-Facing Changes Before: ```nushell $env.FOO = "" $env.FOO = $env.FOO? | default bar $env.FOO # => Empty string ``` After: ```nushell $env.FOO = "" $env.FOO = $env.FOO? | default -e bar $env.FOO # => bar ``` * Uses `val.is_empty`, which means that empty lists and records are also replaced * Empty values in tables (with a column specifier) are also replaced. # Tests + Formatting 7 tests added and 1 updated + 1 new example - 🟢 `toolkit fmt` - 🟢 `toolkit clippy` - 🟢 `toolkit test` - 🟢 `toolkit test stdlib` # After Submitting N/A
This commit is contained in:
parent
0f6996b70d
commit
029f3843d3
@ -23,6 +23,11 @@ impl Command for Default {
|
|||||||
SyntaxShape::String,
|
SyntaxShape::String,
|
||||||
"The name of the column.",
|
"The name of the column.",
|
||||||
)
|
)
|
||||||
|
.switch(
|
||||||
|
"empty",
|
||||||
|
"also replace empty items like \"\", {}, and []",
|
||||||
|
Some('e'),
|
||||||
|
)
|
||||||
.category(Category::Filters)
|
.category(Category::Filters)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -37,7 +42,8 @@ impl Command for Default {
|
|||||||
call: &Call,
|
call: &Call,
|
||||||
input: PipelineData,
|
input: PipelineData,
|
||||||
) -> Result<PipelineData, ShellError> {
|
) -> Result<PipelineData, ShellError> {
|
||||||
default(engine_state, stack, call, input)
|
let empty = call.has_flag(engine_state, stack, "empty")?;
|
||||||
|
default(engine_state, stack, call, input, empty)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn examples(&self) -> Vec<Example> {
|
fn examples(&self) -> Vec<Example> {
|
||||||
@ -80,6 +86,20 @@ impl Command for Default {
|
|||||||
}),
|
}),
|
||||||
])),
|
])),
|
||||||
},
|
},
|
||||||
|
Example {
|
||||||
|
description: r#"Replace the empty string in the "a" column of a list"#,
|
||||||
|
example: "[{a:1 b:2} {a:'' b:1}] | default -e 'N/A' a",
|
||||||
|
result: Some(Value::test_list(vec![
|
||||||
|
Value::test_record(record! {
|
||||||
|
"a" => Value::test_int(1),
|
||||||
|
"b" => Value::test_int(2),
|
||||||
|
}),
|
||||||
|
Value::test_record(record! {
|
||||||
|
"a" => Value::test_string("N/A"),
|
||||||
|
"b" => Value::test_int(1),
|
||||||
|
}),
|
||||||
|
])),
|
||||||
|
},
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -89,6 +109,7 @@ fn default(
|
|||||||
stack: &mut Stack,
|
stack: &mut Stack,
|
||||||
call: &Call,
|
call: &Call,
|
||||||
input: PipelineData,
|
input: PipelineData,
|
||||||
|
default_when_empty: bool,
|
||||||
) -> Result<PipelineData, ShellError> {
|
) -> Result<PipelineData, ShellError> {
|
||||||
let metadata = input.metadata();
|
let metadata = input.metadata();
|
||||||
let value: Value = call.req(engine_state, stack, 0)?;
|
let value: Value = call.req(engine_state, stack, 0)?;
|
||||||
@ -104,7 +125,9 @@ fn default(
|
|||||||
} => {
|
} => {
|
||||||
let record = record.to_mut();
|
let record = record.to_mut();
|
||||||
if let Some(val) = record.get_mut(&column.item) {
|
if let Some(val) = record.get_mut(&column.item) {
|
||||||
if matches!(val, Value::Nothing { .. }) {
|
if matches!(val, Value::Nothing { .. })
|
||||||
|
|| (default_when_empty && val.is_empty())
|
||||||
|
{
|
||||||
*val = value.clone();
|
*val = value.clone();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -118,7 +141,10 @@ fn default(
|
|||||||
engine_state.signals(),
|
engine_state.signals(),
|
||||||
)
|
)
|
||||||
.map(|x| x.set_metadata(metadata))
|
.map(|x| x.set_metadata(metadata))
|
||||||
} else if input.is_nothing() {
|
} else if input.is_nothing()
|
||||||
|
|| (default_when_empty
|
||||||
|
&& matches!(input, PipelineData::Value(ref value, _) if value.is_empty()))
|
||||||
|
{
|
||||||
Ok(value.into_pipeline_data())
|
Ok(value.into_pipeline_data())
|
||||||
} else {
|
} else {
|
||||||
Ok(input)
|
Ok(input)
|
||||||
|
@ -8,7 +8,10 @@ fn adds_row_data_if_column_missing() {
|
|||||||
{"name": "Yehuda"},
|
{"name": "Yehuda"},
|
||||||
{"name": "JT", "rusty_luck": 0},
|
{"name": "JT", "rusty_luck": 0},
|
||||||
{"name": "Andres", "rusty_luck": 0},
|
{"name": "Andres", "rusty_luck": 0},
|
||||||
{"name":"GorbyPuff"}
|
{"name": "Michael", "rusty_luck": []},
|
||||||
|
{"name": "Darren", "rusty_luck": {}},
|
||||||
|
{"name": "Stefan", "rusty_luck": ""},
|
||||||
|
{"name": "GorbyPuff"}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
"#;
|
"#;
|
||||||
@ -44,3 +47,68 @@ fn replaces_null() {
|
|||||||
let actual = nu!(r#"null | default 1"#);
|
let actual = nu!(r#"null | default 1"#);
|
||||||
assert_eq!(actual.out, "1");
|
assert_eq!(actual.out, "1");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn adds_row_data_if_column_missing_or_empty() {
|
||||||
|
let sample = r#"
|
||||||
|
{
|
||||||
|
"amigos": [
|
||||||
|
{"name": "Yehuda"},
|
||||||
|
{"name": "JT", "rusty_luck": 0},
|
||||||
|
{"name": "Andres", "rusty_luck": 0},
|
||||||
|
{"name": "Michael", "rusty_luck": []},
|
||||||
|
{"name": "Darren", "rusty_luck": {}},
|
||||||
|
{"name": "Stefan", "rusty_luck": ""},
|
||||||
|
{"name": "GorbyPuff"}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
"#;
|
||||||
|
|
||||||
|
let actual = nu!(pipeline(&format!(
|
||||||
|
"
|
||||||
|
{sample}
|
||||||
|
| get amigos
|
||||||
|
| default -e 1 rusty_luck
|
||||||
|
| where rusty_luck == 1
|
||||||
|
| length
|
||||||
|
"
|
||||||
|
)));
|
||||||
|
|
||||||
|
assert_eq!(actual.out, "5");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn replace_empty_string() {
|
||||||
|
let actual = nu!(r#"'' | default -e foo"#);
|
||||||
|
assert_eq!(actual.out, "foo");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn do_not_replace_empty_string() {
|
||||||
|
let actual = nu!(r#"'' | default 1"#);
|
||||||
|
assert_eq!(actual.out, "");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn replace_empty_list() {
|
||||||
|
let actual = nu!(r#"[] | default -e foo"#);
|
||||||
|
assert_eq!(actual.out, "foo");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn do_not_replace_empty_list() {
|
||||||
|
let actual = nu!(r#"[] | default 1 | length"#);
|
||||||
|
assert_eq!(actual.out, "0");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn replace_empty_record() {
|
||||||
|
let actual = nu!(r#"{} | default -e foo"#);
|
||||||
|
assert_eq!(actual.out, "foo");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn do_not_replace_empty_record() {
|
||||||
|
let actual = nu!(r#"{} | default {a:5} | columns | length"#);
|
||||||
|
assert_eq!(actual.out, "0");
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user