Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ast.Node should support move and/or copy at any depth of the tree; not just within arrays #732

Open
scr-oath opened this issue Jan 13, 2025 · 4 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@scr-oath
Copy link

If some of the other feature issues are addressed, this may be straightforward to do as a client with a set and delete, but a copy/move (copy with delete of src) would be convenient for reshaping incoming fuzzy json generously into a schema that can parse naturally.

@AsterDY
Copy link
Collaborator

AsterDY commented Jan 22, 2025

Yes, can you introduce your practical scenario

@scr-oath
Copy link
Author

scr-oath commented Feb 4, 2025

In an application that does quite a bit of json munging reshaping, the prevailing solution is to use a combination of gjson and sjson. While I'm aware of the cloudwego.gjson as being faster (for lookups at least, if not walking a structure) I don't believe there's an equivalent to sjson.

The ast seems to have methods for getting things by path, but not for setting by path. Furthermore, Unset only acts upon a parent node.

let's say… for instance, that you have a structure like this:

{
  "foo": {
    "w": 123,
    "h": 345
  }
}

and you want to end up with

{
  "w": 123,
  "h": 345
}

It would be nice to be able to say root.MovePath("foo", "") or the like…

Or, foo had more info, then moving just the width and height something like root.MovePath("foo.w", "w"); root.MovePath("foo.h", "h")

or… maybe some signature to move across different roots might be helpful too - should there be a ton of things on foo that wanted to move to bar… I guess it's all possible with combinations of get, unset, and set, but it does seem fairly verbose for clients to do.

Another aspect of this is setting things at locations in the tree that don't exist yet, would be nice to do something like mkdir -p but for objects… I think I added #731 for that so I won't double down on it here, but as part of move, it would be good to be able to move to locations that don't exist or have some facility to at least make a path of the right type by knowing each path element's type (string for map, int for slice)

@scr-oath
Copy link
Author

scr-oath commented Feb 4, 2025

Similar to what I was saying about #731, given the starting point of

{
  "foo": {
    "w": 123,
    "h": 345
  }
}

You may wish to move from foo to bar (as a rename) to end up with

{
  "bar": {
    "w": 123,
    "h": 345
  }
}

@scr-oath
Copy link
Author

scr-oath commented Feb 4, 2025

Another aspect that may be a flag or something - not sure how often this would play in but just saying… there might be a desire to prune empty…

{
  "depth1": {
     "depth2": {
       "w": 123,
       "h": 345
    }
  }
}

If you move, say… "depth1.depth2" to "foobar" you might want to also delete depth1 since it will become empty at that point to end up with:

{
  "foobar": {
     "w": 123,
     "h": 345
  }
}

@AsterDY AsterDY added enhancement New feature or request help wanted Extra attention is needed labels Feb 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants