Fork me on GitHub
#babashka
<
2024-07-10
>
Felix Dorner11:07:31

With babashka/cli, trying to figure out a good way to :coerce: coerce string to given type. Can I add my own coercion "types"?

Felix Dorner11:07:12

And if I use :coerce [:type], is there a way to require a minimum argument of 1 element?

borkdude11:07:18

You can provide a function I think

teodorlu11:07:12

Conclusions from a bit of experimentation: • You can add your own coercers as function on the toplevel or inside a vector • I can’t see an easy way to write a coercer that asserts that the collection needs at least one argument.

;; I can parse into a vector that takes a coercing function I've written myself
(babashka.cli/parse-opts ["--items" "one" "two"]
                         {:coerce {:items [(fn [item] (keyword "myns" item))]}})
;; => {:items [:myns/one :myns/two]}

;; With my own toplevel coercer, I just get a single value coerced
(babashka.cli/parse-opts ["--items" "one" "two"]
                         {:coerce {:items (fn [something] something)}})
;; => {:items "one"}
A possibility is to add your own validate function that asserts the things you need after parsing the CLI options,
(defn validate [opts]
  (assert (pos? (count (:items opts))))
  opts)

(-> (babashka.cli/parse-opts ["--items" "one" "two"]
                             {:coerce {:items [(fn [item] (keyword "myns" item))]}})
    validate)
;; => {:items [:myns/one :myns/two]}

(-> (babashka.cli/parse-opts ["--items"]
                             {:coerce {:items [(fn [item] (keyword "myns" item))]}})
    validate)
;; => Execution error (ExceptionInfo) at babashka.cli/parse-opts$fn (cli.cljc:320).
;;    Coerce failure: cannot transform (implicit) true with easy_parallellism_with_pipeline_blocking$eval36639$fn__36640@4a1046de
After all, it’s data 🙂

borkdude11:07:02

nice, thanks for checking @U3X7174KS!

❤️ 1
dabrazhe12:07:43

Just checking in. What’s the current go-to method to deploy and write babshka or Clojure code on AWS Lambda? )

dabrazhe12:07:50

Last time I checked it was blambda. Is this still actual and project active?

borkdude12:07:06

#C04PSBFMMDJ is still active I think -- cc @U054AT6KT

1
gratitude-thank-you 1
dabrazhe12:07:36

I also recall holy-lambda was a contender, but never got to try it out. Any there any other entrants in this space?

borkdude12:07:25

there is also a babashka lambda layer on aws available, but I'm not sure if that's still up to date. I'm sure there's also "create your own" approach possible ;)

✔️ 1
zy C12:07:57

my method is just building a jar and upload to aws. Remember to use aws-lambda’s snapshot feat to reduce coldstart-time.

borkdude12:07:01

I think @U037WH7PKAT is referring to a normal AWS Java lambda

jmglov13:07:52

Yup, Blambda is still alive and I do watch the #C04PSBFMMDJ channel. 🙂

👍 1
didibus09:07:07

Holy-lambda has commits in the last months. Never tried it, but it claims it can also do Clojure.

jmglov10:07:54

Holy Lambda is nice, and can do Babashka, JVM Clojure, and native image.