Skip to content

New optional cell-path make use of variables harder #8480

Closed

Description

Related problem

This is related to the merged unreleased MR 8379

Previous to this change i could do the following:

$ let b = "b"
$ {"a": 1} | get -i $b

With this change i cant find a way (expect for using a try-block) to do this:

$ let b = "b"
$ {"a": 1} | get $b?
Error: nu::parser::parse_mismatch

  × Parse mismatch during operation.
   ╭─[entry #3:1:1]
 1 │ {"a": 1} | get $b?
   ·                  ┬
   ·                  ╰── expected .
   ╰────

$ {"a": 1} | get ($b)?
Error: nu::parser::parse_mismatch

  × Parse mismatch during operation.
   ╭─[entry #5:1:1]
 1 │ {"a": 1} | get ($b)?
   ·                    ┬
   ·                    ╰── expected .
   ╰────

$ {"a": 1} | get $"($b)"?
Error: nu::shell::column_not_found

  × Cannot find column
   ╭─[entry #17:1:1]
 1 │ {"a": 1} | get $"($b)"?
   · ────┬───       ────┬───
   ·     │              ╰── cannot find column 'b"?'
   ·     ╰── value originates here
   ╰────

Describe the solution you'd like

I would appreciate it if one of the following would work:

  • get $b?
  • get ($b)?

Describe alternatives you've considered

{"a": 1} | try {get ($b)} catch {null} works, but is a bit chunky in my opinion.

Additional context and details

No response

Activity

  1. ghost added
    category:enhancement
    on Mar 16, 2023
  2. rgwood commented on Mar 16, 2023

    @rgwood
    Contributor

    Thanks for the detailed issue! It's really nice to get feedback like this during the development cycle.

    Using variables in cell paths is something I didn't explore quite enough. I agree that using try/catch is a little awkward. FWIW, you can drop the parens in the try and do {"a": 1} | try {get $b} catch {null}, but that's not a huge improvement.

    A few people have been exploring an into cellpath command that would convert a string to a cell path exactly the same way the parser does. With that, you could do something like:

    let b = "b?" | into cellpath
    {"a": 1} | get $b
    

    The into cellpath would parse the b? and note that the b path member is optional. I'm wondering whether that would meet your needs.

  3. rgwood commented on Mar 16, 2023

    @rgwood
    Contributor

    Another option would be to just bring back the -i flag. I was thinking it wouldn't be needed after the ? change, but I wasn't considering the use of variables.

  4. rgwood commented on Mar 16, 2023

    @rgwood
    Contributor

    Say the b variable is a cell path with multiple members, foo.bar.baz (this can't be done right now because the relationship between strings and cell paths is only half baked, but let's pretend it can). In that case, what behaviour do you expect for get $b?? Do you expect it to make every path member optional, i.e. something equivalent to get foo?.bar?.baz??

    having -i and ? feels a bit wrong.

    I initially thought so, but I've come around to the view that it's useful to have a quick way to make every member in the cell path optional. i.e. get foo?.bar?.baz? would be equivalent to get -i foo.bar.baz.

  5. rgwood commented on Mar 16, 2023

    @rgwood
    Contributor

    Cool, I'll re-add -i soon.

  6. rgwood commented on Mar 16, 2023

    @rgwood
    Contributor

    Fixed in #8488

  7. added this to the v0.78.0 milestone on Mar 17, 2023
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:enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions