From 0c7aacbdb0f96dbd9e78beb1862578f488c65697 Mon Sep 17 00:00:00 2001 From: Joel Kaasinen Date: Thu, 28 Nov 2024 11:03:04 +0200 Subject: [PATCH 1/3] feat: accept vectors of size 2 in addition to MapEntry in unparse fixes #1123 --- src/malli/core.cljc | 6 +++--- src/malli/impl/util.cljc | 2 +- test/malli/core_test.cljc | 5 +++++ 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/malli/core.cljc b/src/malli/core.cljc index 5ccb8a397..0da353032 100644 --- a/src/malli/core.cljc +++ b/src/malli/core.cljc @@ -872,8 +872,8 @@ (let [unparsers (into {} (map (fn [[k _ c]] [k (-unparser c)])) (-children this))] (fn [x] (if (miu/-tagged? x) - (if-some [unparse (get unparsers (key x))] - (unparse (val x)) + (if-some [unparse (get unparsers (first x))] + (unparse (second x)) ::invalid) ::invalid)))) (-transformer [this transformer method options] @@ -1650,7 +1650,7 @@ (fn [x] (if-some [parser (find (dispatch x))] (parser x) ::invalid)))) (-unparser [_] (let [unparsers (reduce-kv (fn [acc k s] (assoc acc k (-unparser s))) {} @dispatch-map)] - (fn [x] (if (miu/-tagged? x) (if-some [f (unparsers (key x))] (f (val x)) ::invalid) ::invalid)))) + (fn [x] (if (miu/-tagged? x) (if-some [f (unparsers (first x))] (f (second x)) ::invalid) ::invalid)))) (-transformer [this transformer method options] ;; FIXME: Probably should not use `dispatch` ;; Can't use `dispatch` as `x` might not be valid before it has been unparsed: diff --git a/src/malli/impl/util.cljc b/src/malli/impl/util.cljc index 84f3b13ab..233391e15 100644 --- a/src/malli/impl/util.cljc +++ b/src/malli/impl/util.cljc @@ -6,7 +6,7 @@ (def ^:const +max-size+ #?(:clj Long/MAX_VALUE, :cljs (.-MAX_VALUE js/Number))) (defn -tagged [k v] #?(:clj (MapEntry. k v), :cljs (MapEntry. k v nil))) -(defn -tagged? [v] (instance? MapEntry v)) +(defn -tagged? [v] (or (instance? MapEntry v) (and (vector? v) (= 2 (count v))))) (defn -invalid? [x] #?(:clj (identical? x :malli.core/invalid), :cljs (keyword-identical? x :malli.core/invalid))) (defn -map-valid [f v] (if (-invalid? v) v (f v))) diff --git a/test/malli/core_test.cljc b/test/malli/core_test.cljc index 8c4641381..68b261ca7 100644 --- a/test/malli/core_test.cljc +++ b/test/malli/core_test.cljc @@ -257,7 +257,9 @@ (is (= (miu/-tagged :pos 1) (m/parse schema* 1))) (is (= ::m/invalid (m/parse schema* 0))) (is (= 1 (m/unparse schema* (miu/-tagged :pos 1)))) + (is (= 1 (m/unparse schema* [:pos 1]))) (is (= ::m/invalid (m/unparse schema* (miu/-tagged :pos 0)))) + (is (= ::m/invalid (m/unparse schema* [:pos 0]))) (doseq [schema [schema schema*]] (testing (m/form schema) @@ -1151,8 +1153,11 @@ (is (= ::m/invalid (m/parse schema invalid5))) (is (= ::m/invalid (m/parse schema invalid6))) (is (= valid1 (m/unparse schema (m/parse schema valid1)))) + (is (= valid1 (m/unparse schema [:sized valid1]))) (is (= valid2 (m/unparse schema (m/parse schema valid2)))) + (is (= valid2 (m/unparse schema [:human valid2]))) (is (= valid3 (m/unparse schema (m/parse schema valid3)))) + (is (= valid3 (m/unparse schema [:sized valid3]))) (is (= ::m/invalid (m/unparse schema invalid1))) (is (= ::m/invalid (m/unparse schema invalid2))) (is (= ::m/invalid (m/unparse schema invalid3))) From cb60f34e53a5a24e5acfae123fa88c3703957c20 Mon Sep 17 00:00:00 2001 From: Joel Kaasinen Date: Thu, 28 Nov 2024 11:06:27 +0200 Subject: [PATCH 2/3] doc: update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e98e7f895..1105214b8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ Malli is in well matured [alpha](README.md#alpha). ## 0.17.0 (UNRELEASED) +* `unparse` accepts vectors in addition to MapEntry values for `:orn` and `:multi` [#1123](https://github.com/metosin/malli/issues/1123) * Don't output `:definitions nil` in swagger. [#1134](https://github.com/metosin/malli/issues/1134) * **BREAKING**: `:gen/fmap` property requires its schema to create a generator. * previous behavior defaulted to a `nil`-returning generator, even if the schema doesn't accept `nil` From 694716273c252ea6fe45d61804736c83e516018f Mon Sep 17 00:00:00 2001 From: Joel Kaasinen Date: Fri, 29 Nov 2024 08:19:01 +0200 Subject: [PATCH 3/3] perf: prefer nth over first/second --- src/malli/core.cljc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/malli/core.cljc b/src/malli/core.cljc index 0da353032..bfe45f407 100644 --- a/src/malli/core.cljc +++ b/src/malli/core.cljc @@ -872,8 +872,8 @@ (let [unparsers (into {} (map (fn [[k _ c]] [k (-unparser c)])) (-children this))] (fn [x] (if (miu/-tagged? x) - (if-some [unparse (get unparsers (first x))] - (unparse (second x)) + (if-some [unparse (get unparsers (nth x 0))] + (unparse (nth x 1)) ::invalid) ::invalid)))) (-transformer [this transformer method options] @@ -1650,7 +1650,7 @@ (fn [x] (if-some [parser (find (dispatch x))] (parser x) ::invalid)))) (-unparser [_] (let [unparsers (reduce-kv (fn [acc k s] (assoc acc k (-unparser s))) {} @dispatch-map)] - (fn [x] (if (miu/-tagged? x) (if-some [f (unparsers (first x))] (f (second x)) ::invalid) ::invalid)))) + (fn [x] (if (miu/-tagged? x) (if-some [f (unparsers (nth x 0))] (f (nth x 1)) ::invalid) ::invalid)))) (-transformer [this transformer method options] ;; FIXME: Probably should not use `dispatch` ;; Can't use `dispatch` as `x` might not be valid before it has been unparsed: