ohler55 / ojg

Optimized JSON for Go
MIT License
857 stars 49 forks source link

feature request: want Del method return the deleted elements #183

Closed jujiale closed 1 week ago

jujiale commented 2 months ago

hello, I find in Del method. `

 // Del removes matching nodes.
func (x Expr) Del(data any) error {
    return x.set(data, delFlag, "delete", false)
}

`

could ojg provide a del method. which could return those elements who have been deleted, thanks.

ohler55 commented 2 months ago

That's a possibility. It would not be as performant and it would think returning both path and value would be best.

Alternatively a delete with a callback is another option.

ohler55 commented 2 months ago

I think the callback approach will be best. Not sure when I will have it done though.

jujiale commented 1 month ago

thanks,I will also research how to achieve this. if done. I will submit a pr.

ohler55 commented 1 month ago

The jp.Modify might be a good example of how to implement. Not exactly the same but similar. It also depends on whether the delete should replace items in a list with nil or remove from list.

ohler55 commented 1 week ago

I've started a branch names expr-walk. The plan is to implement func (x Expr) Walk(data any, cb func(path Expr, data, parent any)). That will walk the matching nodes and allow the cb function to modify and/or collect matching nodes. It should be easy to implement a delete that returns the deleted values along with the path the deleted value was found at with this new function.

jujiale commented 1 week ago

I've started a branch names expr-walk. The plan is to implement func (x Expr) Walk(data any, cb func(path Expr, data, parent any)). That will walk the matching nodes and allow the cb function to modify and/or collect matching nodes. It should be easy to implement a delete that returns the deleted values along with the path the deleted value was found at with this new function.

thanks, that's awesome, I will have a look.

ohler55 commented 1 week ago

Only started the branch. Not even close to being ready for use. Just letting you know in case you wanted to keep an eye on it.

jujiale commented 1 week ago

Only started the branch. Not even close to being ready for use. Just letting you know in case you wanted to keep an eye on it.

OK,get it!

ohler55 commented 1 week ago

The expr-walk branch is ready to be merged. I plan on merging and releasing tomorrow unless someone such as yourself has comments about it.