Created
September 9, 2016 15:45
-
-
Save Jannis/f23a2ecf350b401745d190b02bbb619c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;;;; Test code | |
| (require '[clojure.spec :as s]) | |
| (s/def ::simple-value | |
| symbol?) | |
| (s/def ::link-value | |
| (s/tuple symbol? any?) | |
| #_(s/tuple symbol? keyword?) | |
| (s/def ::single-value | |
| (s/or :simple ::simple-value | |
| :link ::link-value)) | |
| (s/def ::nested-values | |
| (s/cat :parent ::single-value | |
| :children ::query)) | |
| (s/def ::query | |
| (s/and vector? | |
| (s/+ (s/alt :single ::single-value | |
| :nested ::nested-values)))) | |
| (println) | |
| (println "[a b [c]]") | |
| (println) | |
| (clojure.pprint/pprint (s/conform ::query '[a b [c]])) | |
| (println) | |
| (println "[a b [c d]]") | |
| (println) | |
| (clojure.pprint/pprint (s/conform ::query '[a b [c d]])) | |
| (println) | |
| (println "[a b [c d e]]") | |
| (println) | |
| (clojure.pprint/pprint (s/conform ::query '[a b [c d e]])) | |
| ;;;; Output | |
| [a b [c]] | |
| [[:single [:simple a]] | |
| [:nested {:parent [:simple b], :children [[:single [:simple c]]]}]] | |
| [a b [c d]] | |
| [[:single [:simple a]] | |
| [[:nested | |
| {:parent [:simple b], | |
| :children [[:single [:simple c]] [:single [:simple d]]]}]]] | |
| ;; Note the extra vector surroinding [:nested ...] there? | |
| [a b [c d e]] | |
| [[:single [:simple a]] | |
| [:nested | |
| {:parent [:simple b], | |
| :children | |
| [[:single [:simple c]] | |
| [:single [:simple d]] | |
| [:single [:simple e]]]}]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment