about interceptor chains 🧵
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})))
222
could we peek into the chain to see what is actually happening?
maybe something like this:
;; tune the chain so that we get data tap>'ed
((requiring-resolve 'malli.dev/-capture-interceptor))add a tap:
(add-tap (fn [x] (clojure.pprint/pprint (update x :schema m/type)))){: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 :math3something 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}order of tapping seems to be odd 🤔