Closed twelvelabs closed 2 months ago
I'll have to look at that carefully. It might require another interface to support modifying a keyed or indexed.
My apologies that it has taken this long for me to get to this. I've started a branch called modify-keyed-indexed that has the start of the updates. All have been implemented but minimally tested if you want to try it out as I add the tests.
Added in release v1.22.1
Hello!
First off, many thanks for a fantastic library! It's made my life a lot easier and I really appreciate it.
I'm currently using
ojg/jp
in a CLI tool that (among other things) allows users to modify JSON and YAML files via JSON path expressions. In both cases I'm unmarshalling the files tomap[string]any
and passing them intojp.Expr.Modify()
.One thing I'd like to add is the ability to maintain comments, whitespace, and key ordering when updating YAML files. The YAML lib I'm using supports this when unmarshalling into a yaml.Node, so I've been looking into using that instead of an untyped map.
I noticed the recent addition of the
jp.Keyed
andjp.Indexed
interfaces, and I was thinking about creating a wrapper aroundyaml.Node
that implemented those interfaces. This seems like it would do what I want.Unfortunately it doesn't look like the
jp.Expr.Modify()
method supports those new interfaces. That or perhaps I'm invoking it incorrectly 🤷. Here's what I've tried:Which returns:
I expected the
z
keys in thedata
maps to be set to999
. Works as expected when removing theNode
wrappers. Also works when usingjp.Expr.Set()
.I didn't see any references to
Indexed
orKeyed
in https://github.com/ohler55/ojg/blob/develop/jp/modify.go, so I'm thinking thatModify
just does't currently support them. I would love to help out and add that myself, but honestly that code is a bit beyond me 😞. Is there any chance you'd be interested in doing so?