Skip to content

Instantly share code, notes, and snippets.

@Jannis
Created September 9, 2016 15:45
Show Gist options
  • Select an option

  • Save Jannis/f23a2ecf350b401745d190b02bbb619c to your computer and use it in GitHub Desktop.

Select an option

Save Jannis/f23a2ecf350b401745d190b02bbb619c to your computer and use it in GitHub Desktop.
;;;; 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