malli

https://github.com/metosin/malli :malli:
2021-04-06T16:48:21.331Z

Is it possible to create a key in a map using decode?

2021-04-06T16:49:21.331400Z

something like {:a 1 :b 2} => {:c 3}

nilern 2021-04-06T17:27:54.331800Z

Sure it is

nilern 2021-04-06T17:31:58.332Z

(m/decode [:map {:decode/abc (fn [m]
                               (if (and (map? m) (contains? m :a) (contains? m :b))
                                 (-> m
                                     (dissoc :a :b)
                                     (assoc :c (+ (:a m) (:b m))))
                                 m))}
           [:a :int] [:b :int]]
          {:a 1, :b 2}
          (mt/transformer {:name :abc}))
=> {:c 3}

nilern 2021-04-06T17:34:20.332600Z

https://github.com/metosin/malli#value-transformation does have this info although the explanations are a bit sparse

2021-04-06T18:07:14.333Z

I've read it, but somehow it didn't occur to me that I could use decode/encode on the whole map and not on an individual key

2021-04-06T18:07:27.333300Z

great!

nilern 2021-04-06T18:09:05.334100Z

Yes that is the "key" to changing keys :drum_with_drumsticks:

ikitommi 2021-04-06T18:43:20.334200Z

👍 small nitpick: the example could be encode as it transforms the value out of schema. Or the schema should not require :a & :b as they are not in the result.

👍 1
nilern 2021-04-06T18:52:12.334400Z

Indeed