Fork me on GitHub
#clojure
<
2022-06-15
>
winsome20:06:31

I've got the string "\"" - an escaped quote. I need to escape this string so that it looks like this: "\\\"", substituting \ -> \\ and " -> \". I've tried using (str/escape "\"" {"\\" "\\\\" "\"" "\\\""}), but that just returns the input unchanged.

p-himik20:06:35

In "\"", there's just one character, not two. Use (str/escape "\"" {\" "\\\""}).

🙌 1
winsome20:06:16

Ok, so that example worked but now I'm struggling with newlines.

winsome20:06:51

(str/escape "\n" {\n "\\n"}) isn't replacing anything

p-himik20:06:49

Because "\n" is a newline character in a string. And \n is an n character. To get a newline character, you need to use \newline.

winsome20:06:05

regexes are the worst, but your help is helpful

p-himik21:06:52

Those are not regexes though. :) Just Clojure syntax for character literals.

winsome21:06:16

That's right, thanks. And I was able to handle /return without any trouble too. My serialization tests are all passing now : )

👍 1
winsome20:06:03

I'm trying out (str/replace "\"" #"[\"\\]" #(println (pr-str %))), but it's printing out the whole escaped quote as well.

winsome20:06:13

Is it possible to match that backslash somehow?

Alex Miller (Clojure team)20:06:25

you don't need to string escape inside regex literals (you do need regex escapes)

richiardiandrea21:06:31

Hi there, I rarely use conform but today I was playing around and I have spec like

(s/def ::format-1.1.0
  (s/or :ref string? :refs (s/* string?)))
Is there a way for conform to avoid using the tag for it, e.g.:
[:refs
   ["foo" "bar"]]
and just get a plain ["foo" "bar"]?

Alex Miller (Clojure team)21:06:59

yeah, you can wrap s/nonconforming around it

richiardiandrea21:06:27

Oh let me try that Alex, did not know it existed!

Alex Miller (Clojure team)21:06:46

from what I've seen, the 99% use case for wanting this is s/or so we will probably at some point either make nonconforming actually documented or add an option or variant directly to s/or

❤️ 2
richiardiandrea23:06:16

Hi Alex, sorry back to this, I tried and I see s/nonconforming is applied to all the children, is there a way to avoid that for some of them?

richiardiandrea00:06:03

Oh ok I randomly wrapped the children with s/spec makes them "conforming" again and it worked

(s/def ::user-or-users
  (s/nonconforming
   (s/or :user (s/spec :data-export.user/format-1.1.0)
         :users (s/* (s/spec :data-export.user/format-1.1.0)))))
I am not this can be improved, I'll post it in the main channel as well :D

richiardiandrea00:06:25

ooops no sorry it does not

richiardiandrea00:06:38

kind of stuck on this for now, I'll pick this back up prolly on Monday, thanks in advance for any comment/improvement

;; DONT s/nonconforming here would apply to the children and we don't want that
(s/def ::user-or-users
  (s/or :user :data-export.user/format-1.1.0
        :users (s/* (s/spec :data-export.user/format-1.1.0))))

(s/def ::format-1.1.0
  (s/cat :diagnostic-report.status-code (s/nilable string?)
         :diagnostic-report.primary-reader (s/nilable (s/spec :data-export.user/format-1.1.0))
         :diagnostic-report.assisting-readers ::user-or-users
         :diagnostic-report.overall-impressions (s/nilable string?)))

richiardiandrea00:06:03

Oh ok I randomly wrapped the children with s/spec makes them "conforming" again and it worked

(s/def ::user-or-users
  (s/nonconforming
   (s/or :user (s/spec :data-export.user/format-1.1.0)
         :users (s/* (s/spec :data-export.user/format-1.1.0)))))
I am not this can be improved, I'll post it in the main channel as well :D