rewrite-clj

2022-01-06T15:04:13.002500Z

So in relation to the problem I described a few days ago, I wrote a function which attempts to determine whether some zloc is in the key position of a map. But I think it only works for the first map entry

(defn map-key?
  [zloc]
  (and
   (zip/map? (zip/up zloc))
   (zip/leftmost? zloc)))
Anyone know how this could be written to match a key in any map entry?

2022-01-06T15:06:03.003100Z

Or maybe it’s better to just identify the containing maps and replace them as a whole?

borkdude 2022-01-06T16:03:06.004500Z

@cddr how I approached this in rewrite-edn: I just treated every nth element as a key and every n+1th element as a value

lread 2022-01-06T16:08:49.006400Z

@cddr if you need to, you might also want to consider accounting for any #_ that might be present. Example {:a #_ ignored-thing 1}

2022-01-06T16:09:29.006800Z

Oh fortunately I don’t think I’ll need to worry about that but thanks for the headsup

👍 1
yogthos 2022-01-06T21:23:37.007900Z

was wondering if anybody could help here, when I do this

(z/sexpr (z/of-string "{:db.sql/connection #profile\n {:prod {:jdbc-url #env JDBC_URL}}}"))
I end up with
#:db.sql{:connection (read-string "#profile\n{:prod {:jdbc-url #env JDBC_URL}}")}
is there a way to roundtrip this properly?

borkdude 2022-01-06T22:23:54.008600Z

@yogthos you can do this by (binding [*print-namespace-maps* false] ...) , forgive me the bolded font, it's supposed to be earmuffed.

yogthos 2022-01-07T14:34:51.009800Z

ah perfect!

yogthos 2022-01-07T14:39:51.010Z

hmm, I can't find where *print-namespace-maps* is defined though

borkdude 2022-01-07T14:40:30.010200Z

in clojure.core :)

yogthos 2022-01-07T14:40:51.010400Z

ohh

borkdude 2022-01-07T14:40:55.010600Z

the printing behavior you saw comes from clojure, not from rewrite-clj

yogthos 2022-01-07T14:41:00.010800Z

haha I thought it was a rewrite-clj thing

yogthos 2022-01-07T14:41:15.011Z

these are corners of Clojure I haven't explored yet 🙂

yogthos 2022-01-07T14:42:33.011200Z

hmm no dice

(binding [*print-namespace-maps* false]
    (z/sexpr (z/of-string "{:db.sql/connection #profile\n {:prod {:jdbc-url #env JDBC_URL}}}")))

yogthos 2022-01-07T14:43:33.011400Z

it's this bit that's causing trouble (read-string "#profile\n{:prod {:jdbc-url #env JDBC_URL}}")

borkdude 2022-01-07T14:46:24.011600Z

oh what does it say?

borkdude 2022-01-07T14:47:36.011800Z

I see

yogthos 2022-01-07T14:48:05.012Z

yeah not sure why it ends up getting wrapped with read-string

borkdude 2022-01-07T14:48:21.012200Z

I think this is because you cannot convert #profile ..." to an s-expression without letting rewrite-clj know how to resolve that reader tag

borkdude 2022-01-07T14:49:06.012400Z

why do you need the s-expressions, perhaps there is a better alternative?

yogthos 2022-01-07T14:49:56.012600Z

I just need to inject a piece of edn into a file, so yeah maybe I'm barking up a wrong tree here

yogthos 2022-01-07T14:51:44.012800Z

I use a zipper to find the target location, and then do this

(defn edn-merge-value [value]
  (fn [node]
    (if-let [inside-map (z/down node)]
      (-> inside-map
          (z/rightmost)
          (zipper-insert-kw-pairs (z/down value)))
      (z/replace node (z/node (format-zloc value))))))

borkdude 2022-01-07T14:52:33.013Z

I don't see sexpr in there?

borkdude 2022-01-07T14:53:53.013200Z

you can use str instead to get back the string instead

borkdude 2022-01-07T14:54:05.013400Z

sexpr should really be used with caution

yogthos 2022-01-07T14:54:49.013600Z

oh I see

yogthos 2022-01-07T14:56:15.013800Z

but using str on a node will give a map containing the zipper

borkdude 2022-01-07T14:56:39.014Z

you first need to do (z/node ...)

borkdude 2022-01-07T14:57:03.014200Z

and also z/root ... if you're not at the root of the zipper

yogthos 2022-01-07T14:57:11.014400Z

ah ok, this looks like what I'm looking for

(binding [*print-namespace-maps* false]
    (str (z/node (z/of-string "{:db.sql/connection #profile\n {:prod {:jdbc-url #env JDBC_URL}}}"))))

borkdude 2022-01-07T14:57:15.014600Z

so (-> zloc z/root z/node str)

borkdude 2022-01-07T14:57:30.014800Z

yes

yogthos 2022-01-07T14:57:36.015Z

thank you!

borkdude 2022-01-07T14:57:51.015200Z

probably the print-namespace-maps binding isnt' necessary here

borkdude 2022-01-07T14:58:03.015400Z

as rewrite-clj won't change the node representation

yogthos 2022-01-07T14:58:13.015600Z

yeah I think I can drop that now

yogthos 2022-01-07T14:59:59.015800Z

now I'll have to go back and check all the places I'm using z/sexpr, I think in most cases I intended to do (-> value z/node str)