Skip to content

Commit

Permalink
Try macro variant
Browse files Browse the repository at this point in the history
  • Loading branch information
thumbnail committed Sep 19, 2022
1 parent 98ff9c9 commit 96ad72f
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Utilities for [clojure.spec](https://github.com/clojure/spec.alpha).

## Installation

Make sure `[spec-coerce "1.0.0-alpha9"]` (or similar) is present on the classpath if you want to use `nedap.util.spec.api/coerce-map-indicating-invalidity`.
Make sure `[spec-coerce "1.0.0-alpha9"]` (or similar) is present on the classpath and loaded if you want to use `nedap.util.spec.api/coerce-map-indicating-invalidity`.

#### Coordinates

Expand Down
9 changes: 5 additions & 4 deletions src/nedap/utils/spec/api.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
(:require
#?(:clj [clojure.spec.alpha :as spec] :cljs [cljs.spec.alpha :as spec])
[nedap.utils.spec.impl.check]
[nedap.utils.spec.impl.spec-coerce :refer [spec-coerce-available?]])
#?(:cljs (:require-macros [nedap.utils.spec.api :refer [check!]])))
[nedap.utils.spec.impl.spec-coerce :refer [when-spec-coerce-available?]])
#?(:cljs (:require-macros [nedap.utils.spec.api :refer [check!]]
[nedap.utils.spec.impl.spec-coerce :refer [when-spec-coerce-available?]])))

#?(:clj
(defmacro check!
Expand All @@ -16,7 +17,7 @@
{:pre [(-> args count even?)]}
`(nedap.utils.spec.impl.check/check! ~@args)))

(when (spec-coerce-available?)
(when-spec-coerce-available?
(defn coerce-map-indicating-invalidity
"Tries to coerce the map `m` according to spec `spec`.
Expand All @@ -27,6 +28,6 @@
;; Else spec-coerce will fail to coerce things.
{:pre [(check! qualified-ident? spec
map? m)]}
(let [m ((requiring-resolve 'spec-coerce.core/coerce) spec m)]
(let [m ((resolve 'spec-coerce.core/coerce) spec m)]
(cond-> m
(not (spec/valid? spec m)) (assoc ::invalid? true)))))
13 changes: 7 additions & 6 deletions src/nedap/utils/spec/impl/spec_coerce.cljc
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
(ns nedap.utils.spec.impl.spec-coerce)

(defn spec-coerce-available? []
(try
(require 'spec-coerce.core)
true
(catch Exception _
false)))
(defmacro when-spec-coerce-available? [& body]
(when (try
(requiring-resolve 'spec-coerce.core/coerce)
true
(catch Exception _
false))
`(do ~@body)))
1 change: 1 addition & 0 deletions test/unit/nedap/utils/spec/api.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#?(:clj [clojure.spec.alpha :as spec] :cljs [cljs.spec.alpha :as spec])
#?(:clj [clojure.string :as str])
#?(:clj [clojure.test :refer [deftest testing are is use-fixtures]] :cljs [cljs.test :refer-macros [deftest testing is are] :refer [use-fixtures]])
[spec-coerce.core]
[nedap.utils.spec.api :as sut]))

(def validation-failed #"Validation failed")
Expand Down

0 comments on commit 96ad72f

Please sign in to comment.