Skip to content

Commit

Permalink
remove 2-arity encode
Browse files Browse the repository at this point in the history
  • Loading branch information
ikitommi committed May 8, 2018
1 parent b605c15 commit 2ee54e7
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 31 deletions.
10 changes: 0 additions & 10 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,6 @@
(st/decode ::spec $ st/string-transformer))
; :kikka

(as-> "KiKka" $
(st/decode ::spec $ st/string-transformer)
(st/encode ::spec $))
; :clojure.spec.alpha/invalid

(as-> "KiKka" $
(st/decode ::spec $ st/string-transformer)
(st/encode ::spec $ st/string-transformer))
Expand Down Expand Up @@ -96,11 +91,6 @@ no, as there can be multiple valid representations for a encoded value. But it's
(st/decode inst? $ st/string-transformer))
; #inst"2014-02-18T18:25:37.000-00:00"

(as-> "2014-02-18T18:25:37Z" $
(st/decode inst? $ st/string-transformer)
(st/encode inst? $))
; :clojure.spec.alpha/invalid

;; encode using string-transformer
(as-> "2014-02-18T18:25:37Z" $
(st/decode inst? $ st/string-transformer)
Expand Down
10 changes: 0 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,6 @@ Functions `encode`, `decode`, `explain`, `explain-data`, `conform` and `conform!
(st/decode ::spec $ st/string-transformer))
; :kikka

(as-> "KiKka" $
(st/decode ::spec $ st/string-transformer)
(st/encode ::spec $))
; :clojure.spec.alpha/invalid

(as-> "KiKka" $
(st/decode ::spec $ st/string-transformer)
(st/encode ::spec $ st/string-transformer))
Expand Down Expand Up @@ -240,11 +235,6 @@ no, as there can be multiple valid representations for a encoded value. But it c
(st/decode inst? $ st/string-transformer))
; #inst"2014-02-18T18:25:37.000-00:00"

(as-> "2014-02-18T18:25:37Z" $
(st/decode inst? $ st/string-transformer)
(st/encode inst? $))
; :clojure.spec.alpha/invalid

;; encode using string-transformer
(as-> "2014-02-18T18:25:37Z" $
(st/decode inst? $ st/string-transformer)
Expand Down
16 changes: 7 additions & 9 deletions src/spec_tools/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,13 @@
(defn encode
"Transforms a value (using a [[Transformer]]) from external
format into a value defined by the spec. On error, returns `::s/invalid`."
([spec value]
(encode spec value nil))
([spec value transformer]
(binding [*transformer* transformer, *encode?* true]
(let [spec (into-spec spec)
conformed (s/conform spec value)]
(if (= conformed +invalid+)
+invalid+
(s/unform spec conformed))))))
[spec value transformer]
(binding [*transformer* transformer, *encode?* true]
(let [spec (into-spec spec)
conformed (s/conform spec value)]
(if (= conformed +invalid+)
+invalid+
(s/unform spec conformed)))))

(defn select-spec
"Drops all extra keys out of a Keys spec value. To use this recursively,
Expand Down
4 changes: 2 additions & 2 deletions test/cljc/spec_tools/core_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@
(is (= ::s/invalid (st/decode ::my-spec-map encoded)))
(is (= decoded (st/decode ::my-spec-map decoded))))
(testing "encode fails if no encoder is defined"
(is (= ::s/invalid (st/encode ::my-spec-map invalid)))))
(is (= ::s/invalid (st/encode ::my-spec-map invalid nil)))))
(testing "with transformer"
(testing "decoding is applied before validation, if defined"
(is (= ::s/invalid (st/decode ::my-spec-map encoded my-type-transformer)))
Expand All @@ -302,7 +302,7 @@
(is (= ::s/invalid (st/decode ::my-type-map encoded)))
(is (= decoded (st/decode ::my-type-map decoded))))
(testing "encode fails if no encoder is defined"
(is (= ::s/invalid (st/encode ::my-type-map invalid)))))
(is (= ::s/invalid (st/encode ::my-type-map invalid nil)))))
(testing "with transformer"
(testing "decoding is applied before validation, if defined"
(is (= ::s/invalid (st/decode ::my-type-map encoded my-type-transformer)))
Expand Down

0 comments on commit 2ee54e7

Please sign in to comment.