This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-05-16
Channels
- # announcements (2)
- # asami (124)
- # babashka (30)
- # babashka-sci-dev (73)
- # beginners (40)
- # biff (1)
- # calva (39)
- # clj-kondo (54)
- # clj-otel (1)
- # cljdoc (59)
- # cljs-dev (8)
- # clojars (2)
- # clojure (96)
- # clojure-austin (16)
- # clojure-boston (6)
- # clojure-europe (51)
- # clojure-nl (1)
- # clojure-norway (1)
- # clojure-russia (60)
- # clojure-uk (4)
- # clojurescript (34)
- # community-development (6)
- # cursive (2)
- # datahike (10)
- # datascript (18)
- # emacs (109)
- # etaoin (1)
- # events (3)
- # figwheel-main (41)
- # fulcro (13)
- # helix (4)
- # introduce-yourself (5)
- # jobs (1)
- # leiningen (5)
- # lsp (8)
- # malli (6)
- # meander (7)
- # nrepl (6)
- # off-topic (60)
- # pathom (29)
- # polylith (8)
- # re-frame (5)
- # reitit (1)
- # releases (1)
- # remote-jobs (1)
- # rewrite-clj (33)
- # sci (3)
- # shadow-cljs (3)
- # xtdb (82)
I noticed a strange behaviour with z/assoc:
; As expected:
(-> (z/of-string "{}") (z/assoc :demo "string-value"))
=> [<map: {:demo "string-value"} > {:l [], :pnodes [<forms: {} >], :ppath nil, :r nil, :changed? true}]
; Quoting something gives a strange map:
(-> (z/of-string "{}") (z/assoc :demo "\"quoted\""))
=> [<map: {:demo "", quoted ""} > {:l [], :pnodes [<forms: {} >], :ppath nil, :r nil, :changed? true}]
; I would expect this:
(-> (z/of-string "{:demo \"\\\"quoted\\\"\" }"))
=> [<map: {:demo "\"quoted\""} > {:l [], :pnodes [<forms: {:demo "\"quoted\""} >], :ppath nil, :r nil}]
Or do I need to wrap the value to z/assoc in something?
Edit1: Typos and: Thanks for a great library!
Edit2: I'm using 1.0.767-alpha
.Hiya @ivar.refsdal thanks for your question! I’ll fire up a REPL soon and take a look.
@ivar.refsdal Ok I’ll start with rerunning your example from a plain old clj
repl.
user=> (require '[rewrite-clj.zip :as z])
nil
user=> (-> (z/of-string "{}") (z/assoc :demo "string-value"))
[<map: {:demo "string-value"}> {:l [], :pnodes [<forms: {}>], :ppath nil, :r nil, :changed? true}]
user=> (-> (z/of-string "{}") (z/assoc :demo "\"quoted\""))
[<map: {:demo ""quoted""}> {:l [], :pnodes [<forms: {}>], :ppath nil, :r nil, :changed? true}]
user=> (-> (z/of-string "{:demo \"\\\"quoted\\\"\" }"))
[<map: {:demo "\"quoted\"" }> {:l [], :pnodes [<forms: {:demo "\"quoted\"" }>], :ppath nil, :r nil}]
The first thing I’m noticing is that our outputs for (-> (z/of-string "{}") (z/assoc :demo "\"quoted\""))
are different.
To make sure we are on the page, can you rerun on your side and see if there is a difference?I’m gonna guess something formatted your output weird and that we are on the same page.
So, I think maybe the issue is that you are expecting strings to the same as reading from a file?
The string "string-value"
is internally the string string-value
The string "\"quote\""
is internally the string "quoted"
And those are what will be seen by rewrite-clj.
You are right in that the formatted output is weird in some way. It also works on the console for me (but not in IntelliJ/Cursive). However, here is a related problem (?)/thing I don't understand:
(-> "{}" z/of-string (z/assoc :demo "here is a quote:\". End.") (z/root-string))
=> "{:demo \"here is a quote:\". End.\"}"
I also tried with the example from the user guide:
https://github.com/clj-commons/rewrite-clj/blob/main/doc/01-user-guide.adoc#finding-elements-with-the-zip-api
and replaced
(-> zloc-desc (z/replace "My first Project.") z/root-string println)
with
(-> zloc-desc (z/replace "Project with a quote: \". A quote.") z/root-string println)
It outputs:
(defproject my-project "0.1.0-SNAPSHOT"
:description "Project with a quote: ". A quote.")
There is something wrong, no?
(Or am I just confused maybe?)Thanks for your time and reply 🙂
1.0.767-alpha does not do it properly (I think!):
$ clj -Sdeps '{:deps {rewrite-clj/rewrite-clj {:mvn/version "1.0.767-alpha"}}}' -e '(do
(require (quote [rewrite-clj.zip :as z]))
(-> (z/of-string "{}")
(z/assoc :demo "and a quote: \". <- A quote.")
(z/root-string)
(println))
)'
WARNING: Implicit use of clojure.main with options is deprecated, use -M
{:demo "and a quote: ". <- A quote."}
whereas 0.6.1 does it correctly:
$ clj -Sdeps '{:deps {rewrite-clj/rewrite-clj {:mvn/version "0.6.1"}}}' -e '(do
(require (quote [rewrite-clj.zip :as z]))
(-> (z/of-string "{}")
(z/assoc :demo "and a quote: \". <- A quote.")
(z/root-string)
(println))
)'
WARNING: Implicit use of clojure.main with options is deprecated, use -M
{:demo "and a quote: \". <- A quote."}
Head has same behaviour as .767:
$ clj -Sdeps '{:deps {rewrite-clj/rewrite-clj {:git/url "" :sha "0bf3ee3ac625738f7fa3e5e163039e1bcf47fff2"}}}' -e '(do
(require (quote [rewrite-clj.zip :as z]))
(-> (z/of-string "{}")
(z/assoc :demo "and a quote: \". <- A quote.")
(z/root-string)
(println))
)'
Cloning:
Checking out: at 0bf3ee3ac625738f7fa3e5e163039e1bcf47fff2
WARNING: Implicit use of clojure.main with options is deprecated, use -M
{:demo "and a quote: ". <- A quote."}
Thanks for comparing against v0.6.1, that is interesting. I’m more confused, frankly, by 0.6.1 behaviour than 1.0.767 behaviour. I’ll take a deeper look.
So more narrowed down to the coercion, we can see the difference: v1
❯ clj -Sdeps '{:deps {rewrite-clj/rewrite-clj {:mvn/version "1.0.767-alpha"}}}'
Clojure 1.11.1
user=> (require '[rewrite-clj.node :as n])
nil
user=> (n/coerce "hey \" man")
<token: "hey " man">
v0
❯ clj -Sdeps '{:deps {rewrite-clj/rewrite-clj {:mvn/version "0.6.1"}}}'
Clojure 1.11.1
user=> (require '[rewrite-clj.node :as n])
nil
user=> (n/coerce "hey \" man")
<token: "hey \" man">
I’ll pinpoint what change made v1 do things differently.Raised an isssue: https://github.com/clj-commons/rewrite-clj/issues/176
@U04V15CAJ would you mind having a peek and sharing your opinion on the issue? My current feeling is that I unknowingly (at least I don’t remember) fixed an issue in v0.
@UE21H2HHD Will do later this week!
Update examples in https://github.com/clj-commons/rewrite-clj/issues/176 to use pr to make things clearer (even to me!).
We are wrapping a string in "
so we need to wrap any inner "
(I’ve flip flopped and now think rewrite-clj v0 is correct).
So @ivar.refsdal, it took my a while to get there, but I finally understand! Thanks so much for raising this, I’ll follow up with a fix sometime soon.
(and also thanks @U04V15CAJ, as usual!)