Fork me on GitHub
#malli
<
2019-12-07
>
roklenarcic00:12:59

hm ok… but should the`-transformer` function return a function that returns transform function? Because so far it just seems that it brings a lot of constantly use

roklenarcic00:12:13

I see so much constantly use that I wonder what is the other case (i.e. non-constantly)

ikitommi03:12:04

-transformer already sees the schema, so it can return directly an .... interceptor? The later comment shows how we can get rid of vobstantly in the schema properties too: https://github.com/metosin/malli/issues/136#issuecomment-562731476

ikitommi11:12:43

the current internal vocabulary is quite messy, would these be good names for the future: * method :decode :encode * stage :enter :leave * context :json :string :before :after ...

ikitommi12:12:12

:enter/:leave and chain of contexts enable same kind of things. Wondering do we need them both…

(def transformer (mt/transformer {:name :before} mt/string-transformer {:name :after}))

(m/decode 
  [:and 
   {:decode/after 'inc} 
   int?] 
  "1" 
  transformer)
; => 2

; :enter
;  :before
;  :strip-keys
;  :string => ->int "1" => 1
;  :after => inc 1 => 2
; :leave
;  :after
;  :string
;  :strip-keys
;  :before

(m/decode 
  [:and 
   {:decode/before {:leave 'inc}}
   int?]
  "1" 
  transformer)
; => 2

; :enter
;  :before
;  :strip-keys
;  :string => ->int "1" => 1
;  :after
; :leave
;  :after
;  :string
;  :strip-keys
;  :before => inc 1 => 2 

ikitommi16:12:05

Did a small spike, seems to work, so PR out. Simplifies the Transformer Protocol and enables chaining of tranformers (instead of overriding schema-based encoder & decoders like before). Also, better naming of things https://github.com/metosin/malli/pull/137

ikitommi16:12:15

(m/decode
  [int? {:decode/before '(constantly {:leave inc})
         :decode/after '(constantly (partial * 2))}]
  "10"
  (mt/transformer
    {:name :before}
    mt/string-transformer
    {:decoders {'int? (constantly inc)}} ;; anonymous
    {:name :after}))
; => 23

;; :enter
;;  :string   "10" => 10
;;  anonymous   10 => 11
;;  :after      11 => 22
;; :leave
;;  :before     22 => 23