Skip to content

Construct cell paths programmatically #7337

Closed

Description

@kubouch

Related problem

It is currently impossible to update/insert/upsert a nested path from a variable:

let p = 'ls.use_ls_colors'
$env.config | upsert $p false

Nushell treats $p as a string and instead of setting $env.config.ls.use_ls_colors, it ads a new entry $env.config.'ls.use_ls_colors'. This is also a bug because you shouldn't be allowed to have . as a column name.

Describe the solution you'd like

  1. IMO, the first parameter to update/insert/upsert (and possibly other relevant commands) should auto-convert the string into a nested cell paths.
  2. Another option would be to add into cell-path command that would do the same, and you'd have to call upsert ($p | into cell-path) false.

The options are not mutually exclusive. Either way, the bug that . can appear as a column name should be fixed.

Describe alternatives you've considered

No response

Additional context and details

key value
version 0.72.1
branch main
commit_hash 850ecf6
build_os linux-x86_64
build_target x86_64-unknown-linux-gnu
rust_version rustc 1.65.0 (897e37553 2022-11-02)
rust_channel 1.65.0-x86_64-unknown-linux-gnu
cargo_version cargo 1.65.0 (4bc8f24d3 2022-10-20)
pkg_version 0.72.1
build_time 2022-12-03 12:09:09 +02:00
build_rust_channel release
features database, dataframe, default, trash, which, zip
installed_plugins gstat

Activity

  1. added
    category:bug
    category:enhancement
    on Dec 3, 2022
  2. webbedspace commented on Dec 4, 2022

    @webbedspace
    Contributor

    I really don't like automatically casting string to cell path (regardless of whether that's easy or not). Don't other programming languages deal with this by just having computed indexing? In Javascript you'd do foo[bar][baz]. To me, in Nushell you ought to be able to do stuff like (ls).($rownumber).name or upsert ($p).($q) false. Obviously this doesn't allow storing a full path inside a string like in your example, but that doesn't really inconvenience other languages that much.

    Also, allowing . in column names is by no means a bug: you need to be able to create stuff like {filename.txt: 200b, a.out: 0b} in the language. ls | group-by name would immediately stop working if you forbade it.

  3. theAkito commented on Dec 4, 2022

    @theAkito

    All such issues are basically a sub-issue of #7033.

    It's about Nu not interpreting interpretable stuff & just making it a string.

    This limits programmability & automatisation a lot.

    The day Nu will stop making strings out of legitimate, programmatic arguments, will be the day the functionality & programmability & therefore usefulness of Nu will dramatically increase.

  4. changed the title [-]Treat strings as cell paths more predictably[/-] [+]Construct cell paths programmatically[/+] on Dec 5, 2022
  5. Kangaxx-0 commented on Dec 7, 2022

    @Kangaxx-0
    Contributor

    My two cents, nu is both programming language + shell, so things gets interesting whenever this kind of feature requesting coming in. For this particular one, I feel we want to should support the auto-conversion, which does not have to change the parser dramatically.

    Regards to Into cellpath command, what else Vaule type could be considered as IntoCellPath convertible?

    image

  6. added a commit that references this issue on Dec 10, 2022
  7. kubouch commented on Dec 10, 2022

    @kubouch
    ContributorAuthor
  8. Kangaxx-0 commented on Dec 11, 2022

    @Kangaxx-0
    Contributor

    See also #7417 (comment)

    I'd like to continue the discussion prior to your comments

    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
    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).

    If I get this understood correctly, you would like to see and try re-tokenizing during into cellpath's run function. Nu has parsing and eval phases running separately, they should not be intervening with each other? Or I just misunderstood your comment?

  9. fdncred commented on Mar 10, 2024

    @fdncred
    Contributor

    closed because we have into cell-path now #11322

  10. added this to the v0.90.0 milestone on Mar 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    category:bugcategory:enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions