Skip to content

into cellpath command#7417

Merged
kubouch merged 4 commits intonushell:mainfrom
Kangaxx-0:gaxx/into-path
Dec 10, 2022
Merged

into cellpath command#7417
kubouch merged 4 commits intonushell:mainfrom
Kangaxx-0:gaxx/into-path

Conversation

@Kangaxx-0
Copy link
Copy Markdown
Contributor

Description

Address part of feature request #7337, add a small command into cellpath to allow string -> cellpath auto-conversion, with this change, we could run

let p = 'ls.use_ls_colors'
$env.config | upsert ($p | nito cellpath) false

image

Note - This pr only covers String -> CellPath, any other conversions should be considered as expected?

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 after the PR is merged, if necessary. This will help us keep the docs up to date.

@Kangaxx-0 Kangaxx-0 marked this pull request as ready for review December 9, 2022 19:53
@Kangaxx-0 Kangaxx-0 changed the title Gaxx/into path into cellpath command Dec 9, 2022
@kubouch
Copy link
Copy Markdown
Contributor

kubouch commented Dec 10, 2022

I think this is a great idea but IMO it should be more robust. I can see a couple of points where the command would fail:

  • Quoted cell path members: "foo.'filename.txt'"
  • Cell paths with integers: "foo.0.b"

I keep thinking we shouldn't add new logic handling these cases because that would be just duplicating the logic that's already in the parser. What I would try:

  1. Create a new working set and use the lexer to correctly split the input string, same as here.
  2. Use parse_cell_path() on the stream of tokens to get the cell path expression <-- maybe not the best idea
  3. Create Value::CellPath from the expression.

Stuff like that is generally not safe, but I think this should OK because the CellPath and PathMember structs do not reference anything new added to the newly created working set (which will be thrown away). They only point at spans which are already present in the old working set. It would be a problem if we added subexpressions to cell paths, like foo.(let x = 'bar'; $x).

I'm going to merge this because it is an improvement over the current state but the above points should eventually be addressed.

@kubouch kubouch merged commit f0e93c2 into nushell:main Dec 10, 2022
rgwood added a commit to rgwood/nushell that referenced this pull request Dec 19, 2022
rgwood added a commit that referenced this pull request Dec 19, 2022
This reverts commit f0e93c2 (PR #7417).

I'm currently [working on improving cell
paths](#7498 (comment)),
and I realized that I would need to make several improvements to `into
cellpath` along the lines of Jakub's comment here:
#7417 (comment)

I don't think `into cellpath` is quite ready for prime-time, and I'd
like to remove it before the upcoming release.
@rgwood
Copy link
Copy Markdown
Contributor

rgwood commented Dec 19, 2022

Thanks for the PR, @Kangaxx-0. I reverted this for now because I'm working on some cell path improvements and the duplicated parser logic in into cellpath was causing trouble.

We'd welcome another PR to add this back with the improvements that @kubouch suggested (but if you're going to do that, maybe wait a little bit to avoid conflicts; my cell path changes will hopefully be done in the next couple days).

@Kangaxx-0
Copy link
Copy Markdown
Contributor Author

Thanks for the PR, @Kangaxx-0. I reverted this for now because I'm working on some cell path improvements and the duplicated parser logic in into cellpath was causing trouble.

We'd welcome another PR to add this back with the improvements that @kubouch suggested (but if you're going to do that, maybe wait a little bit to avoid conflicts; my cell path changes will hopefully be done in the next couple days).

I get the basic idea that comment is asking, however, I don't feel confident to reparse input during eval. In general, is it ok to call lex during command running, which is in evaluation stage?

@kubouch
Copy link
Copy Markdown
Contributor

kubouch commented Dec 21, 2022

Thanks for the PR, @Kangaxx-0. I reverted this for now because I'm working on some cell path improvements and the duplicated parser logic in into cellpath was causing trouble.
We'd welcome another PR to add this back with the improvements that @kubouch suggested (but if you're going to do that, maybe wait a little bit to avoid conflicts; my cell path changes will hopefully be done in the next couple days).

I get the basic idea that comment is asking, however, I don't feel confident to reparse input during eval. In general, is it ok to call lex during command running, which is in evaluation stage?

Using lex should be totally fine because it does not alter a state of anything. Maybe calling parse_cell_path() would be too sensitive, because you could get into a string interpolation which then could have sub-expressions which can modify the working set. So calling parse_cell_path() directly might not be the best idea, but using the lexer, you could at least split the cell path members correctly, instead of splitting the string manually by .. Then, following the logic found in parse_cell_path(), iterate through the lexer output and convert them to either integers or strings. This should be totally safe, you'd just be looking up spans from the engine state.

@fdncred fdncred mentioned this pull request Mar 16, 2023
@fdncred
Copy link
Copy Markdown
Contributor

fdncred commented Mar 16, 2023

Using lex should be totally fine because it does not alter a state of anything. Maybe calling parse_cell_path()

I tried this and it was too difficult to get it to work. JT also didn't think we should do it as it comes too close to eval.

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.

4 participants