Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: accept vectors of size 2 in addition to MapEntry in unparse #1140

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
6 changes: 3 additions & 3 deletions src/malli/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nth might be more equivalent in performance here since I think first needs to go via seq.

::invalid)
::invalid))))
(-transformer [this transformer method options]
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion src/malli/impl/util.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down
5 changes: 5 additions & 0 deletions test/malli/core_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)))
Expand Down