From 2d07c6eedbf0e1c6dd25cc168d7c151d9cf4b303 Mon Sep 17 00:00:00 2001 From: pwygab <88221256+merelymyself@users.noreply.github.com> Date: Fri, 16 Dec 2022 14:03:38 +0800 Subject: [PATCH] ensure `get` doesn't delve too deep in nested lists (#7497) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Description Fixes #7494. ``` /home/gabriel/CodingProjects/nushell〉[[{foo: bar}]] | get foo 12/16/2022 12:31:17 PM Error: nu::parser::not_found (link) × Not found. ╭─[entry #1:1:1] 1 │ [[{foo: bar}]] | get foo · ───────┬────── · ╰── did not find anything under this name ╰──── ``` # User-Facing Changes cell paths no longer drill into nested tables. # Tests + Formatting Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass # After Submitting If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. --- crates/nu-command/tests/commands/get.rs | 10 ++++++++++ crates/nu-command/tests/commands/open.rs | 2 +- crates/nu-protocol/src/value/mod.rs | 1 + 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/crates/nu-command/tests/commands/get.rs b/crates/nu-command/tests/commands/get.rs index 693bd0967..c5eeea512 100644 --- a/crates/nu-command/tests/commands/get.rs +++ b/crates/nu-command/tests/commands/get.rs @@ -219,3 +219,13 @@ fn quoted_column_access() { assert_eq!(actual.out, "4"); } + +#[test] +fn get_does_not_delve_too_deep_in_nested_lists() { + let actual = nu!( + cwd: ".", + r#"[[{foo: bar}]] | get foo"# + ); + + assert!(actual.err.contains("did not find anything under this name")); +} diff --git a/crates/nu-command/tests/commands/open.rs b/crates/nu-command/tests/commands/open.rs index af5266cfc..5bce98f04 100644 --- a/crates/nu-command/tests/commands/open.rs +++ b/crates/nu-command/tests/commands/open.rs @@ -179,7 +179,7 @@ fn parses_json() { fn parses_xml() { let actual = nu!( cwd: "tests/fixtures/formats", - "open jonathan.xml | get rss.children.channel.children | get item.children | get link.children.0.3.3.0" + "open jonathan.xml | get rss.children.channel.children | get 0.item.children | get 0.link.children.0.0" ); assert_eq!( diff --git a/crates/nu-protocol/src/value/mod.rs b/crates/nu-protocol/src/value/mod.rs index d1070cbd5..110d62734 100644 --- a/crates/nu-protocol/src/value/mod.rs +++ b/crates/nu-protocol/src/value/mod.rs @@ -717,6 +717,7 @@ impl Value { let mut output = vec![]; let mut hasvalue = false; let mut temp: Result = Err(ShellError::NotFound(*span)); + let vals = vals.iter().filter(|v| matches!(v, Value::Record { .. })); for val in vals { temp = val.clone().follow_cell_path( &[PathMember::String {