Fork me on GitHub
#malli
<
2022-06-28
>
Derek00:06:47

Cross-posting from #announcements : Announcing org.passen/malapropism. A small library for configuration data backed by malli https://github.com/dpassen/malapropism

🎉 1
Alexander Moskvichev04:06:16

Is there an easy way to change default date format in malli.transofm? I've read several discussions about custom registry, etc, but just started with malli, it's too hard for now to understand. I use malli with reitit, just need to change outgoing date format

Setzer2217:06:26

Does malli support custom function predicates? I noticed some functions seem to be supported but most of them give an "invalid schema" error and I don't know if there's another way of doing this

Setzer2217:06:59

actually, what I need is define a schema for a map that contains a core.async channel. I can always use :any but it would've been nice to use something like #(instance? ManyToManyChannel %)

Setzer2217:06:23

Also, unrelated question 😄 I'm pretty curious about this: https://github.com/metosin/malli/blob/master/docs/function-schemas.md#tldr In particular, in this snippet:

(defn plus1
  "Adds one to the number"
  {:malli/schema [:=> [:cat :int] :int]}
  [x] (inc x))
What's this way of using metadata to define the schema of the function via defn? Is this a defn-like macro defined somewhere?

skynet17:06:03

@jsanchezf if I understand, it's the normal Clojure defn and that's the attr-map? argument which becomes metadata https://clojuredocs.org/clojure.core/defn

Setzer2217:06:03

@skynet But then, how does it work? Malli has no way of knowing when a function is redefined. Simply defining some metadata does not lead to instrumentation happening

dvingo18:06:42

(dev/start! {:report (pretty/reporter)})
check the source of that call for the answer 🙂

dvingo18:06:35

> Without instrumentation turned on, there is no schema enforcement:

dvingo18:06:54

> defn schemas can be defined with standard Var metadata. It allows defn schema documentation and instrumentation without dependencies to malli itself from the functions. It's just data.

Setzer2218:06:35

I see, so looking at the source (https://github.com/metosin/malli/blob/b745b73a93109a71643ce58302189a6f69c56d5e/src/malli/dev.clj#L15) it walks all the namespaces at the time where you call dev/start! , then looks for instrumented functions. But how will it know when I create a new function or define a new namespace? Do I need to call start! again for it to pick it up?

ikitommi07:06:07

yes, you need to call start! again when you annotate new functions. Tried to hook Var-watching for already defined schematized defns, but the Clojure core doesn’t support that easily. Calling start! makes the collecting explicit.

ingesol21:06:14

I’m using malli to instrument functions and print results using the pretty printer. Some functions accept and return huge maps, so huge that it makes the output of the pretty printer unusable. Any hints on how to improve this?

👍 2
eskos06:06:17

https://github.com/greglook/puget can make pretty prints even more prettier; sometimes just adding color helps a lot • https://github.com/lambdaisland/deep-diff2 (or clojure.data/diff) can be used to diff the content; with a bit of trickery you can print only the interesting parts

ikitommi07:06:22

@U8SFC8HLP Puget in nice, but Malli uses a custom pretty printer directly on top of fipp - different opinions about colors & works with CLJS too. About the huge maps, currently there is no omitting of valid values (https://github.com/bhb/expound#show-valid-values), would have needed that too, but have had no time to implement. PR would be welcome on this. Also, I think it’s the final piece before extracting the pretty printing from reitit & malli into a clean and minimal lib (https://github.com/metosin/virhe).

ingesol07:06:31

I think showing valid values is useful to get the full picture. It also makes the report easier to parse sometimes. If there are 4 args to the fn, and 1 is wrong, it can be easier on the eyes to navigate to the wrong one when you use the valid ones as guides.

ikitommi07:06:47

ok, I’m all ears on how to make it better then.

ingesol07:06:55

In my case, I have a re-frame app-db. The number of keys in the map is not huge, but the number of nested keys is. So some way of sniffing out the approximate size of the map and deciding to print the first 500 chars or something would be nice. That was what I was looking for in my question. Would be nice if fipp had that option.

ingesol07:06:26

From reading the code, it looks like we just pass the map to fipp for printing?

ingesol07:06:13

but virhe isn’t currently used in malli, is it? I suppose maybe I can use it to implement an alternative to the default pretty printer?

ikitommi07:06:38

virhe is… inlined 🙂

(ns malli.dev.virhe
  "initial code for "

ingesol07:06:59

so… what I’m asking for is already available?

ikitommi07:06:07

actual virhe-repo is just a README, “copy code here when it’s ok”

ikitommi07:06:36

you can either swap the EDNPrinter or pass options to it to work differently

ikitommi07:06:42

all code is in malli repo.

ingesol07:06:06

awesome, thanks! I just didn’t notice that part of the API. Will have a go at this 🙂

ingesol09:06:51

In case it’s useful for anyone, here’s the change I needed. I wanted to avoid printing the re-frame app-db. Both because the contents are usually not interesting, and because it’s way too large to be printed effectively. In the case of a value not matching the schema of our app db, it would usually be something that does not match the check here so it would be printed anyway.

(extend-type v/EdnPrinter
  fv/IVisitor
  (visit-map [this x]
    (if (:some-key-always-in-our-db x)
      ;; Avoid printing app db to console, as it is way too large
      (v/-color :text "{... app db ...}" x)
      ;; Inlining original implementation from EdnPrinter
      (let [xs (sort-by identity (fn [a b] (arr/rank (first a) (first b))) x)]
        (fe/pretty-coll this (v/-color :text "{" this) xs [:span (v/-color :text "," this) :line] (v/-color :text "}" this)
                        (fn [printer [k v]]
                          [:span (fv/visit printer k) " " (fv/visit printer v)]))))))

👍 3
mikerod18:09:14

Sadly, the same cannot be applied to the CLJ side because the .virhe.EdnPrinter record type directly implements the fipp.visit/IVisitor protocol-generated interface & extend-type doesn’t allow reimplementations when that is the case. eg.

(defprotocol MyProto
  (action [this]))
(defrecord MyRecord []
  MyProto
  (action [this]  this))
(extend-type MyRecord
  MyProto
  (action [this] ::NEW))
;; Execution error (IllegalArgumentException) at user/eval651681 (...)
;; class user.MyRecord already directly implements user.MyProto for protocol:#'user/MyProto
I don’t see much of a way around this other than reimplementing the printer and ensuring that gets passed along through whatever printing call chain of interests. 😞

steveb8n22:06:51

maybe #portal?