├── .gitignore ├── LICENSE ├── README.md ├── deps.edn ├── doc └── history.md ├── project.clj ├── src └── spectacles │ ├── impl.cljc │ └── lenses.cljc └── test └── spectacles ├── cljs_self_test_runner.cljs ├── cljs_test_runner.cljs └── impl_test.cljc /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /classes 3 | /checkouts 4 | pom.xml 5 | pom.xml.asc 6 | *.jar 7 | *.class 8 | /.lein-* 9 | /.nrepl-port 10 | .hgignore 11 | .hg/ 12 | out/ 13 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC 2 | LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM 3 | CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT. 4 | 5 | 1. DEFINITIONS 6 | 7 | "Contribution" means: 8 | 9 | a) in the case of the initial Contributor, the initial code and 10 | documentation distributed under this Agreement, and 11 | 12 | b) in the case of each subsequent Contributor: 13 | 14 | i) changes to the Program, and 15 | 16 | ii) additions to the Program; 17 | 18 | where such changes and/or additions to the Program originate from and are 19 | distributed by that particular Contributor. A Contribution 'originates' from 20 | a Contributor if it was added to the Program by such Contributor itself or 21 | anyone acting on such Contributor's behalf. Contributions do not include 22 | additions to the Program which: (i) are separate modules of software 23 | distributed in conjunction with the Program under their own license 24 | agreement, and (ii) are not derivative works of the Program. 25 | 26 | "Contributor" means any person or entity that distributes the Program. 27 | 28 | "Licensed Patents" mean patent claims licensable by a Contributor which are 29 | necessarily infringed by the use or sale of its Contribution alone or when 30 | combined with the Program. 31 | 32 | "Program" means the Contributions distributed in accordance with this 33 | Agreement. 34 | 35 | "Recipient" means anyone who receives the Program under this Agreement, 36 | including all Contributors. 37 | 38 | 2. GRANT OF RIGHTS 39 | 40 | a) Subject to the terms of this Agreement, each Contributor hereby grants 41 | Recipient a non-exclusive, worldwide, royalty-free copyright license to 42 | reproduce, prepare derivative works of, publicly display, publicly perform, 43 | distribute and sublicense the Contribution of such Contributor, if any, and 44 | such derivative works, in source code and object code form. 45 | 46 | b) Subject to the terms of this Agreement, each Contributor hereby grants 47 | Recipient a non-exclusive, worldwide, royalty-free patent license under 48 | Licensed Patents to make, use, sell, offer to sell, import and otherwise 49 | transfer the Contribution of such Contributor, if any, in source code and 50 | object code form. This patent license shall apply to the combination of the 51 | Contribution and the Program if, at the time the Contribution is added by the 52 | Contributor, such addition of the Contribution causes such combination to be 53 | covered by the Licensed Patents. The patent license shall not apply to any 54 | other combinations which include the Contribution. No hardware per se is 55 | licensed hereunder. 56 | 57 | c) Recipient understands that although each Contributor grants the licenses 58 | to its Contributions set forth herein, no assurances are provided by any 59 | Contributor that the Program does not infringe the patent or other 60 | intellectual property rights of any other entity. Each Contributor disclaims 61 | any liability to Recipient for claims brought by any other entity based on 62 | infringement of intellectual property rights or otherwise. As a condition to 63 | exercising the rights and licenses granted hereunder, each Recipient hereby 64 | assumes sole responsibility to secure any other intellectual property rights 65 | needed, if any. For example, if a third party patent license is required to 66 | allow Recipient to distribute the Program, it is Recipient's responsibility 67 | to acquire that license before distributing the Program. 68 | 69 | d) Each Contributor represents that to its knowledge it has sufficient 70 | copyright rights in its Contribution, if any, to grant the copyright license 71 | set forth in this Agreement. 72 | 73 | 3. REQUIREMENTS 74 | 75 | A Contributor may choose to distribute the Program in object code form under 76 | its own license agreement, provided that: 77 | 78 | a) it complies with the terms and conditions of this Agreement; and 79 | 80 | b) its license agreement: 81 | 82 | i) effectively disclaims on behalf of all Contributors all warranties and 83 | conditions, express and implied, including warranties or conditions of title 84 | and non-infringement, and implied warranties or conditions of merchantability 85 | and fitness for a particular purpose; 86 | 87 | ii) effectively excludes on behalf of all Contributors all liability for 88 | damages, including direct, indirect, special, incidental and consequential 89 | damages, such as lost profits; 90 | 91 | iii) states that any provisions which differ from this Agreement are offered 92 | by that Contributor alone and not by any other party; and 93 | 94 | iv) states that source code for the Program is available from such 95 | Contributor, and informs licensees how to obtain it in a reasonable manner on 96 | or through a medium customarily used for software exchange. 97 | 98 | When the Program is made available in source code form: 99 | 100 | a) it must be made available under this Agreement; and 101 | 102 | b) a copy of this Agreement must be included with each copy of the Program. 103 | 104 | Contributors may not remove or alter any copyright notices contained within 105 | the Program. 106 | 107 | Each Contributor must identify itself as the originator of its Contribution, 108 | if any, in a manner that reasonably allows subsequent Recipients to identify 109 | the originator of the Contribution. 110 | 111 | 4. COMMERCIAL DISTRIBUTION 112 | 113 | Commercial distributors of software may accept certain responsibilities with 114 | respect to end users, business partners and the like. While this license is 115 | intended to facilitate the commercial use of the Program, the Contributor who 116 | includes the Program in a commercial product offering should do so in a 117 | manner which does not create potential liability for other Contributors. 118 | Therefore, if a Contributor includes the Program in a commercial product 119 | offering, such Contributor ("Commercial Contributor") hereby agrees to defend 120 | and indemnify every other Contributor ("Indemnified Contributor") against any 121 | losses, damages and costs (collectively "Losses") arising from claims, 122 | lawsuits and other legal actions brought by a third party against the 123 | Indemnified Contributor to the extent caused by the acts or omissions of such 124 | Commercial Contributor in connection with its distribution of the Program in 125 | a commercial product offering. The obligations in this section do not apply 126 | to any claims or Losses relating to any actual or alleged intellectual 127 | property infringement. In order to qualify, an Indemnified Contributor must: 128 | a) promptly notify the Commercial Contributor in writing of such claim, and 129 | b) allow the Commercial Contributor tocontrol, and cooperate with the 130 | Commercial Contributor in, the defense and any related settlement 131 | negotiations. The Indemnified Contributor may participate in any such claim 132 | at its own expense. 133 | 134 | For example, a Contributor might include the Program in a commercial product 135 | offering, Product X. That Contributor is then a Commercial Contributor. If 136 | that Commercial Contributor then makes performance claims, or offers 137 | warranties related to Product X, those performance claims and warranties are 138 | such Commercial Contributor's responsibility alone. Under this section, the 139 | Commercial Contributor would have to defend claims against the other 140 | Contributors related to those performance claims and warranties, and if a 141 | court requires any other Contributor to pay any damages as a result, the 142 | Commercial Contributor must pay those damages. 143 | 144 | 5. NO WARRANTY 145 | 146 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS PROVIDED ON 147 | AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER 148 | EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR 149 | CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A 150 | PARTICULAR PURPOSE. Each Recipient is solely responsible for determining the 151 | appropriateness of using and distributing the Program and assumes all risks 152 | associated with its exercise of rights under this Agreement , including but 153 | not limited to the risks and costs of program errors, compliance with 154 | applicable laws, damage to or loss of data, programs or equipment, and 155 | unavailability or interruption of operations. 156 | 157 | 6. DISCLAIMER OF LIABILITY 158 | 159 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT NOR ANY 160 | CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, 161 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION 162 | LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 163 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 164 | ARISING IN ANY WAY OUT OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE 165 | EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY 166 | OF SUCH DAMAGES. 167 | 168 | 7. GENERAL 169 | 170 | If any provision of this Agreement is invalid or unenforceable under 171 | applicable law, it shall not affect the validity or enforceability of the 172 | remainder of the terms of this Agreement, and without further action by the 173 | parties hereto, such provision shall be reformed to the minimum extent 174 | necessary to make such provision valid and enforceable. 175 | 176 | If Recipient institutes patent litigation against any entity (including a 177 | cross-claim or counterclaim in a lawsuit) alleging that the Program itself 178 | (excluding combinations of the Program with other software or hardware) 179 | infringes such Recipient's patent(s), then such Recipient's rights granted 180 | under Section 2(b) shall terminate as of the date such litigation is filed. 181 | 182 | All Recipient's rights under this Agreement shall terminate if it fails to 183 | comply with any of the material terms or conditions of this Agreement and 184 | does not cure such failure in a reasonable period of time after becoming 185 | aware of such noncompliance. If all Recipient's rights under this Agreement 186 | terminate, Recipient agrees to cease use and distribution of the Program as 187 | soon as reasonably practicable. However, Recipient's obligations under this 188 | Agreement and any licenses granted by Recipient relating to the Program shall 189 | continue and survive. 190 | 191 | Everyone is permitted to copy and distribute copies of this Agreement, but in 192 | order to avoid inconsistency the Agreement is copyrighted and may only be 193 | modified in the following manner. The Agreement Steward reserves the right to 194 | publish new versions (including revisions) of this Agreement from time to 195 | time. No one other than the Agreement Steward has the right to modify this 196 | Agreement. The Eclipse Foundation is the initial Agreement Steward. The 197 | Eclipse Foundation may assign the responsibility to serve as the Agreement 198 | Steward to a suitable separate entity. Each new version of the Agreement will 199 | be given a distinguishing version number. The Program (including 200 | Contributions) may always be distributed subject to the version of the 201 | Agreement under which it was received. In addition, after a new version of 202 | the Agreement is published, Contributor may elect to distribute the Program 203 | (including its Contributions) under the new version. Except as expressly 204 | stated in Sections 2(a) and 2(b) above, Recipient receives no rights or 205 | licenses to the intellectual property of any Contributor under this 206 | Agreement, whether expressly, by implication, estoppel or otherwise. All 207 | rights in the Program not expressly granted under this Agreement are 208 | reserved. 209 | 210 | This Agreement is governed by the laws of the State of New York and the 211 | intellectual property laws of the United States of America. No party to this 212 | Agreement will bring a legal action under this Agreement more than one year 213 | after the cause of action arose. Each party waives its rights to a jury trial 214 | in any resulting litigation. 215 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # spectacles 2 | 3 | Lenses for Clojure, checked at runtime using spec. It provides `get`, 4 | `get-in`, `assoc`, `assoc-in`, `update` and `update-in`, where keys, 5 | paths and passed values are checked based on existing specs. 6 | 7 | Since Clojure spec is still in alpha, this library should also be 8 | considered to be in alpha -- so, highly experimental, very likely to 9 | change, possibly flawed. 10 | 11 | This library works in both Clojure and ClojureScript. 12 | 13 | ## Leiningen dependency 14 | 15 | Use this with Leiningen: 16 | 17 | `[spectacles "0.3.5"]` 18 | 19 | ## Usage 20 | 21 | Setup the scene with a few specs: 22 | 23 | ```clojure 24 | (ns my-ns 25 | (:require [spectacles.lenses :as lens] 26 | [clojure.spec.alpha :as s])) 27 | 28 | 29 | (s/def ::filename string?) 30 | (s/def ::dims (s/coll-of string?)) 31 | (s/def ::target-dims (s/keys :req-un [::dims] 32 | :opt-un [::the-cat])) 33 | (s/def ::the-cat (s/cat :a string? :b number?)) 34 | (s/def ::targets (s/keys :req-un [::filename ::target-dims])) 35 | ``` 36 | 37 | ...and some test data: 38 | 39 | ```clojure 40 | (def targets {:filename "foo" :target-dims {:dims ["foo" "bar"]}}) 41 | ``` 42 | 43 | You can then do: 44 | 45 | ``` 46 | > (lens/get targets ::targets :filename) 47 | 48 | "foo" 49 | ``` 50 | 51 | The keys are checked against the `::targets` spec: 52 | 53 | ```clojure 54 | > (lens/get targets ::targets :WRONG) 55 | 56 | ExceptionInfo Invalid key :WRONG for spec :my-ns/targets 57 | (valid keys: #{:target-dims :filename}) 58 | ``` 59 | 60 | Same goes for `get-in`: 61 | 62 | ```clojure 63 | > (lens/get-in targets [::targets :target-dims :dims]) 64 | 65 | ["foo" "bar"] 66 | ``` 67 | 68 | Note that the second argument of `get-in` is a vector whose first 69 | element is the spec of the data structure and the rest of the vector 70 | is the path into the key we are looking to retrieve. These two bits of 71 | information are passed as a single vector so that you can define and 72 | pass lenses as parameters by passing a single value. 73 | 74 | `get-in` checks each key of the path against the root spec and also 75 | deeper specs that it encounters: 76 | 77 | ```clojure 78 | > (lens/get-in targets [::targets :target-dims :WRONG]) 79 | 80 | Unhandled clojure.lang.ExceptionInfo 81 | Invalid key :WRONG for spec :my-ns/target-dims 82 | (valid keys: #{:the-cat :dims}) 83 | {:reason :invalid-key, 84 | :collection {:dims ["foo" "bar"]}, 85 | :key :WRONG, 86 | :spec :my-ns/target-dims, 87 | :valid-keys #{:the-cat :dims}, 88 | :path [:target-dims :WRONG], 89 | :root-map {:filename "foo", :target-dims {:dims ["foo" "bar"]}}} 90 | ``` 91 | 92 | ### Changing data 93 | 94 | You can also `assoc`, `assoc-in`, `update-in` values into data 95 | structures, in which case the passed values are checked using spec 96 | before updating the structure: 97 | 98 | ```clojure 99 | > (lens/assoc-in targets [::targets :target-dims :dims] ["zoo" "far"]) 100 | 101 | {:filename "foo", :target-dims {:dims ["zoo" "far"]}} 102 | 103 | > (lens/assoc-in targets [::targets :target-dims :dims] 10) 104 | 105 | Invalid value 10 for key :dims in value {:dims ["foo" "bar"]} 106 | (should conform to: (clojure.spec.alpha/coll-of clojure.core/string?)) 107 | 108 | > (lens/update-in targets [::targets :target-dims :dims] conj 10) 109 | 110 | ExceptionInfo Invalid value ["foo" "bar" 10] for key :dims in value {:dims ["foo" "bar"]} 111 | (should conform to: (clojure.spec.alpha/coll-of clojure.core/string?)) 112 | 113 | ``` 114 | 115 | ### Lenses pointing to `s/cat` 116 | 117 | You can access values of structures spec'ed using `s/cat` by index or by name: 118 | 119 | ```clojure 120 | > (lens/get ["foo" 10] ::the-cat 0) 121 | 122 | "foo" 123 | 124 | > (lens/get ["foo" 10] ::the-cat :a) 125 | 126 | "foo" 127 | 128 | > (def targets2 129 | {:filename "foo" :target-dims {:dims ["foo" "bar"] :the-cat ["foo" 10]}}) 130 | 131 | > (lens/update-in targets2 [::targets :target-dims :the-cat :a] str "bar") 132 | 133 | {:filename "foo", :target-dims {:dims ["foo" "bar"], :the-cat ["foobar" 10]}} 134 | 135 | > (lens/get-in targets2 [::targets :target-dims :the-cat 2]) 136 | 137 | Unhandled clojure.lang.ExceptionInfo 138 | Invalid key 2 for spec :spectacles.impl-test/the-cat (valid keys: 139 | #{0 1 :a :b}) 140 | 141 | ``` 142 | 143 | ### Composing lenses 144 | 145 | Let's add a bit more depth to the spec to explore how to compose 146 | lenses. 147 | 148 | ```clojure 149 | (s/def ::deeper3 string?) 150 | (s/def ::deeper2 (s/keys :opt-un [::deeper3])) 151 | (s/def ::deeper1 (s/keys :opt-un [::deeper2])) 152 | 153 | ;;redefine spec for ::target-dims 154 | (s/def ::target-dims (s/keys :req-un [::dims] 155 | :opt-un [::the-cat ::simple-map ::deeper1])) 156 | ``` 157 | 158 | To compose two lenses you can do: 159 | 160 | ```clojure 161 | > (lens/comp [::targets :target-dims :deeper1] [::deeper1 :deeper2 :deeper3]) 162 | 163 | [:my-fn/targets :target-dims :deeper1 :deeper2 :deeper3] 164 | ``` 165 | 166 | When composing, there is a check on whether the spec corresponding to 167 | the last key of the first lens matches to the target spec of the 168 | second lens. For instance, the first lens here points to the 169 | `:deeper1` key (which corresponds to the `:my-fn/deeper1` spec), but 170 | the second lens targets the `:my-fn/deeper2` spec (as you can see in 171 | the first element of the vector): 172 | 173 | ```clojure 174 | > (lens/comp [::targets :target-dims :deeper1] [::deeper2 :deeper3]) 175 | 176 | ExceptionInfo Cannot compose: last spec of lens1 (:my-fn/deeper1) 177 | does not match first spec of lens2 (:my-fn/deeper2) 178 | ``` 179 | 180 | ## ClojureScript differences 181 | 182 | The ClojureScript version of the library behaves slightly differently 183 | to the Clojure version. Specifically, none of the operations will be 184 | able to check the key predicate of `map-of` specs. This is because 185 | this functionality depends on running `eval` on the form of `map-of` 186 | and ClojureScript doesn't have eval. 187 | 188 | ## Developers 189 | 190 | Run Clojure unit tests with: 191 | 192 | ``` 193 | lein test 194 | ``` 195 | 196 | Run ClojureScript unit tests with (default setup uses node): 197 | 198 | ``` 199 | lein doo 200 | ``` 201 | 202 | Run self-hosted ClojureScript unit tests with: 203 | 204 | ``` 205 | lein tach lumo 206 | ``` 207 | 208 | and 209 | 210 | ``` 211 | lein tach planck 212 | ``` 213 | 214 | ## License 215 | 216 | Copyright © 2017-2018 Stathis Sideris 217 | 218 | Distributed under the Eclipse Public License either version 1.0 or (at 219 | your option) any later version. 220 | -------------------------------------------------------------------------------- /deps.edn: -------------------------------------------------------------------------------- 1 | {:deps {org.clojure/clojure {:mvn/version "1.9.0"}}} 2 | -------------------------------------------------------------------------------- /doc/history.md: -------------------------------------------------------------------------------- 1 | # Version history 2 | 3 | ## 0.3.5 4 | 5 | * Clojurescript support 6 | 7 | ## 0.3.4 8 | 9 | * Fix 10 | [issue 4](https://github.com/stathissideris/spectacles/issues/4): 11 | get-in would fail when path included non-keyword keys 12 | 13 | ## 0.3.3 14 | 15 | * Bugfixes to functionality of previous version 16 | 17 | ## 0.3.2 18 | 19 | * Support for spec that refer other specs 20 | 21 | ## 0.3.1 22 | 23 | * Support for specs that contain s/and, s/or. 24 | 25 | ## 0.3.0 26 | 27 | * Initial public release 28 | -------------------------------------------------------------------------------- /project.clj: -------------------------------------------------------------------------------- 1 | (defproject spectacles "0.3.5" 2 | :description "Lenses for Clojure, checked at runtime using spec." 3 | :url "https://github.com/stathissideris/spectacles" 4 | :license {:name "Eclipse Public License" 5 | :url "http://www.eclipse.org/legal/epl-v10.html"} 6 | :dependencies [[org.clojure/clojure "1.9.0"] 7 | [org.clojure/clojurescript "1.10.238"]] 8 | 9 | :plugins [[lein-cljsbuild "1.1.7"] 10 | [lein-doo "0.1.10"] 11 | [lein-tach "1.0.0"]] 12 | 13 | :doo {:build "test" 14 | :alias {:default [:node]}} 15 | 16 | :tach {:test-runner-ns spectacles.cljs-self-test-runner} 17 | 18 | :cljsbuild {:builds [{:id "test" 19 | :source-paths ["src" "test"] 20 | :compiler {:output-to "out/testable.js" 21 | :main spectacles.cljs-test-runner 22 | :target :nodejs 23 | :optimizations :none}}]}) 24 | -------------------------------------------------------------------------------- /src/spectacles/impl.cljc: -------------------------------------------------------------------------------- 1 | (ns spectacles.impl 2 | (:require [clojure.spec.alpha :as s] 3 | #?(:cljs [goog.string.format :as gsf]))) 4 | 5 | #?(:cljs (def format goog.string.format)) 6 | 7 | (defn- spec-form-type [x] 8 | (cond (seq? x) (first x) 9 | (and (keyword? x) (namespace x)) :spec-ref 10 | :else :default)) 11 | 12 | (defmulti valid-keys spec-form-type) 13 | (defmethod valid-keys :default [_] #{}) 14 | 15 | (defmethod valid-keys :spec-ref [s] (-> s s/get-spec s/form valid-keys)) 16 | (defmethod valid-keys :req [[_ k]] k) 17 | (defmethod valid-keys :opt [[_ k]] k) 18 | (defmethod valid-keys :req-un [[_ k]] (->> k (map name) (map keyword))) 19 | (defmethod valid-keys :opt-un [[_ k]] (->> k (map name) (map keyword))) 20 | 21 | #?(:clj 22 | (defmethod valid-keys `s/map-of 23 | [[_ key-pred _]] (eval key-pred)) 24 | 25 | ;;this is not supported in cljs 26 | :cljs 27 | (defmethod valid-keys `s/map-of 28 | [[_ key-pred _]] any?)) 29 | 30 | (defmethod valid-keys `s/keys 31 | [[_ & spec]] 32 | (set (mapcat valid-keys (partition 2 spec)))) 33 | 34 | (defmethod valid-keys 'keys ;; so that it works with spell-spec.alpha/strict-keys 35 | [[_ & spec]] 36 | (set (mapcat valid-keys (partition 2 spec)))) 37 | 38 | (defmethod valid-keys `s/and 39 | [[_ & spec]] 40 | (set (mapcat valid-keys spec))) 41 | 42 | (defmethod valid-keys `s/or 43 | [[_ & spec]] 44 | (set (mapcat (comp valid-keys second) (partition 2 spec)))) 45 | 46 | (defmethod valid-keys `s/cat 47 | [[_ & spec]] 48 | (set (concat (map first (partition 2 spec)) 49 | (range (/ (count spec) 2))))) 50 | 51 | (defmulti get-value* (fn [_ spec _] (spec-form-type (s/form spec)))) 52 | (defmethod get-value* `s/keys [m _ k] (get m k)) 53 | (defmethod get-value* 'keys [m _ k] (get m k)) ;; so that it works with spell-spec.alpha/strict-keys 54 | (defmethod get-value* `s/map-of [m _ k] (get m k)) 55 | (defmethod get-value* `s/and [m _ k] (if (integer? k) (nth m k) (get m k))) 56 | (defmethod get-value* `s/or [m _ k] (if (integer? k) (nth m k) (get m k))) 57 | (defmethod get-value* `s/cat [m spec k] 58 | (if (integer? k) 59 | (nth m k) 60 | (let [c (s/conform spec m)] 61 | (get c k)))) 62 | 63 | (defn get-value [m spec-name k] 64 | (if (= ::na spec-name) 65 | (get m k) 66 | (let [spec (s/get-spec spec-name) 67 | form (s/form spec) 68 | valid-ks (valid-keys form)] 69 | (if-not (valid-ks k) 70 | (throw (ex-info (format "Invalid key %s for spec %s (valid keys: %s)" 71 | (pr-str k) (str spec-name) (pr-str valid-ks)) 72 | {:reason :invalid-key 73 | :collection m 74 | :key k 75 | :spec spec-name 76 | :valid-keys valid-ks})) 77 | (get-value* m spec k))))) 78 | 79 | (defmulti keys->spec-names 80 | "Takes a spec form and returns a map of unqualified keys to (fully 81 | qualified) spec names." 82 | spec-form-type) 83 | (defmethod keys->spec-names :default [_] #{}) 84 | (defmethod keys->spec-names :spec-ref [s] (-> s s/get-spec s/form keys->spec-names)) 85 | (defmethod keys->spec-names `s/cat [_] ::na) 86 | (defmethod keys->spec-names `s/map-of [_] ::na) 87 | (defmethod keys->spec-names `s/keys 88 | [[_ & spec]] 89 | (as-> spec $ 90 | (partition 2 $) 91 | (mapcat second $) 92 | (zipmap (map (comp keyword name) $) $))) 93 | (defmethod keys->spec-names 'keys ;; so that it works with spell-spec.alpha/strict-keys 94 | [[_ & spec]] 95 | (as-> spec $ 96 | (partition 2 $) 97 | (mapcat second $) 98 | (zipmap (map (comp keyword name) $) $))) 99 | 100 | (defmethod keys->spec-names `s/and 101 | [[_ & specs]] 102 | (apply merge (map keys->spec-names specs))) 103 | 104 | (defmethod keys->spec-names `s/or 105 | [spec] 106 | (apply merge (map keys->spec-names (drop 1 (take-nth 2 spec))))) 107 | 108 | (defn- key->spec [parent-spec spec-name] 109 | (cond (= parent-spec ::na) 110 | ::na 111 | 112 | (and (keyword? spec-name) (namespace spec-name)) 113 | spec-name 114 | 115 | :else 116 | (let [m (-> parent-spec s/get-spec s/form keys->spec-names)] 117 | (if (= ::na m) m 118 | (get m spec-name))))) 119 | 120 | (defn get-value-in [m [spec-name & path]] 121 | (second 122 | (reduce 123 | (fn [[spec-name mm] k] 124 | [(key->spec spec-name k) 125 | (try 126 | (get-value mm spec-name k) 127 | (catch #?(:clj clojure.lang.ExceptionInfo 128 | :cljs cljs.core.ExceptionInfo) e 129 | (throw (ex-info (.getMessage e) 130 | (assoc (ex-data e) 131 | :path (vec path) 132 | :root-map m)))))]) 133 | [spec-name m] path))) 134 | 135 | (defmulti assoc-value* (fn [_ spec _ _] (spec-form-type (s/form spec)))) 136 | (defmethod assoc-value* :spec-ref [m spec k v] (assoc-value* m (-> spec s/get-spec s/form) k v)) 137 | (defmethod assoc-value* `s/keys [m _ k v] (assoc m k v)) 138 | (defmethod assoc-value* 'keys [m _ k v] (assoc m k v)) ;; so that it works with spell-spec.alpha/strict-keys 139 | (defmethod assoc-value* `s/and [m _ k v] (if (integer? k) (assoc (vec m) k v) (assoc m k v))) 140 | (defmethod assoc-value* `s/or [m _ k v] (if (integer? k) (assoc (vec m) k v) (assoc m k v))) 141 | (defmethod assoc-value* `s/cat [m spec k v] 142 | (if (integer? k) 143 | (assoc (vec m) k v) 144 | (let [c (s/conform spec m)] 145 | (s/unform spec (assoc c k v))))) 146 | 147 | (defn assoc-value [m spec-name k v] 148 | (let [key-spec (s/get-spec spec-name) 149 | form (s/form key-spec) 150 | valid-ks (valid-keys form)] 151 | (if-not (valid-ks k) 152 | (throw (ex-info (format "Invalid key %s for spec %s (valid keys: %s)" 153 | (str k) (str spec-name) (pr-str valid-ks)) 154 | {:reason :invalid-key 155 | :collection m 156 | :key k 157 | :value v 158 | :spec spec-name 159 | :valid-keys valid-ks})) 160 | (let [value-spec (key->spec spec-name k)] 161 | (if (and (not= ::na value-spec) (not (s/valid? value-spec v))) 162 | (throw (ex-info (format "Invalid value %s for key %s in value %s (should conform to: %s)" 163 | (pr-str v) (str k) (pr-str m) (pr-str (s/form value-spec))) 164 | {:reason :invalid-value 165 | :collection m 166 | :key k 167 | :value v 168 | :spec spec-name})) 169 | (assoc-value* m key-spec k v)))))) 170 | 171 | (defn update-value [m spec-name k fun & more] 172 | (let [v (get-value m spec-name k)] 173 | (assoc-value m spec-name k (apply fun v more)))) 174 | 175 | (defn assoc-value-in [m [spec-name & path] v] 176 | (let [[k & ks] path] 177 | (if ks 178 | (let [child-spec (key->spec spec-name k)] 179 | (assoc-value m spec-name k 180 | (assoc-value-in (get-value m spec-name k) (into [child-spec] ks) v))) 181 | (assoc-value m spec-name k v)))) 182 | 183 | (defn update-value-in [m lens fun & more] 184 | (let [v (get-value-in m lens)] 185 | (assoc-value-in m lens (apply fun v more)))) 186 | 187 | (defn compose [lens1 lens2] 188 | (let [last-spec (reduce key->spec (first lens1) (rest lens1))] 189 | (if-not (= last-spec (first lens2)) 190 | (throw (ex-info (format "Cannot compose: last spec of lens1 (%s) does not match first spec of lens2 (%s)" 191 | (pr-str last-spec) (pr-str (first lens2))) 192 | {:lens1 lens1 193 | :lens2 lens2})) 194 | (vec (concat lens1 (rest lens2)))))) 195 | -------------------------------------------------------------------------------- /src/spectacles/lenses.cljc: -------------------------------------------------------------------------------- 1 | (ns spectacles.lenses 2 | (:refer-clojure :exclude [get get-in assoc assoc-in update update-in comp]) 3 | (:require [spectacles.impl :as impl])) 4 | 5 | (def get impl/get-value) 6 | (def get-in impl/get-value-in) 7 | 8 | (def assoc impl/assoc-value) 9 | (def assoc-in impl/assoc-value-in) 10 | 11 | (def update impl/update-value) 12 | (def update-in impl/update-value-in) 13 | 14 | (def comp impl/compose) 15 | -------------------------------------------------------------------------------- /test/spectacles/cljs_self_test_runner.cljs: -------------------------------------------------------------------------------- 1 | (ns spectacles.cljs-self-test-runner 2 | (:require [clojure.test :refer [run-tests]] 3 | [spectacles.impl-test])) 4 | 5 | (run-tests 'spectacles.impl-test) 6 | -------------------------------------------------------------------------------- /test/spectacles/cljs_test_runner.cljs: -------------------------------------------------------------------------------- 1 | (ns spectacles.cljs-test-runner 2 | (:require [doo.runner :refer-macros [doo-tests]] 3 | [spectacles.impl-test])) 4 | 5 | (doo-tests 'spectacles.impl-test) 6 | -------------------------------------------------------------------------------- /test/spectacles/impl_test.cljc: -------------------------------------------------------------------------------- 1 | (ns spectacles.impl-test 2 | (:require [spectacles.impl :as sut :refer [valid-keys get-value get-value-in 3 | assoc-value update-value 4 | assoc-value-in update-value-in 5 | compose]] 6 | [clojure.test :refer [deftest testing is run-tests]] 7 | [clojure.spec.alpha :as s])) 8 | 9 | (deftest valid-keys-test 10 | (is (= #{:echo-server :api-secret :call-receipients :msg-receipients :api-key} 11 | (valid-keys `(s/keys :req-un [::api-key ::api-secret ::echo-server ::call-receipients ::msg-receipients])))) 12 | (is (= #{} (valid-keys `(fn [x] (string? x))))) 13 | (is (= #{} (valid-keys `string?))) 14 | (is (= #{:echo-server :api-secret :call-receipients :msg-receipients :api-key} 15 | (valid-keys `(s/and (s/keys :req-un [::api-key ::api-secret ::echo-server ::call-receipients ::msg-receipients]) string?)))) 16 | (is (= #{0 1 2 :config :path :text} 17 | (valid-keys `(s/cat :config ::file-storage :path string? :text string?))))) 18 | 19 | (s/def ::filename string?) 20 | (s/def ::dims (s/coll-of string?)) 21 | (s/def ::simple-map (s/map-of string? number?)) 22 | (s/def ::deeper3 string?) 23 | (s/def ::deeper2 (s/and (s/keys :opt-un [::deeper3 ::deeper2]) 24 | identity)) 25 | (s/def ::deeper1 (s/or :2 (s/keys :opt-un [::deeper2]) 26 | :3 (s/keys :opt-un [::deeper3]))) 27 | (s/def ::target-dims (s/keys :req-un [::dims] 28 | :opt-un [::the-cat ::simple-map ::deeper1])) 29 | (s/def ::the-cat (s/cat :a string? :b number?)) 30 | (s/def ::targets (s/keys :req-un [::filename ::target-dims])) 31 | 32 | (def targets {:filename "foo" :target-dims {:dims ["foo" "bar"]}}) 33 | 34 | (s/def ::other-keys (s/keys :req-un [::the-cat ::deeper3 ::filename])) 35 | (s/def ::other (s/and ::other-keys (fn [x] (= 3 (count x))))) 36 | (s/def ::other2 ::other) 37 | 38 | (def other-value {:deeper3 "foo" 39 | :filename "bar" 40 | :the-cat ["foo" 4]}) 41 | 42 | (deftest get-valid-keys-test 43 | (testing "for specs that refer to other specs" 44 | (is (= #{:the-cat :filename :deeper3} 45 | (@#'sut/valid-keys (-> ::other s/get-spec s/form)))) 46 | (is (= #{:the-cat :filename :deeper3} 47 | (@#'sut/valid-keys (-> ::other2 s/get-spec s/form)))))) 48 | 49 | (deftest get-value-test 50 | (is (s/valid? ::targets targets)) 51 | 52 | (testing "for s/keys" 53 | (is (= "foo" (get-value targets ::targets :filename))) 54 | (is (thrown? #?(:clj Exception :cljs js/Object) (get-value targets ::targets :filo)))) 55 | 56 | (testing "for s/cat" 57 | (testing "using names" 58 | (is (= "foo" (get-value ["foo" 10] ::the-cat :a))) 59 | (is (= 10 (get-value ["foo" 10] ::the-cat :b))) 60 | (is (thrown? #?(:clj Exception :cljs js/Object) (get-value ["foo" 10] ::the-cat :c)))) 61 | (testing "using indexes" 62 | (is (= "foo" (get-value ["foo" 10] ::the-cat 0))) 63 | (is (= 10 (get-value ["foo" 10] ::the-cat 1))) 64 | (is (thrown? #?(:clj Exception :cljs js/Object) (get-value ["foo" 10] ::the-cat 3))))) 65 | 66 | (testing "for specs that refer to other specs" 67 | (is (= "foo" (get-value other-value ::other :deeper3))) 68 | (is (= "bar" (get-value other-value ::other :filename))) 69 | (is (= ["foo" 4] (get-value other-value ::other :the-cat))) 70 | 71 | (is (= "foo" (get-value other-value ::other2 :deeper3))) 72 | (is (= "bar" (get-value other-value ::other2 :filename))) 73 | (is (= ["foo" 4] (get-value other-value ::other2 :the-cat))))) 74 | 75 | (s/def ::top2 (s/keys :req-un [::parent ::children])) 76 | (s/def ::parent string?) 77 | (s/def ::children (s/map-of keyword? ::child)) 78 | (s/def ::child (s/keys :req-un [::age])) 79 | (s/def ::age number?) 80 | 81 | (s/def ::top3 (s/keys :req-un [::parent3 ::children3])) 82 | (s/def ::parent3 string?) 83 | (s/def ::children3 (s/map-of integer? ::child3)) 84 | (s/def ::child3 (s/keys :req-un [::age3])) 85 | (s/def ::age3 number?) 86 | 87 | ;; see issue https://github.com/stathissideris/spectacles/issues/4 88 | (deftest get-in-with-map-of-in-path 89 | (testing "string keys in path" 90 | (let [m {:parent "fred" 91 | :children {:bert {:age 1} 92 | :sam {:age 3} 93 | :fiona {:age 6}}}] 94 | (is (= {:bert {:age 1}, :sam {:age 3}, :fiona {:age 6}} 95 | (get-value-in m [::top2 :children]))) 96 | (is (= {:age 1} (get-value-in m [::top2 :children :bert]))) 97 | (is (= 1 (get-value-in m [::top2 :children :bert :age]))) 98 | 99 | ;;not supported in cljs 100 | #?(:clj (is (thrown? #?(:clj Exception :cljs js/Object) (get-value-in m [::top2 :children "bert" :age])))))) 101 | (testing "integer keys in path" 102 | (let [m {:parent3 "fred" 103 | :children3 {1 {:age3 1} 104 | 2 {:age3 3} 105 | 3 {:age3 6}}}] 106 | (is (= {1 {:age3 1}, 2 {:age3 3}, 3 {:age3 6}} 107 | (get-value-in m [::top3 :children3]))) 108 | (is (= {:age3 3} 109 | (get-value-in m [::top3 :children3 2]))) 110 | (is (= 3 (get-value-in m [::top3 :children3 2 :age3]))) 111 | 112 | ;;not supported in cljs 113 | #?(:clj (is (thrown? #?(:clj Exception :cljs js/Object) (get-value-in m [::top3 :children3 "2" :age3]))))))) 114 | 115 | (def targets2 116 | {:filename "foo" :target-dims {:dims ["foo" "bar"] :the-cat ["foo" 10] :simple-map {"foo" 2 "bar" 3}}}) 117 | 118 | (deftest get-value-in-test 119 | (is (s/valid? ::targets targets2)) 120 | 121 | (testing "for s/keys" 122 | (is (= ["foo" "bar"] (get-value-in targets [::targets :target-dims :dims]))) 123 | (is (thrown? #?(:clj Exception :cljs js/Object) (get-value-in targets [::targets :WRONG]))) 124 | (is (thrown? #?(:clj Exception :cljs js/Object) (get-value-in targets [::targets :target-dims :WRONG]))) 125 | (testing "with missing keys and invalid keys deeper" 126 | (is 127 | (thrown? 128 | #?(:clj Exception :cljs js/Object) 129 | (get-value-in 130 | {:filename "foo" :target-dims {:dims ["foo" "bar"] :the-cat ["foo" 10] :simple-map {"foo" 2 "bar" 3}}} 131 | [::targets :target-dims :deeper1 :WRONG])))) 132 | (testing "with missing keys and invalid keys deeper" 133 | (is 134 | (thrown? 135 | #?(:clj Exception :cljs js/Object) 136 | (get-value-in 137 | {:filename "foo" :target-dims {:dims ["foo" "bar"] :the-cat ["foo" 10] :simple-map {"foo" 2 "bar" 3}}} 138 | [::targets :target-dims :deeper1 :deeper2 :WRONG]))))) 139 | 140 | (testing "for s/keys and s/cat" 141 | (is (= "foo" (get-value-in targets2 [::targets :target-dims :the-cat 0]))) 142 | (is (= "foo" (get-value-in targets2 [::targets :target-dims :the-cat :a]))) 143 | (is (= 10 (get-value-in targets2 [::targets :target-dims :the-cat 1]))) 144 | (is (= 10 (get-value-in targets2 [::targets :target-dims :the-cat :b]))) 145 | (is (thrown? #?(:clj Exception :cljs js/Object) (get-value-in targets2 [::targets :target-dims :the-cat 2]))) 146 | (is (thrown? #?(:clj Exception :cljs js/Object) (get-value-in targets2 [::targets :target-dims :the-cat :c])))) 147 | 148 | (testing "for s/map-of" 149 | (is (= 2 (get-value-in targets2 [::targets :target-dims :simple-map "foo"]))) 150 | 151 | ;;not supported in cljs 152 | #?(:clj (is (thrown? #?(:clj Exception :cljs js/Object) (get-value-in targets2 [::targets :target-dims :simple-map 10]))))) 153 | 154 | (testing "for s/and and s/or" 155 | (is (= "pretty deep" 156 | (get-value-in 157 | {:filename "foo" 158 | :target-dims 159 | {:dims ["foo" "bar"] 160 | :deeper1 161 | {:deeper2 162 | {:deeper2 163 | {:deeper3 "pretty deep"}}}}} 164 | [::targets :target-dims :deeper1 :deeper2 :deeper2 :deeper3]))) 165 | (is (= "pretty deep" 166 | (get-value-in 167 | {:filename "foo" 168 | :target-dims 169 | {:dims ["foo" "bar"] 170 | :deeper1 171 | {:deeper3 "pretty deep"}}} 172 | [::targets :target-dims :deeper1 :deeper3])))) 173 | 174 | (testing "for specs that refer to other specs" 175 | (is (= "foo" (get-value-in other-value [::other :the-cat 0]))) 176 | (is (= "foo" (get-value-in other-value [::other2 :the-cat 0]))) 177 | (is (= 4 (get-value-in other-value [::other :the-cat 1]))) 178 | (is (= 4 (get-value-in other-value [::other2 :the-cat 1]))))) 179 | 180 | (deftest assoc-value-test 181 | (is (= ["bar" 10] (assoc-value ["foo" 10] ::the-cat :a "bar"))) 182 | (is (= ["foo" 20] (assoc-value ["foo" 10] ::the-cat :b 20))) 183 | 184 | (testing "for specs that refer to other specs" 185 | (is (= {:deeper3 "baz", :filename "bar", :the-cat ["foo" 4]} 186 | (assoc-value other-value ::other :deeper3 "baz"))) 187 | (is (= {:deeper3 "foo", :filename "boo", :the-cat ["foo" 4]} 188 | (assoc-value other-value ::other :filename "boo"))) 189 | (is (= {:deeper3 "foo", :filename "bar", :the-cat ["bar" 5]} 190 | (assoc-value other-value ::other :the-cat ["bar" 5]))) 191 | 192 | (is (= {:deeper3 "baz", :filename "bar", :the-cat ["foo" 4]} 193 | (assoc-value other-value ::other2 :deeper3 "baz"))) 194 | (is (= {:deeper3 "foo", :filename "boo", :the-cat ["foo" 4]} 195 | (assoc-value other-value ::other2 :filename "boo"))) 196 | (is (= {:deeper3 "foo", :filename "bar", :the-cat ["bar" 5]} 197 | (assoc-value other-value ::other2 :the-cat ["bar" 5]))))) 198 | 199 | (deftest update-value-test 200 | (is (= ["foobar" 10] (update-value ["foo" 10] ::the-cat :a #(str % "bar")))) 201 | (is (= ["foo" 11] (update-value ["foo" 10] ::the-cat :b inc))) 202 | 203 | (testing "for specs that refer to other specs" 204 | (is (= {:deeper3 "foobar", :filename "bar", :the-cat ["foo" 4]} 205 | (update-value other-value ::other :deeper3 #(str % "bar")))) 206 | (is (= {:deeper3 "foo", :filename "barbar", :the-cat ["foo" 4]} 207 | (update-value other-value ::other :filename #(str % "bar")))) 208 | (is (= {:deeper3 "foo", :filename "bar", :the-cat ["bar" 4]} 209 | (update-value other-value ::other :the-cat assoc 0 "bar"))))) 210 | 211 | (def targets3 212 | {:filename "foo" :target-dims {:dims ["foo" "bar"] :the-cat ["foo" 10]}}) 213 | 214 | (deftest assoc-value-in-test 215 | (testing "for s/keys" 216 | (is (= {:filename "foo", :target-dims {:dims ["zoo" "far"]}} 217 | (assoc-value-in targets [::targets :target-dims :dims] ["zoo" "far"]))) 218 | (is (thrown? #?(:clj Exception :cljs js/Object) (assoc-value-in targets [::targets :target-dims :dims] 10))) 219 | (is (thrown? #?(:clj Exception :cljs js/Object) (assoc-value-in targets [::targets :WRONG] 10))) 220 | (is (thrown? #?(:clj Exception :cljs js/Object) (assoc-value-in targets [::targets :target-dims :WRONG] 10))) 221 | (testing "with missing keys and invalid keys deeper" 222 | (is 223 | (thrown? 224 | #?(:clj Exception :cljs js/Object) 225 | (assoc-value-in 226 | {:filename "foo" :target-dims {:dims ["foo" "bar"] :the-cat ["foo" 10] :simple-map {"foo" 2 "bar" 3}}} 227 | [::targets :target-dims :deeper1 :deeper2 :WRONG] 20))))) 228 | 229 | (testing "for s/keys and s/cat" 230 | (is (= {:filename "foo", :target-dims {:dims ["foo" "bar"], :the-cat ["bar" 10]}} 231 | (assoc-value-in targets3 [::targets :target-dims :the-cat 0] "bar"))) 232 | (is (= {:filename "foo", :target-dims {:dims ["foo" "bar"], :the-cat ["bar" 10]}} 233 | (assoc-value-in targets3 [::targets :target-dims :the-cat :a] "bar"))) 234 | (is (= {:filename "foo", :target-dims {:dims ["foo" "bar"], :the-cat ["foo" 20]}} 235 | (assoc-value-in targets3 [::targets :target-dims :the-cat 1] 20))) 236 | (is (= {:filename "foo", :target-dims {:dims ["foo" "bar"], :the-cat ["foo" 20]}} 237 | (assoc-value-in targets3 [::targets :target-dims :the-cat :b] 20))) 238 | (is (thrown? #?(:clj Exception :cljs js/Object) (assoc-value-in targets3 [::targets :target-dims :the-cat 2] 555))) 239 | (is (thrown? #?(:clj Exception :cljs js/Object) (assoc-value-in targets3 [::targets :target-dims :the-cat :c] 555)))) 240 | 241 | (testing "for s/and and s/or" 242 | (is (= {:filename "foo" 243 | :target-dims 244 | {:dims ["foo" "bar"] 245 | :deeper1 246 | {:deeper2 247 | {:deeper2 248 | {:deeper3 "not too much"}}}}} 249 | (assoc-value-in 250 | {:filename "foo" 251 | :target-dims 252 | {:dims ["foo" "bar"] 253 | :deeper1 254 | {:deeper2 255 | {:deeper2 256 | {:deeper3 "pretty deep"}}}}} 257 | [::targets :target-dims :deeper1 :deeper2 :deeper2 :deeper3] 258 | "not too much"))) 259 | (is (= {:filename "foo" 260 | :target-dims 261 | {:dims ["foo" "bar"] 262 | :deeper1 263 | {:deeper3 "not too much"}}} 264 | (assoc-value-in 265 | {:filename "foo" 266 | :target-dims 267 | {:dims ["foo" "bar"] 268 | :deeper1 269 | {:deeper3 "pretty deep"}}} 270 | [::targets :target-dims :deeper1 :deeper3] 271 | "not too much")))) 272 | 273 | (testing "for specs that refer to other specs" 274 | (is (= {:deeper3 "baz", :filename "bar", :the-cat ["foo" 4]} 275 | (assoc-value-in other-value [::other :deeper3] "baz"))) 276 | (is (= {:deeper3 "foo", :filename "boo", :the-cat ["foo" 4]} 277 | (assoc-value-in other-value [::other :filename] "boo"))) 278 | (is (= {:deeper3 "foo", :filename "bar", :the-cat ["bar" 4]} 279 | (assoc-value-in other-value [::other :the-cat 0] "bar"))) 280 | (is (= {:deeper3 "foo", :filename "bar", :the-cat ["foo" 10]} 281 | (assoc-value-in other-value [::other :the-cat 1] 10))) 282 | 283 | (is (= {:deeper3 "baz", :filename "bar", :the-cat ["foo" 4]} 284 | (assoc-value-in other-value [::other2 :deeper3] "baz"))) 285 | (is (= {:deeper3 "foo", :filename "boo", :the-cat ["foo" 4]} 286 | (assoc-value-in other-value [::other2 :filename] "boo"))) 287 | (is (= {:deeper3 "foo", :filename "bar", :the-cat ["bar" 4]} 288 | (assoc-value-in other-value [::other2 :the-cat 0] "bar"))) 289 | (is (= {:deeper3 "foo", :filename "bar", :the-cat ["foo" 10]} 290 | (assoc-value-in other-value [::other2 :the-cat 1] 10))))) 291 | 292 | (deftest update-value-in-test 293 | (testing "for s/keys" 294 | (is (= {:filename "foo", :target-dims {:dims ["foo" "bar" "baz"]}} 295 | (update-value-in targets [::targets :target-dims :dims] conj "baz"))) 296 | (is (thrown? #?(:clj Exception :cljs js/Object) (update-value-in targets [::targets :target-dims :dims] conj 10))) 297 | (is (thrown? #?(:clj Exception :cljs js/Object) (update-value-in targets [::targets :WRONG] 10))) 298 | (is (thrown? #?(:clj Exception :cljs js/Object) (update-value-in targets [::targets :target-dims :WRONG] 10)))) 299 | 300 | (testing "for s/keys and s/cat" 301 | (is (= {:filename "foo", :target-dims {:dims ["foo" "bar"], :the-cat ["foobar" 10]}} 302 | (update-value-in targets3 [::targets :target-dims :the-cat 0] str "bar"))) 303 | (is (= {:filename "foo", :target-dims {:dims ["foo" "bar"], :the-cat ["foobar" 10]}} 304 | (update-value-in targets3 [::targets :target-dims :the-cat :a] str "bar"))) 305 | (is (= {:filename "foo", :target-dims {:dims ["foo" "bar"], :the-cat ["foo" 11]}} 306 | (update-value-in targets3 [::targets :target-dims :the-cat 1] inc))) 307 | (is (= {:filename "foo", :target-dims {:dims ["foo" "bar"], :the-cat ["foo" 11]}} 308 | (update-value-in targets3 [::targets :target-dims :the-cat :b] inc))) 309 | (is (thrown? #?(:clj Exception :cljs js/Object) (update-value-in targets3 [::targets :target-dims :the-cat 2] inc))) 310 | (is (thrown? #?(:clj Exception :cljs js/Object) (update-value-in targets3 [::targets :target-dims :the-cat :c] inc)))) 311 | 312 | (testing "for specs that refer to other specs" 313 | (is (= {:deeper3 "foobar", :filename "bar", :the-cat ["foo" 4]} 314 | (update-value-in other-value [::other :deeper3] #(str % "bar")))) 315 | (is (= {:deeper3 "foo", :filename "barbar", :the-cat ["foo" 4]} 316 | (update-value-in other-value [::other :filename] #(str % "bar")))) 317 | (is (= {:deeper3 "foo", :filename "bar", :the-cat ["foobar" 4]} 318 | (update-value-in other-value [::other :the-cat 0] #(str % "bar")))) 319 | (is (= {:deeper3 "foo", :filename "bar", :the-cat ["foo" 5]} 320 | (update-value-in other-value [::other :the-cat 1] inc))) 321 | 322 | (is (= {:deeper3 "foobar", :filename "bar", :the-cat ["foo" 4]} 323 | (update-value-in other-value [::other2 :deeper3] #(str % "bar")))) 324 | (is (= {:deeper3 "foo", :filename "barbar", :the-cat ["foo" 4]} 325 | (update-value-in other-value [::other2 :filename] #(str % "bar")))) 326 | (is (= {:deeper3 "foo", :filename "bar", :the-cat ["foobar" 4]} 327 | (update-value-in other-value [::other2 :the-cat 0] #(str % "bar")))) 328 | (is (= {:deeper3 "foo", :filename "bar", :the-cat ["foo" 5]} 329 | (update-value-in other-value [::other2 :the-cat 1] inc))))) 330 | 331 | (deftest compose-test 332 | (is (= [:spectacles.impl-test/targets :target-dims :deeper1 :deeper2 :deeper3] 333 | (compose [::targets :target-dims :deeper1] [::deeper1 :deeper2 :deeper3]))) 334 | (is (= [:spectacles.impl-test/targets :target-dims :deeper1 :deeper2 :deeper3] 335 | (compose [::targets :target-dims] [::target-dims :deeper1 :deeper2 :deeper3]))) 336 | (is (thrown? #?(:clj Exception :cljs js/Object) (compose [::targets :target-dims :deeper1] [::deeper2 :deeper3])))) 337 | --------------------------------------------------------------------------------