Repository navigation
Add into cell-path for dynamic cell-path creation - #11322
Conversation
|
Reminds me of this one #8482 I vote yes to this, just like I did last time. |
|
Looks like your example 3 from #8482 works now (probably not due to anything in this PR): let data = {a: {b: {c: [1 2 3]}}}
let target = "a.b.c.1" | into cell-path
$data | get $target
# => 2 |
|
The problem is that it's essentially doing eval, that's why the two previous PRs were closed/reverted (see fdncred's link, from there you can find the second PR). I'm not sure we'll be able to accept the PR since we didn't the two previous ones implementing the same thing. What might be the preferred way is extending the cell path syntax to allow things like |
Would this be acceptable without using 'some.structure.path' | split row '.' | into cell-pathFrom #8482 I see:
With subexpressions currently absent would making a new In a future where cell-path subexpressions exist would using |
|
I could imagine that That said, I was hoping we could use the cell-path literals and just use those as values without resorting to strings. That gives us something that's a bit more self-documenting. There are also some plans in the future to extend cell-paths to do more, like glob matching across cells. If we get too narrow with how cell-paths work now, then we might limit ourselves. I agree with the others who mentioned we should steer clear of anything that's like |
Implement PartialOrd for PathMember to ignore spans to allow comparisons
This allows creation of an arbitrary cell-path
d32e0de to
751fb65
Compare
|
I have updated this PR to remove converting a string into a cell-path. There is an example that shows how a user can do their own parsing to create a cell path with To (hopefully) reduce confusion passing a string to |
|
Thanks for working on this @drbrain. Sorry it took so long for us to come to a decision. |
# Description
The `cell-path` is a type that can be created statically with
`$.nested.structure.5`, but can't be created from user input. This makes
it difficult to take advantage of commands that accept a cell-path to
operate on data structures.
This PR adds `into cell-path` for dynamic cell-path creation.
`into cell-path` accepts the following input shapes:
* Bare integer (equivalent to `$.1`)
* List of strings and integers
* List of records with entries `value` and `optional`
* String (parsed into a cell-path)
## Example usage
An example of where `into cell-path` can be used is in working with `git
config --list`. The git configuration has a tree structure that maps
well to nushell records. With dynamic cell paths it is easy to convert
`git config list` to a record:
```nushell
git config --list
| lines
| parse -r '^(?<key>[^=]+)=(?<value>.*)'
| reduce --fold {} {|entry, result|
let path = $entry.key | into cell-path
$result
| upsert $path {||
$entry.value
}
}
| select remote
```
Output:
```
╭────────┬──────────────────────────────────────────────────────────────────╮
│ │ ╭──────────┬───────────────────────────────────────────────────╮ │
│ remote │ │ │ ╭───────┬───────────────────────────────────────╮ │ │
│ │ │ upstream │ │ url │ [email protected]:nushell/nushell.git │ │ │
│ │ │ │ │ fetch │ +refs/heads/*:refs/remotes/upstream/* │ │ │
│ │ │ │ ╰───────┴───────────────────────────────────────╯ │ │
│ │ │ │ ╭───────┬─────────────────────────────────────╮ │ │
│ │ │ origin │ │ url │ [email protected]:drbrain/nushell │ │ │
│ │ │ │ │ fetch │ +refs/heads/*:refs/remotes/origin/* │ │ │
│ │ │ │ ╰───────┴─────────────────────────────────────╯ │ │
│ │ ╰──────────┴───────────────────────────────────────────────────╯ │
╰────────┴──────────────────────────────────────────────────────────────────╯
```
## Errors
`lex()` + `parse_cell_path()` are forgiving about what is allowed in a
cell-path so it will allow what appears to be nonsense to become a
cell-path:
```nushell
let table = [["!@$%^&*" value]; [key value]]
$table | get ("!@$%^&*.0" | into cell-path)
# => key
```
But it will reject bad cell-paths:
```
❯ "a b" | into cell-path
Error: nu::shell::cant_convert
× Can't convert to cell-path.
╭─[entry nushell#14:1:1]
1 │ "a b" | into cell-path
· ───────┬──────
· ╰── can't convert string to cell-path
╰────
help: "a b" is not a valid cell-path (Parse mismatch during operation.)
```
# User-Facing Changes
New conversion command `into cell-path`
# Tests + Formatting
- 🟢 `toolkit fmt`
- 🟢 `toolkit clippy`
- 🟢 `toolkit test`
- 🟢 `toolkit test stdlib`
# After Submitting
Automatic documentation updates
Description
The
cell-pathis a type that can be created statically with$.nested.structure.5, but can't be created from user input. This makes it difficult to take advantage of commands that accept a cell-path to operate on data structures.This PR adds
into cell-pathfor dynamic cell-path creation.into cell-pathaccepts the following input shapes:$.1)valueandoptionalExample usage
An example of where
into cell-pathcan be used is in working withgit config --list. The git configuration has a tree structure that maps well to nushell records. With dynamic cell paths it is easy to convertgit config listto a record:Output:
Errors
lex()+parse_cell_path()are forgiving about what is allowed in a cell-path so it will allow what appears to be nonsense to become a cell-path:But it will reject bad cell-paths:
User-Facing Changes
New conversion command
into cell-pathTests + Formatting
toolkit fmttoolkit clippytoolkit testtoolkit test stdlibAfter Submitting
Automatic documentation updates