Fork me on GitHub
#babashka
<
2020-09-03
>
hugod12:09:43

Is there any way to extend fipp’s IEdn protocol in babashka, to control pretty printing?

borkdude12:09:41

Currently fipp is used as a stand-in for clojure.pprint because clojure.pprint didn't work well with GraalVM at the time. I'm not sure if we expose fipp itself as well

hugod12:09:41

Just looking for a way to control pretty printing, or even printing

hugod12:09:40

I’m trying to output a tagged literal for regexes

borkdude12:09:36

Can you give an example?

borkdude12:09:03

Of how you would do it on the JVM I mean

hugod12:09:38

What I naively tried was

(extend-protocol fipp.ednize/IEdn
  java.util.regex.Pattern
  (-edn [x]
    (tagged-literal "regex" (pr-str x))))
I have an aero reader for a #regex tag, but need to be able to round trip values.

hugod12:09:22

Let me see if that works on JVM

hugod12:09:55

mm, doesn’t seem to do what I expected

borkdude12:09:06

user=> (defmethod print-method java.util.regex.Pattern [v ^java.io.Writer w] (.write w (pr-str (tagged-literal 'regex (.pattern v)))))
#object[clojure.lang.MultiFn 0x6aef4eb8 "[email protected]"]
user=> (clojure.pprint/pprint #"foo")
#regex "foo"

borkdude12:09:16

something like this?

borkdude12:09:26

You can also postwalk your data first and turn it into a tagged literal maybe, since overriding print-method for core types might not be a good idea. But it might be OK for scripts

borkdude12:09:06

I'm surprised bb supports this btw, I didn't expect it :)

hugod12:09:30

OK, I just need to explicitly call fipp.ednize/edn.

borkdude12:09:10

what about the above?

hugod12:09:05

postwalk is certainly a viable workaround. ednize isn’t working as it refers to ThreadLocal.

borkdude12:09:37

What about the above print-method ?

hugod12:09:53

Oh, sorry missed that. I thought I tried that before. Let me see

borkdude12:09:51

It does work on my machine's bb

hugod12:09:25

OK, sorry for the noise. That does indeed work nicely 🙂

borkdude12:09:30

No problem, I'm learning something myself too

borkdude12:09:49

This could be useful for preserving the old behavior in all other cases:

(def ^:dynamic *print-config* nil)

(def old-method (get-method print-method java.util.regex.Pattern))

(defmethod print-method java.util.regex.Pattern [v ^java.io.Writer w]
  (if *print-config*
    (.write w (pr-str (tagged-literal 'regex (.pattern v))))
    (old-method v w)))

(prn #"foo")
(binding [*print-config* true]
  (prn #"foo"))

👍 1
borkdude12:09:53

But we might and then we could also expose more of it maybe

unbalanced22:09:35

hey @borkdude. Babashaka rocks! Thanks!

unbalanced22:09:23

might be a trap, but it feels like with a shockingly small amount of work this could be a monster alternative to Ansible. I tried it today but I don't know enough about executing remote ssh commands effectively, but I'm absolutely gonna check it out

unbalanced22:09:03

although I suppose if I start going down that rabbithole I should probably just use clojure hmmm

unbalanced22:09:09

but it's sooo tempting

sogaiu23:09:24

@goomba i don't know what ansible does in detail, but perhaps there is some effort that addresses some of the functionality here? https://github.com/lread/clj-graal-docs/blob/master/doc/external-resources.md may be https://github.com/epiccastle/spire ?

🔥 1
unbalanced23:09:12

awesommmmeee resources!

sogaiu05:09:52

we have the #graalvm folks and others to thank 🙂

unbalanced23:09:32

wooo okay spire was probably what I was looking for. I was using babashka today to provision about a dozen machines

unbalanced23:09:21

The tempting part from an enterprise perspective is I can sneak babashka even onto a pretty locked down redhat where they might not be crazy about JVM (for whatever reason)

unbalanced23:09:26

Enterprise is so weird