rewrite-clj

2022-01-04T12:29:09.186500Z

Morning folks, and happy new year…. I’ve spent my christmas break exploring rewrite-clj. It looks really cool and has helped me to finally understand zippers. Kudos on all the project docs. They’ve been really helpful. I’m wondering how you’d go about determining whether some zloc that has been navigated to is a string. My first thought was to say (string? (zip/node zloc)) but that doesn’t work because my string is a token. Then I found node/value which seems to return the string but is marked as deprecated. I tried following the advice in the deprecation node but both node/child-sexprs and node/children are coming out as nil.

lread 2022-01-05T20:28:17.206500Z

Glad you found the docs helpful. Always looking to improve them. Let us know if anything was amiss/confusing.

2022-01-04T12:30:08.186900Z

Ah I see now value works now.

borkdude 2022-01-04T12:42:22.187200Z

@cddr sexpr should also work

2022-01-04T12:44:32.189500Z

Yeah but the problem is that sexpr seems to return strings even for map nodes. The bigger picture is that I’m using this to edit a json tree and need to isolate my edits to strings in the rhs position of any maps (i.e. leave the keys alone).

borkdude 2022-01-04T12:44:53.189900Z

sexpr should not return a string for a map node :)

2022-01-04T12:46:59.190500Z

Mm, maybe I made a mistake. Checking again…

borkdude 2022-01-04T12:47:34.191200Z

although you're speaking about JSON. rewrite-clj is intended to be used on clojure code... JSON...?

2022-01-04T12:49:15.192700Z

Yeah but it comes with a zipper that works well with a variety of data-types. The ones in core work only seem to work if the entire data-structure is built out of the same collection type

borkdude 2022-01-04T12:50:01.193400Z

What are you referring to by "it" in "it comes with"?

2022-01-04T12:50:11.193700Z

If there’s something else you’re aware of that’s better for editing JSON trees I’d be pleased to discover alternatives. (it=rewrite-clj)

borkdude 2022-01-04T12:50:31.194200Z

what does your JSON tree look like? Is that valid Clojure code?

2022-01-04T12:51:28.195100Z

It’s a JSON schema so not clojure code at all

borkdude 2022-01-04T12:51:44.195400Z

if rewrite-clj accepts JSON then that might be accidental, I would not expect that to work at all

borkdude 2022-01-04T12:51:50.195600Z

unless you convert it to EDN first

2022-01-04T12:52:08.196100Z

Oh sorry, yeah I convert it to EDN first before piping it into rewrite-clj

borkdude 2022-01-04T12:52:55.196700Z

ok, yes, that should work 😅 you might also be interested in https://github.com/borkdude/rewrite-edn which is a thin layer over rewrite-clj

👀 1
borkdude 2022-01-04T12:57:58.198700Z

There have been some ideas floating around to make a more elaborate version of tag that returns :string etc and not only :token, which I think should be a good solution in general. I've been making my own predicates in clj-kondo for this.

borkdude 2022-01-04T12:59:19.200Z

@lee We could also do something like :rewrite-clj/namespaced-map and use (derive :rewrite-clj/namespaced-map :rewrite-clj/map) so you can use isa? on it. Looks good to me.

borkdude 2022-01-04T12:59:52.200300Z

I think this is one of the main missing things in rewrite-clj