malli

ikitommi 2025-01-11T19:11:24.634579Z

about interceptor chains 🧵

ikitommi 2025-01-11T19:11:38.865679Z

what does the following return?

(m/decode
 [:int {:decode/math {:enter (partial + 1)}
        :decode/math2 {:enter (partial + 10)
                       :leave (partial * 2)}
        :decode/math3 {:enter (partial + 100)}}]
 0
 (mt/transformer
  (mt/transformer {:name :math})
  (mt/transformer {:name :math2})
  (mt/transformer {:name :math3})))

ikitommi 2025-01-11T19:11:53.388039Z

222

ikitommi 2025-01-11T19:12:27.960069Z

could we peek into the chain to see what is actually happening?

ikitommi 2025-01-11T19:13:28.493299Z

maybe something like this:

;; tune the chain so that we get data tap>'ed
((requiring-resolve 'malli.dev/-capture-interceptor))

ikitommi 2025-01-11T19:17:10.272099Z

add a tap:

(add-tap (fn [x] (clojure.pprint/pprint (update x :schema m/type))))

ikitommi 2025-01-11T19:18:29.258889Z

{:schema :int
 :name :math
 :phase :enter
 :input 0
 :output 1}

{:schema :int
 :name :math2
 :phase :enter
 :input 1
 :output 11}

{:schema :int,
 :name [:math :math2],
 :phase :enter,
 :input 0,
 :output 11}

{:schema :int
 :name :math3
 :phase :enter
 :input 11
 :output 111}

{:schema :int,
 :name [[:math :math2] :math3],
 :phase :enter,
 :input 0,
 :output 111}

{:schema :int
 :name :math2
 :phase :leave
 :input 111
 :output 222}

{:schema :int,
 :name [:math :math2],
 :phase :leave,
 :input 111,
 :output 222}

{:schema :int,
 :name [[:math :math2] :math3],
 :phase :leave,
 :input 111,
 :output 222}
:name here is the composition of different transforming functions • :math is just the one • [:math :math2] is their composition (does nothing, just prints thre result) • [[:math :math2] :math3] is composition of previous with :math3

ikitommi 2025-01-11T19:50:28.621069Z

something more real-life:

(def Address
  [:map
   [:id string?]
   [:tags [:set keyword?]]
   [:address
    [:map
     [:street string?]
     [:city string?]
     [:zip int?]]]])

(m/decode
 Address
 {:id "Lillan",
  :tags ["coffee" "artesan" "garden"],
  :address {:street "Ahlmanintie 29"
            :city "Tampere"
            :zip 33100}}
 (mt/json-transformer))
;{:id "Lillan"
; :tags #{:coffee :artesan :garden}
; :address {:street "Ahlmanintie 29"
;           :city "Tampere"
;           :zip 33100}}
… taps…
;{:schema :set,
; :name :json,
; :phase :enter,
; :input ["coffee" "artesan" "garden"],
; :output #{"coffee" "garden" "artesan"}}
;
;{:schema keyword?,
; :name :json,
; :phase :enter,
; :input "coffee",
; :output :coffee}
;
;{:schema keyword?,
; :name :json,
; :phase :enter,
; :input "garden",
; :output :garden}
;
;{:schema keyword?,
; :name :json,
; :phase :enter,
; :input "artesan",
; :output :artesan}
;
;{:schema int?
; :name :json
; :phase :enter
; :input 33100
; :output 33100}

ikitommi 2025-01-11T20:04:42.884759Z

order of tapping seems to be odd 🤔

ikitommi 2025-01-11T20:07:54.977669Z

https://github.com/metosin/malli/pull/1160