Skip to content

Commit

Permalink
Support overrides on coerce-structure
Browse files Browse the repository at this point in the history
  • Loading branch information
wilkerlucio committed Aug 3, 2017
1 parent 2710118 commit ca513c2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/spec_coerce/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -273,14 +273,18 @@
:coercion any?)
:ret qualified-keyword?)

(defn coerce-structure [x]
(defn coerce-structure
"Recursively coerce map values on a structure."
(walk/prewalk (fn [x]
(if (map? x)
(with-meta (into {} (map (fn [[k v]] [k (coerce k v)])) x)
(meta x))
x))
x))
([x] (coerce-structure x {}))
([x {::keys [overrides]}]
(walk/prewalk (fn [x]
(if (map? x)
(with-meta (into {} (map (fn [[k v]]
(let [coercion (get overrides k k)]
[k (coerce coercion v)]))) x)
(meta x))
x))
x)))

(s/fdef coerce-structure
:args (s/cat :x any?)
Expand Down
10 changes: 10 additions & 0 deletions test/spec_coerce/core_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,14 @@
:sub {::infer-int "42"}})
{::some-coercion 321
::not-defined "bla"
:sub {::infer-int 42}}))
(is (= (sc/coerce-structure {::some-coercion "321"
::not-defined "bla"
:unqualified "12"
:sub {::infer-int "42"}}
{::sc/overrides {::not-defined `keyword?
:unqualified ::infer-int}})
{::some-coercion 321
::not-defined :bla
:unqualified 12
:sub {::infer-int 42}})))

0 comments on commit ca513c2

Please sign in to comment.