From 06f4a4d7826bade84077ac27b4c55a6bf62f43e6 Mon Sep 17 00:00:00 2001 From: Tommi Reiman Date: Thu, 7 Sep 2023 14:36:03 +0300 Subject: [PATCH] :enum & := encode correctly --- src/malli/transform.cljc | 24 ++++++++++++++++-------- test/malli/transform_test.cljc | 17 ++++++++++++----- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/src/malli/transform.cljc b/src/malli/transform.cljc index 2daa85801..59ae8ee5f 100644 --- a/src/malli/transform.cljc +++ b/src/malli/transform.cljc @@ -189,11 +189,17 @@ (set? x) (seq x) :else x)) -(defn -infer-child-decoder-compiler [schema _] - (-> schema (m/children) (m/-infer) {:keyword -string->keyword - :symbol -string->symbol - :int -string->long - :double -string->double})) +(defn -infer-child-compiler [method] + (fn [schema _] + (some-> schema + (m/children) + (m/-infer) + {:keyword {:decode -string->keyword + :encode m/-keyword->string} + :symbol {:decode -string->symbol} + :int {:decode -string->long} + :double {:decode -string->double}} + (method -any->string)))) ;; ;; decoders @@ -216,8 +222,8 @@ 'double? -number->double 'inst? -string->date - :enum {:compile -infer-child-decoder-compiler} - := {:compile -infer-child-decoder-compiler} + :enum {:compile (-infer-child-compiler :decode)} + := {:compile (-infer-child-compiler :decode)} :double -number->double :keyword -string->keyword @@ -239,6 +245,9 @@ 'uuid? -any->string + :enum {:compile (-infer-child-compiler :encode)} + := {:compile (-infer-child-compiler :encode)} + :keyword m/-keyword->string :symbol -any->string :qualified-keyword m/-keyword->string @@ -301,7 +310,6 @@ :>= -any->string :< -any->string :<= -any->string - := -any->string :not= -any->string 'double -any->string})) diff --git a/test/malli/transform_test.cljc b/test/malli/transform_test.cljc index d4edc96d8..327aec311 100644 --- a/test/malli/transform_test.cljc +++ b/test/malli/transform_test.cljc @@ -1039,8 +1039,15 @@ :equals2 'kikka :equals3 1 :equals4 1.1}] - (testing "is not enabled by default" - (is (= value (m/decode schema value nil)))) - (testing "works with json and string transformers" - (is (= expected (m/decode schema value (mt/json-transformer)))) - (is (= expected (m/decode schema value (mt/string-transformer))))))) + (testing "decoding" + (testing "is not enabled by default" + (is (= value (m/decode schema value nil)))) + (testing "works with json and string transformers" + (is (= expected (m/decode schema value (mt/json-transformer)))) + (is (= expected (m/decode schema value (mt/string-transformer)))))) + (testing "encoding" + (testing "is not enabled by default" + (is (= expected (m/encode schema expected nil)))) + (testing "works with json and string transformers" + (is (= value (m/encode schema expected (mt/json-transformer)))) + (is (= value (m/encode schema expected (mt/string-transformer))))))))