Skip to content

Commit

Permalink
Merge pull request #517 from bsless/faster-registry
Browse files Browse the repository at this point in the history
Faster registry
  • Loading branch information
ikitommi authored Aug 23, 2021
2 parents 802f74d + 0f22aa4 commit 1055b3c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
6 changes: 5 additions & 1 deletion src/malli/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,12 @@
(assoc @v schema))))

(defn -registry
{:arglists '([] [{:keys [registry]}])}
([] default-registry)
([{:keys [registry]}] (or (mr/registry registry) default-registry)))
([opts]
(or (when opts
(mr/registry (opts :registry)))
default-registry)))

(defn- -lookup [?schema options]
(let [registry (-registry options)]
Expand Down
18 changes: 14 additions & 4 deletions src/malli/registry.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,22 @@
(defn simple-registry [schemas]
(reify
Registry
(-schema [_ type] (get schemas type))
(-schema [_ type] (schemas type))
(-schemas [_] schemas)))

(defn registry [?registry]
(cond (satisfies? Registry ?registry) ?registry
(map? ?registry) (simple-registry ?registry)))
(defn registry
[?registry]
#?(:clj
(when ?registry
(if (instance? malli.registry.Registry ?registry)
?registry
(if (map? ?registry)
(simple-registry ?registry)
(when (satisfies? Registry ?registry)
?registry))))
:cljs
(cond (satisfies? Registry ?registry) ?registry
(map? ?registry) (simple-registry ?registry))))

;;
;; custom
Expand Down
2 changes: 1 addition & 1 deletion test/malli/core_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -1435,7 +1435,7 @@
(and (= (m/validate s v) (nil? es))
(results= (m/explain s v) (and es {:schema s, :value v, :errors es}))
(= (m/parse s v) (if (nil? es) (if (= typ :cat) v v*) ::m/invalid))
(or (some? es) (= (m/unparse s (if (= typ :cat) v v*) s) v))))
(or (some? es) (= (m/unparse s (if (= typ :cat) v v*)) v))))

0 nil [{:path [], :in [], :schema s, :value 0, :type ::m/invalid-type}]
"foo" nil [{:path [], :in [], :schema s, :value "foo", :type ::m/invalid-type}]
Expand Down

0 comments on commit 1055b3c

Please sign in to comment.