This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-02-18
Channels
- # announcements (1)
- # asami (14)
- # babashka (43)
- # beginners (36)
- # biff (2)
- # calva (24)
- # clerk (3)
- # clj-kondo (9)
- # cljdoc (18)
- # clojure (16)
- # clojure-berlin (1)
- # clojure-europe (7)
- # clojurescript (8)
- # clr (4)
- # conjure (3)
- # cursive (2)
- # emacs (14)
- # funcool (4)
- # humbleui (2)
- # hyperfiddle (105)
- # kaocha (3)
- # malli (5)
- # membrane (23)
- # off-topic (16)
- # pathom (3)
- # reitit (25)
- # releases (3)
- # shadow-cljs (13)
- # xtdb (6)
Is sun.nio.fs.WindowsPath
not included in babashka? I get Could not resolve symbol: sun.nio.fs.WindowsPath
.
it doesn't look like it's exposed for reflection/direct instantiation, but the class is "included":
user=> (class (fs/path "."))
sun.nio.fs.WindowsPath
Need more context, why do you need this? If you need to deal with paths, you can use babashka.fs
or cast to a public super class. sun.
classes are implementation details that you should usually not refer to directly
I was trying to serialize paths to edn as a tagged thing so that I could read it back into a path. I expect to be told that it's a bad idea and I should put it in a defrecord instead or not do it at all.
(defmethod print-method sun.nio.fs.WindowsPath [c ^java.io.Writer w]
(spit w (str "#path " (pr-str (str c)))))
Currently, I'm trying to just not need to do it at all.print-method
dispatches by the class and I don't know how to make that work with a superclass.
user=> (defmethod print-method java.nio.file.Path [c ^java.io.Writer w] (spit w (str "#path " (pr-str (str c)))))
#object[clojure.lang.MultiFn 0x1d24865a "[email protected]"]
user=> (pr-str (fs/path "."))
"#path \".\""
Hi, is there a full example for a babashka.nrepl middleware? I am currently unclear about the concepts. Can I define a middleware in a bb program or do I need to compile a bb with my middleware?
E.g. by adding the middleware as a bb.edn dependency and then evaluating it. There is a programmatic way of starting the nrepl server already in: babashka.nrepl.server/start-server!
where users could maybe pass the middleware or so
I'm still in the process of understanding. babashka.nrepl.server/start-server!
can be passed an :xform
, where I pass middleware, correct?
And my middleware I can hypothetically load in user space? I checked and I can load almost all of cider.nrepl (middleware) with bb
yes, you can pass an xform, but I'm not sure if this works in user space. you can view the babashka nrepl tests how this works, it's transducer-based
I see. Yea I need to bend my mind into juggleling the 2 phases of graalvm program and user space. So those examples I make work by either running a clojure program (already did) or compiling them with graalvm (basically a bb version with middleware?)
I do this using clj -M:babashka/dev
:
:babashka/dev {:paths []
:deps {babashka/babashka {:local/root "/Users/borkdude/dev/babashka"}}
:main-opts ["-m" "babashka.main"]
}
That alias is in my ~/.clojure/deps.edn
The middleware xform implementation isn't something that I came up with myself but was designed by @U7RJTCH6J - perhaps he has ideas how to enable middleware from the JVM nrepl ecosystem
it would be sweet if we could just re-use the cider inspector middleware for example
I suspect there will be a way to transform a JVM nREPL middleware function to a bb-middleware compatible thing, at least, I hope
else we could maybe implement another stack of JVM middleware on top of the xform stuff
maybe it would be good to start with the simplest / dumbest middleware example and try to make that work
yea first a user defined middleware that logs all requests to file for example. The cider inspect
yeah or even a middleware which logs all requests using timbre, the debugging middleware :)
I think the exposed nrepl server function should just take the (fn [handler msg])
kind of middleware and not expose this :xform
stuff to users since it's more or less for people who want to implement a babashka-like thing, but bb users are likely not familiar with that
maybe the babashka.nrepl library should also support passing those, as an alternative to :xform
As long as babashka.nrepl.server/start-server!
is available from within the sci context, I think passing the xform based middleware should work.
> it would be sweet if we could just re-use the cider inspector middleware for example When I looked into this previously. It didn't seem like it was possible to reuse existing cider middleware without pulling a bunch of dependencies. If you're pulling in a bunch of dependencies, then why not just use cider's nrepl implementation?
> I think the exposed nrepl server function should just take the (fn [handler msg])
kind of middleware and not expose this :xform
stuff
The xform
and the handler
forms are mostly just rearrangements of the same stuff:
(def my-handler-middleware
(fn [handler msg]
(handler response)))
(defn my-xform-middleware
(fn [rf]
(fn [result msg]
(rf result response))))
The main difference is that instead of calling handler
, you call (rf result ...)
. Some of the nice things about the transducer approach is that there's a place to init state and it's much easier to test since you can just transduce requests into a vector of responses.> If you're pulling in a bunch of dependencies, then why not just use cider's nrepl implementation?
Because those would not be babashka's own dependencies, but dependencies in bb.edn
in user space
So to convert a handler to an xform-middleware you can do:
(defn handler->xform-middleware [handler]
(fn [rf]
(fn [result msg]
(rf result (handler identity msg))))
?I was looking back at the old issue, https://github.com/babashka/babashka.nrepl/issues/11#issuecomment-879300306. I think you can convert it with:
(defn middleware->transducer
([middleware]
(fn [rf]
(fn
([] (rf))
([result] (rf result))
([result input]
((middleware #(rf result %)) input))))))
@U02CV2P4J6S Is that enough for you to work with? I think any custom middleware should go through that function and then be "appended" to the default xform middlewares
@U04V15CAJ for loading the middleware. Maybe we pass a list of symbols with --middleware
, then in babashka.main.clj we resolve those with the sci context right?
Maybe then we produce a middleware handler that in turn uses sci/eval
?