Fork me on GitHub
#rewrite-clj
<
2022-01-04
>
cddr12:01:09

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.

lread20:01:17

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

cddr12:01:08

Ah I see now value works now.

borkdude12:01:22

@cddr sexpr should also work

cddr12:01:32

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).

borkdude12:01:53

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

cddr12:01:59

Mm, maybe I made a mistake. Checking again…

borkdude12:01:34

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

cddr12:01:15

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

borkdude12:01:01

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

cddr12:01:11

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)

borkdude12:01:31

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

cddr12:01:28

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

borkdude12:01:44

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

borkdude12:01:50

unless you convert it to EDN first

cddr12:01:08

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

borkdude12:01:55

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
borkdude12:01:58

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.

borkdude12:01:19

@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.

borkdude12:01:52

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