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
Data readers are not being loaded #419
Comments
Note to self: in Clojure the user is responsible for requiring the namespace with data reader functions:
So babashka should do the same. |
Note:
When Clojure starts, it searches for files named data_readers.clj at the root of the classpath. Each such file must contain a Clojure map of symbols, like this:
|
@albersonn I have tested with discovering all
This takes about 14-30 ms. So I don't think it would be acceptable to discover them at babashka startup time unless I can find a way to optimize this. Possible strategies:
|
Possible optimization: Currently we're walking the entire jar files when we search something on the classpath, but this might not be necessary when we already know the exact filename This turned out to help. For the above classpath it now takes 8ms. Further optimization may be to combine the search for |
The further optimization from the last comment didn't seem to help that much in the data loaders case, but it did help in general when finding things on the classpath. For a smaller classpath like:
searching for data_readers takes about 3ms:
Maybe that's ok enough. When people do not use a classpath they are not bothered by this slowdown. |
@borkdude maybe this can be a flag in |
@albersonn That might be a good idea since data readers haven't been that common yet. Will the data reader configuration also be provided using this flag or does babashka still scan the classpath itself? I'll still have to figure out how to implement data readers since I guess babashka or sci has to have a In Clojure I notice that it populates this dynamic var before you require any namespace:
So it already creates these vars and namespaces but leaves the vars unbound until they are actually populated by requiring the namespaces. |
@albersonn One compromise might be to make a function in So a program would look like:
and then the rest would be the same as in Clojure. |
@borkdude I guess the function in babashka classpath looks better than the switch. It'll be very declarative of what scripts uses/loads data readers. This will avoid some bugs/unexpected behaviours if you forget the switch and will be (kind of) faster to debug slow scripts. I like it. |
Another alternative is to let people set data-readers themselves by doing something like:
This would make it compatible with clojure as well and would not incur any startup penalty. |
It seems we must use
|
I don't know if the last mentioned alternative will always work since theoretically a namespace with the data reader function in it can already start using it in that very namespace. |
@albersonn With babashka master this is now possible: bb "(set! *data-readers* {'t/tag inc}) #t/tag 1"
2 Can you test if this suffices for your use case? Do you need a binary or can you build yourself? You can also run with the JVM like: lein bb "(set! *data-readers* {'t/tag inc}) #t/tag 1"
2 or: clojure -A:main "(set! *data-readers* {'t/tag inc}) #t/tag 1"
2 |
@borkdude I tested and this solution looks good enough for me. Thanks! |
v0.0.92
OS X Mojave 10.14.6 (18G4032)
Custom data readers are not being loaded
There are some data readers defined in a project which is in the classpath but I can't access them
repro
BABASHKA_PRELOADS
bb "#your/datareader 'something"
expected behavior
Example code
readers.clj
data_readers.clj
The text was updated successfully, but these errors were encountered: