Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

metadata added by read-string (as compared to clj implementation). #1665

Closed
skat-kurt opened this issue Feb 5, 2024 · 9 comments
Closed

Comments

@skat-kurt
Copy link

version
Babashka 1.3.186

platform
Arch Linux (6.7.3-arch1-2)

problem
read-string on edn and then printing it back with *print-meta* bound to true gives different results between babashka and clojure.

repro

The following file is on disk at PATH.

{:name
 test

 :description
 "Testing read-string from bb"

 :details
 {:submap {:first  [some values]
           :second [(comp odd?)]}}}

Now execute the following sequence of expressions.

(def read-edn (read-string (slurp PATH)))

(binding [*print-meta* true]
    (prn read-edn))

expected behavior

I would execpt the output to be identical with the output that standard clojure prints. The babashka version is almost the same, but 1 expression (the list with symbols compand odd? got tagged with metadata (^{:line 9, :column 21}).

The actual edn file I want to read has some metadata tagging in it that I want to keep when writing a new file, but I don't want extra metadata from the read process. For now I have fixed this by postwalking the datastructure from read-line.

eg.

(require '[clojure.walk :as walk])

(def read-edn (walk/postwalk
                 (fn [x]
                   (if (meta x)
                     (vary-meta x dissoc :line :column)
                     x))
                 (read-string (slurp PATH))))
@borkdude
Copy link
Collaborator

borkdude commented Feb 5, 2024

@skat-kurt It's not recommended to use read-string for reading EDN files. This is what we have clojure.edn/read-string for. read-string is for reading code.

@skat-kurt
Copy link
Author

Thanks, using clojure.edn/read-string works perfectly (and I should have known...)

@borkdude
Copy link
Collaborator

borkdude commented Feb 5, 2024

Nonetheless, I'll try to align bb's behavior to clj's.

@borkdude borkdude reopened this Feb 5, 2024
@skat-kurt
Copy link
Author

I'll have to stick with my original work-around for now. The original file I want to read contains a regex, which edn/readstring doesn't like

(edn/read-string "#\"test\"")

Results in the

java.lang.RuntimeException: No dispatch macro for: "

exception, mentioned in another issue as well. That behavior is identical with standard clojure though.

@borkdude
Copy link
Collaborator

borkdude commented Feb 5, 2024

regexes aren't part of EDN, so if your file ends with .edn I would say this is just invalid syntax

@skat-kurt
Copy link
Author

You are right. A pity that the clojure.edn namespace isn't symetric. I mean, there is no clojure.edn/write-string function that could throw when serializing data to edn that cannot be expressed in it. I use pr-str instead, which happily writes regex etc.

Some background. The "edn" files form the basis to auto-generate leiningen compatible project.clj files and some of them need :userjar-merge-with in combination with regex, as seen in the example project.clj file https://gitlab.com/technomancy/leiningen/blob/master/sample.project.clj#L436. I'll find another way to express this in the source edn files, so that they become valid edn files and take it from there.

@borkdude
Copy link
Collaborator

borkdude commented Feb 5, 2024

Well, project.clj isn't technically EDN.

@skat-kurt
Copy link
Author

I think I'll go with introducing a reader tag for regex, so that the edn files at least become valid, while edn/read-string returns regex data. Something like the following.

(edn/read-string
            {:readers {'regex re-pattern}}
            "#regex \"the-pattern\"")

@borkdude
Copy link
Collaborator

borkdude commented Feb 5, 2024

@skat-kurt I recommend using something like edamame for this, which is built into babashka as well. You can tweak whatever you want to allow in your .ednish or .cljish file. E.g. if you want to have EDN + regexes but never a location, you can do this:

(require '[edamame.core :as e])
(e/parse-string "..." {:regex true :location? (constantly false)})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants