Skip to content

Commit

Permalink
Optional argument default-fn to default-value-transformer
Browse files Browse the repository at this point in the history
See an example usage here:

https://github.com/piotr-yuxuan/malli-cli/blob/main/src/piotr_yuxuan/malli_cli.clj#L220-L222

This doesn't change existing behaviour but only add `default-fn`
optional argument. When set, this function is applied on the default
value found under key `key`. This optional, additional indirection
allows a seamless way to define `:env-var` defaults.
  • Loading branch information
piotr-yuxuan committed Nov 23, 2021
1 parent c8e2b2e commit b88b713
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/malli/transform.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@
(defn default-value-transformer
([]
(default-value-transformer nil))
([{:keys [key defaults] :or {key :default}}]
([{:keys [key default-fn defaults] :or {key :default, default-fn identity}}]
(let [get-default (fn [schema]
(if-some [e (some-> schema m/properties (find key))]
(constantly (val e))
Expand All @@ -402,7 +402,9 @@
(reduce-kv
(fn [acc k f]
(if-not (contains? x k)
(assoc acc k (f))
(if-let [d (default-fn (f))]
(assoc acc k d)
acc)
acc))
x defaults)
x)))))}]
Expand Down

0 comments on commit b88b713

Please sign in to comment.