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

Add metadata on destructure for symbols #1113

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
30 changes: 23 additions & 7 deletions src/malli/destructure.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -75,19 +75,35 @@
:when f, v vs] (f (namespace k) (str v))))

(defn -keys [{:keys [keys strs syms] :as arg} {:keys [::references] :or {references true} :as options}]
(let [any (fn [f ks] (map (fn [k] [(f k) :any]) ks))]
(let [any (fn [f ks] (map (fn [k] [(f k) :any {:metadata (meta k)}]) ks))]
(->> (concat (any keyword keys) (any str strs) (any identity syms)
(map (fn [k] [k (if (and references (qualified-keyword? k)) k :any)]) (-qualified-keys arg))
(map (fn [[k v]] [v (-transform {:arg k} options false)]) (filter #(miu/-tagged? (key %)) arg)))
(map (fn [k]
[k
(if (and references (qualified-keyword? k))
k
:any)
{:metadata (meta k)}])
(-qualified-keys arg))
(map (fn [[k v]]
[v
(-transform {:arg k} options false)
{:metadata (meta k)}])
(filter #(miu/-tagged? (key %)) arg)))
(distinct))))

(defn -map [arg {:keys [::references ::required-keys ::closed-maps ::sequential-maps]
:or {references true, sequential-maps true} :as options} rest]
(let [keys (-keys arg options)
->entry (fn [[k t]] (let [ref (and references (qualified-keyword? k))]
(cond (and ref required-keys) k
required-keys [k t]
:else (cond-> [k {:optional true}] (not ref) (conj t)))))
->entry (fn [[k t metadata]] (let [ref (and references (qualified-keyword? k)) ]
(if metadata
(cond (and ref required-keys) [k metadata]
required-keys [k metadata t]
:else (cond-> [k (merge {:optional true} metadata)]
(not ref)
(conj t)))
(cond (and ref required-keys) k
required-keys [k t]
:else (cond-> [k {:optional true}] (not ref) (conj t))))))
->arg (fn [[k t]] [:cat [:= k] (if (and references (qualified-keyword? k)) k t)])
schema (cond-> [:map] closed-maps (conj {:closed true}) :always (into (map ->entry keys)))]
(if (or rest sequential-maps)
Expand Down
5 changes: 4 additions & 1 deletion src/malli/experimental.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@
(let [{:keys [name return doc arities] body-meta :meta :as parsed} (m/parse schema args)
var-meta (meta name)
_ (when (= ::m/invalid parsed) (m/-fail! ::parse-error {:schema schema, :args args}))
parse (fn [{:keys [args] :as parsed}] (merge (md/parse args) parsed))
required-keys (or (:malli/required-keys var-meta) (:malli/required-keys body-meta))
parse (fn [{:keys [args] :as parsed}]
(merge (md/parse args {::md/required-keys (boolean required-keys)})
parsed))
->schema (fn [{:keys [schema]}] [:=> schema (:schema return :any)])
single (= :single (key arities))
parglists (if single (->> arities val parse vector) (->> arities val :arities (map parse)))
Expand Down