Skip to content

allow accessing data with deep variable path #8466

Closed

Description

@amtoine

Related problem

i have some data i would like to run a closure on top of, without knowing in advance what the exact path in the data is nor the exact value i work with.

this works with a single depth-path, e.g. a, but not with multiple levels, e.g. a.b.c.1

e.g.

  • define some data
let data = {a: {b: {c: [1 2 3]}}}
  • ✔️ this works
>_ let target = "a"
>_ $data | get $target
╭───┬───────────────────╮
│   │ ╭───┬───────────╮ │
│ b │ │   │ ╭───┬───╮ │ │
│   │ │ c │ │ 0 │ 1 │ │ │
│   │ │   │ │ 1 │ 2 │ │ │
│   │ │   │ │ 2 │ 3 │ │ │
│   │ │   │ ╰───┴───╯ │ │
│   │ ╰───┴───────────╯ │
╰───┴───────────────────╯
>_ $data | update $target []
╭───┬────────────────╮
│ a │ [list 0 items] │
╰───┴────────────────╯
  • ❌ but this does not work
>_ let target = "a.b.c.1"
>_ $data | get $target
Error: nu::shell::column_not_found

  × Cannot find column
   ╭─[entry #14:1:1]
 1 │ $data | get $target
   · ──┬──       ───┬───
   ·   │            ╰── cannot find column 'a.b.c.1'
   ·   ╰── value originates here
   ╰────

>_ $data | update $target 0
Error: nu::shell::column_not_found

  × Cannot find column
   ╭─[entry #16:1:1]
 1 │ $data | update $target 0
   · ──┬──          ───┬───
   ·   │               ╰── cannot find column 'a.b.c.1'

   ·   ╰── value originates here
   ╰────

Describe the solution you'd like

  • with the same data
let data = {a: {b: {c: [1 2 3]}}}
  • ✔️ this would still work
>_ let target = "a"
>_ $data | get $target
╭───┬───────────────────╮
│   │ ╭───┬───────────╮ │
│ b │ │   │ ╭───┬───╮ │ │
│   │ │ c │ │ 0 │ 1 │ │ │
│   │ │   │ │ 1 │ 2 │ │ │
│   │ │   │ │ 2 │ 3 │ │ │
│   │ │   │ ╰───┴───╯ │ │
│   │ ╰───┴───────────╯ │
╰───┴───────────────────╯
>_ $data | update $target []
╭───┬────────────────╮
│ a │ [list 0 items] │
╰───┴────────────────╯
  • ✔️ and this would work as well
>_ let target = "a.b.c.1"
>_ $data | get $target
2
>_ $data | update $target 0
╭───┬───────────────────────────╮
│   │ ╭───┬───────────────────╮ │
│ a │ │   │ ╭───┬───────────╮ │ │
│   │ │ b │ │   │ ╭───┬───╮ │ │ │
│   │ │   │ │ c │ │ 0 │ 1 │ │ │ │
│   │ │   │ │   │ │ 1 │ 0 │ │ │ │
│   │ │   │ │   │ │ 2 │ 3 │ │ │ │
│   │ │   │ │   │ ╰───┴───╯ │ │ │
│   │ │   │ ╰───┴───────────╯ │ │
│   │ ╰───┴───────────────────╯ │
╰───┴───────────────────────────╯

Describe alternatives you've considered

No response

Additional context and details

No response

Activity

  1. added
    category:enhancement
    on Mar 15, 2023
  2. fdncred commented on Mar 15, 2023

    @fdncred
    Contributor

    We used to have an into cellpath (with whatever name cellpath was before it was named cellpath) and then you could do:

    let target = ("a.b.c.1" | into cellpath)
    $data | get $target
    

    and it would work. This would be pretty easy to implement if we wanted to go back that direction. Otherwise we could update it to "just work" as one would expect, although I'm not sure how to do that myself right now.

  3. added
    semantics
    on Mar 15, 2023
  4. amtoine commented on Mar 16, 2023

    @amtoine
    MemberAuthor

    in the end i think this is a simple duplicate of #6597 and being taken care of in #8482 😌

  5. fdncred commented on Mar 17, 2023

    @fdncred
    Contributor

    closing due to #8493

  6. added this to the v0.78.0 milestone on Mar 17, 2023
  7. amtoine commented on Mar 17, 2023

    @amtoine
    MemberAuthor

    niiiice 😋

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:enhancementsemantics

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions