forked from extern/nushell
Optional members in cell paths: Attempt 2 (#8379)
This is a follow up from https://github.com/nushell/nushell/pull/7540. Please provide feedback if you have the time! ## Summary This PR lets you use `?` to indicate that a member in a cell path is optional and Nushell should return `null` if that member cannot be accessed. Unlike the previous PR, `?` is now a _postfix_ modifier for cell path members. A cell path of `.foo?.bar` means that `foo` is optional and `bar` is not. `?` does _not_ suppress all errors; it is intended to help in situations where data has "holes", i.e. the data types are correct but something is missing. Type mismatches (like trying to do a string path access on a date) will still fail. ### Record Examples ```bash { foo: 123 }.foo # returns 123 { foo: 123 }.bar # errors { foo: 123 }.bar? # returns null { foo: 123 } | get bar # errors { foo: 123 } | get bar? # returns null { foo: 123 }.bar.baz # errors { foo: 123 }.bar?.baz # errors because `baz` is not present on the result from `bar?` { foo: 123 }.bar.baz? # errors { foo: 123 }.bar?.baz? # returns null ``` ### List Examples ``` 〉[{foo: 1} {foo: 2} {}].foo Error: nu:🐚:column_not_found × Cannot find column ╭─[entry #30:1:1] 1 │ [{foo: 1} {foo: 2} {}].foo · ─┬ ─┬─ · │ ╰── cannot find column 'foo' · ╰── value originates here ╰──── 〉[{foo: 1} {foo: 2} {}].foo? ╭───┬───╮ │ 0 │ 1 │ │ 1 │ 2 │ │ 2 │ │ ╰───┴───╯ 〉[{foo: 1} {foo: 2} {}].foo?.2 | describe nothing 〉[a b c].4? | describe nothing 〉[{foo: 1} {foo: 2} {}] | where foo? == 1 ╭───┬─────╮ │ # │ foo │ ├───┼─────┤ │ 0 │ 1 │ ╰───┴─────╯ ``` # Breaking changes 1. Column names with `?` in them now need to be quoted. 2. The `-i`/`--ignore-errors` flag has been removed from `get` and `select` 1. After this PR, most `get` error handling can be done with `?` and/or `try`/`catch`. 4. Cell path accesses like this no longer work without a `?`: ```bash 〉[{a:1 b:2} {a:3}].b.0 2 ``` We had some clever code that was able to recognize that since we only want row `0`, it's OK if other rows are missing column `b`. I removed that because it's tricky to maintain, and now that query needs to be written like: ```bash 〉[{a:1 b:2} {a:3}].b?.0 2 ``` I think the regression is acceptable for now. I plan to do more work in the future to enable streaming of cell path accesses, and when that happens I'll be able to make `.b.0` work again.
This commit is contained in:
@ -71,7 +71,7 @@ fn cal_sees_pipeline_year() {
|
||||
let actual = nu!(
|
||||
cwd: ".", pipeline(
|
||||
r#"
|
||||
cal --full-year 1020 | get -i monday | first 4 | to json -r
|
||||
cal --full-year 1020 | get monday | first 4 | to json -r
|
||||
"#
|
||||
));
|
||||
|
||||
|
@ -23,7 +23,7 @@ fn regular_columns() {
|
||||
#[test]
|
||||
fn skip_cell_rejection() {
|
||||
let actual = nu!(cwd: ".", pipeline(
|
||||
r#"[ {a: 1, b: 2,c:txt}, { a:val } ] | reject a | get c.0"#));
|
||||
r#"[ {a: 1, b: 2,c:txt}, { a:val } ] | reject a | get c?.0"#));
|
||||
|
||||
assert_eq!(actual.out, "txt");
|
||||
}
|
||||
|
@ -165,7 +165,7 @@ fn select_ignores_errors_successfully1() {
|
||||
let actual = nu!(
|
||||
cwd: ".", pipeline(
|
||||
r#"
|
||||
[{a: 1, b: 2} {a: 3, b: 5} {a: 3}] | select -i b | length
|
||||
[{a: 1, b: 2} {a: 3, b: 5} {a: 3}] | select b? | length
|
||||
"#
|
||||
));
|
||||
|
||||
@ -178,7 +178,7 @@ fn select_ignores_errors_successfully2() {
|
||||
let actual = nu!(
|
||||
cwd: ".", pipeline(
|
||||
r#"
|
||||
[{a: 1} {a: 2} {a: 3}] | select -i b | to nuon
|
||||
[{a: 1} {a: 2} {a: 3}] | select b? | to nuon
|
||||
"#
|
||||
));
|
||||
|
||||
@ -190,7 +190,7 @@ fn select_ignores_errors_successfully2() {
|
||||
fn select_ignores_errors_successfully3() {
|
||||
let actual = nu!(
|
||||
cwd: ".", pipeline(
|
||||
r#"sys | select -i invalid_key | to nuon"#
|
||||
r#"sys | select invalid_key? | to nuon"#
|
||||
));
|
||||
|
||||
assert_eq!(actual.out, "{invalid_key: null}".to_string());
|
||||
@ -201,38 +201,10 @@ fn select_ignores_errors_successfully3() {
|
||||
fn select_ignores_errors_successfully4() {
|
||||
let actual = nu!(
|
||||
cwd: ".", pipeline(
|
||||
r#"[a b c] | select -i invalid_key | to nuon"#
|
||||
r#""key val\na 1\nb 2\n" | lines | split column -c " " | select foo? | to nuon"#
|
||||
));
|
||||
|
||||
assert_eq!(
|
||||
actual.out,
|
||||
"[[invalid_key]; [null], [null], [null]]".to_string()
|
||||
);
|
||||
assert!(actual.err.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn select_ignores_errors_successfully5() {
|
||||
let actual = nu!(
|
||||
cwd: ".", pipeline(
|
||||
r#"[a b c] | select -i 0.0"#
|
||||
));
|
||||
|
||||
assert!(actual.out.is_empty());
|
||||
assert!(actual.err.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn select_ignores_errors_successfully6() {
|
||||
let actual = nu!(
|
||||
cwd: ".", pipeline(
|
||||
r#""key val\na 1\nb 2\n" | lines | split column -c " " | select -i "100" | to nuon"#
|
||||
));
|
||||
|
||||
assert_eq!(
|
||||
actual.out,
|
||||
r#"[["100"]; [null], [null], [null]]"#.to_string()
|
||||
);
|
||||
assert_eq!(actual.out, r#"[[foo]; [null], [null], [null]]"#.to_string());
|
||||
assert!(actual.err.is_empty());
|
||||
}
|
||||
|
||||
|
@ -15,7 +15,7 @@ fn filters_by_unit_size_comparison() {
|
||||
fn filters_with_nothing_comparison() {
|
||||
let actual = nu!(
|
||||
cwd: "tests/fixtures/formats",
|
||||
r#"'[{"foo": 3}, {"foo": null}, {"foo": 4}]' | from json | get -i foo | compact | where $it > 1 | math sum"#
|
||||
r#"'[{"foo": 3}, {"foo": null}, {"foo": 4}]' | from json | get foo | compact | where $it > 1 | math sum"#
|
||||
);
|
||||
|
||||
assert_eq!(actual.out, "7");
|
||||
|
Reference in New Issue
Block a user