Skip to content

Into cell path - #8482

Closed
fdncred wants to merge 4 commits into
nushell:mainfrom
fdncred:into_cell_path
Closed

fdncred wants to merge 4 commits into
nushell:mainfrom
fdncred:into_cell_path

Conversation

@fdncred

@fdncred fdncred commented Mar 16, 2023 •

Copy link
Copy Markdown
Contributor

Description

This PR is an attempt to allow you to specify cell-paths as strings and convert them for usage.

Example 1 - Working now

> let a = {a: 1, b:{b1:21, b2:22}}
> let s = 'b.b1'
> $a | get b.b1
21
> $a | get $s 
Error: nu::shell::column_not_found

  × Cannot find column
   ╭─[entry #6:1:1]
 1 │ $a | get $s
   · ─┬       ─┬
   ·  │        ╰── cannot find column 'b.b1'
   ·  ╰── value originates here
   ╰────
> $a | get ($s | into cell-path)
21

Example 2 - Working now

> let data = {a: {b: {c: [1 2 3]}}}
> let target = 'a'
> $data | get $target
╭───┬──────────────────╮
│ b │ {record 1 field} │
╰───┴──────────────────╯
> let target = "a.b.c"
> $data | get $target
Error: nu::shell::column_not_found

  × Cannot find column
   ╭─[entry #12:1:1]
 1 │ $data | get $target
   · ──┬──       ───┬───
   ·   │            ╰── cannot find column 'a.b.c'
   ·   ╰── value originates here
   ╰────
> $data | get ($target | into cell-path)
╭───┬───╮
│ 0 │ 1 │
│ 1 │ 2 │
│ 2 │ 3 │
╰───┴───╯

Example 3 (still doesn't work - is '1' a cell-path in this example? The rest (a.b.c) are columns, but 1 is a row-index.)

> let target = "a.b.c.1"
> $data | get ($target | into cell-path)
Error: nu::shell::column_not_found

  × Cannot find column
   ╭─[entry #8:1:1]
 1 │ let data = {a: {b: {c: [1 2 3]}}}
   ·                         ┬
   ·                         ╰── value originates here
   ╰────
   ╭─[entry #19:1:1]
 1 │ $data | get ($target | into cell-path)
   ·                        ───────┬──────
   ·                               ╰── cannot find column '1'
   ╰────

closes #6597
closes #8466

User-Facing Changes

I totally missed this PR #7417 which apparently got reverted here #7523. Interesting. Looks like I'm doing much of the same but perhaps not as nice. :)

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

Note
from nushell you can also use the toolkit as follows

use toolkit.nu  # or use an `env_change` hook to activate it automatically
toolkit check pr

After Submitting

If your PR had any user-facing changes, update the documentation after the PR is merged, if necessary. This will help us keep the docs up to date.

@codecov

codecov Bot commented Mar 16, 2023 •

Copy link
Copy Markdown

Codecov Report

Merging #8482 (125abee) into main (4c97b3d) will increase coverage by 0.02%.
The diff coverage is 89.88%.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #8482      +/-   ##
==========================================
+ Coverage   68.43%   68.46%   +0.02%     
==========================================
  Files         621      622       +1     
  Lines       99938   100099     +161     
==========================================
+ Hits        68394    68531     +137     
- Misses      31544    31568      +24     
Impacted Files Coverage Δ
crates/nu-command/src/conversions/into/cellpath.rs 89.53% <89.53%> (ø)
crates/nu-command/src/default_context.rs 99.78% <100.00%> (+<0.01%) ⬆️
crates/nu-command/src/example_test.rs 100.00% <100.00%> (ø)

... and 6 files with indirect coverage changes

@sophiajt

Copy link
Copy Markdown
Contributor

This feels a bit like eval-lite :) Something I was wondering about is if we support things like $foo.$bar in the future and use $bar as where we get the path from, would it break in this case? Same as we make paths more advanced, like being able to add searches to them $foo.[name == 'bar*'] etc. Like it works fine for now, just thinking ahead a bit and wondering if future changes we do (like Riley's recent ? syntax) might make this one tricky.

@fdncred

fdncred commented Mar 16, 2023

Copy link
Copy Markdown
Contributor Author

I've been trying to get lex and parse_cell_path functions hooked up so the cell path can be parsed properly but I'm just running into dead ends everywhere.

@Dorumin

Dorumin commented Mar 16, 2023

Copy link
Copy Markdown
Contributor

Kubouch's comment here is relevant if you try to incorporate the parser at runtime on the string, it is very much eval-lite given you'd have to be able to follow variables at a minimum for it to be useful, and it gets worse when you get subexpressions like jt mentioned, so something like [name == $var] if it ever does land. I'm not super knowledgeable on nushell's parser, but that's the impression it left on me. Maybe into cellpath will have to be restricted to "static" paths that have all of their context inside the source string.

@fdncred

fdncred commented Mar 16, 2023

Copy link
Copy Markdown
Contributor Author

Looks like this won't be happening. IMO, it's helpful to users as was #7417. Others disagree. The best work-able solution I heard about it was somehow changing the parser to allow cell-path literals but I'm not sure that'll ever see the light of day either.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

allow accessing data with deep variable path get should accept cellpath specified as string

3 participants