mirror of
https://github.com/nushell/nushell.git
synced 2025-06-30 14:40:06 +02:00
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:
@ -1,6 +1,7 @@
|
||||
use nu_parser::ParseError;
|
||||
use nu_parser::*;
|
||||
use nu_protocol::ast::Call;
|
||||
use nu_protocol::ast::{Call, PathMember};
|
||||
use nu_protocol::Span;
|
||||
use nu_protocol::{
|
||||
ast::{Expr, Expression, PipelineElement},
|
||||
engine::{Command, EngineState, Stack, StateWorkingSet},
|
||||
@ -321,6 +322,118 @@ pub fn parse_int_with_underscores() {
|
||||
))
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn parse_cell_path() {
|
||||
let engine_state = EngineState::new();
|
||||
let mut working_set = StateWorkingSet::new(&engine_state);
|
||||
|
||||
working_set.add_variable(
|
||||
"foo".to_string().into_bytes(),
|
||||
Span::test_data(),
|
||||
nu_protocol::Type::Record(vec![]),
|
||||
false,
|
||||
);
|
||||
|
||||
let (block, err) = parse(&mut working_set, None, b"$foo.bar.baz", true, &[]);
|
||||
|
||||
assert!(err.is_none());
|
||||
assert_eq!(block.len(), 1);
|
||||
let expressions = &block[0];
|
||||
assert_eq!(expressions.len(), 1);
|
||||
|
||||
// hoo boy this pattern matching is a pain
|
||||
if let PipelineElement::Expression(_, expr) = &expressions[0] {
|
||||
if let Expr::FullCellPath(b) = &expr.expr {
|
||||
assert!(matches!(
|
||||
b.head,
|
||||
Expression {
|
||||
expr: Expr::Var(_),
|
||||
..
|
||||
}
|
||||
));
|
||||
if let [a, b] = &b.tail[..] {
|
||||
if let PathMember::String { val, optional, .. } = a {
|
||||
assert_eq!(val, "bar");
|
||||
assert_eq!(optional, &false);
|
||||
} else {
|
||||
panic!("wrong type")
|
||||
}
|
||||
|
||||
if let PathMember::String { val, optional, .. } = b {
|
||||
assert_eq!(val, "baz");
|
||||
assert_eq!(optional, &false);
|
||||
} else {
|
||||
panic!("wrong type")
|
||||
}
|
||||
} else {
|
||||
panic!("cell path tail is unexpected")
|
||||
}
|
||||
} else {
|
||||
panic!("Not a cell path");
|
||||
}
|
||||
} else {
|
||||
panic!("Not an expression")
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn parse_cell_path_optional() {
|
||||
let engine_state = EngineState::new();
|
||||
let mut working_set = StateWorkingSet::new(&engine_state);
|
||||
|
||||
working_set.add_variable(
|
||||
"foo".to_string().into_bytes(),
|
||||
Span::test_data(),
|
||||
nu_protocol::Type::Record(vec![]),
|
||||
false,
|
||||
);
|
||||
|
||||
let (block, err) = parse(&mut working_set, None, b"$foo.bar?.baz", true, &[]);
|
||||
|
||||
if let Some(err) = err {
|
||||
dbg!(err);
|
||||
panic!();
|
||||
}
|
||||
|
||||
assert_eq!(block.len(), 1);
|
||||
let expressions = &block[0];
|
||||
assert_eq!(expressions.len(), 1);
|
||||
|
||||
// hoo boy this pattern matching is a pain
|
||||
if let PipelineElement::Expression(_, expr) = &expressions[0] {
|
||||
if let Expr::FullCellPath(b) = &expr.expr {
|
||||
assert!(matches!(
|
||||
b.head,
|
||||
Expression {
|
||||
expr: Expr::Var(_),
|
||||
..
|
||||
}
|
||||
));
|
||||
if let [a, b] = &b.tail[..] {
|
||||
if let PathMember::String { val, optional, .. } = a {
|
||||
assert_eq!(val, "bar");
|
||||
assert_eq!(optional, &true);
|
||||
} else {
|
||||
panic!("wrong type")
|
||||
}
|
||||
|
||||
if let PathMember::String { val, optional, .. } = b {
|
||||
assert_eq!(val, "baz");
|
||||
assert_eq!(optional, &false);
|
||||
} else {
|
||||
panic!("wrong type")
|
||||
}
|
||||
} else {
|
||||
panic!("cell path tail is unexpected")
|
||||
}
|
||||
} else {
|
||||
panic!("Not a cell path");
|
||||
}
|
||||
} else {
|
||||
panic!("Not an expression")
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn parse_binary_with_hex_format() {
|
||||
let engine_state = EngineState::new();
|
||||
|
Reference in New Issue
Block a user