Skip to content

Commit

Permalink
fix key-transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
ikitommi committed Jul 14, 2020
1 parent 4cf197b commit 134ff69
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/malli/transform.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -303,16 +303,16 @@
:encoders {:map transform}}))))

(defn key-transformer [{:keys [decode encode]}]
(let [transform (fn [f]
(if f {:leave (fn [x]
(let [transform (fn [f stage]
(if f {stage (fn [x]
(if (map? x)
(reduce-kv
(fn [m k v] (assoc m (f k) v))
(empty x) x)
x))}))]
(transformer
{:decoders {:map (transform decode)}
:encoders {:map (transform encode)}})))
{:decoders {:map (transform decode :enter)}
:encoders {:map (transform encode :leave)}})))

(defn default-value-transformer []
(let [get-default (fn [schema] (some-> schema m/properties :default))
Expand Down
15 changes: 14 additions & 1 deletion test/malli/transform_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,20 @@
(is (= {"x_key" 18 "y_key" "john" "a_key" "doe"}
(m/encode [:map [:x int?] [:y string?] [[:opt :z] boolean?]]
{:x 18 :y "john" :a "doe"}
key-transformer))))))
key-transformer)))))

(testing "from strings and back"
(let [schema [:map
[:id :string]
[:github-followers pos-int?]]
transformer (mt/transformer
(mt/key-transformer {:decode keyword, :encode name})
(mt/string-transformer))
value {"id" "123", "github-followers" "10"}]
(is (= {:id "123", :github-followers 10}
(as-> value $ (m/decode schema $ transformer))))
(is (= value
(as-> value $ (m/decode schema $ transformer) (m/encode schema $ transformer)))))))

(deftest interceptor-style-transformers
(testing "map"
Expand Down

0 comments on commit 134ff69

Please sign in to comment.