I took a swing at this using prismatic schema:
(ns schema-test.core
(:require [schema.core :as s]
[schema.utils :as utils]
[schema.coerce :as coerce])
(:import schema.core.NamedSchema))
(def Alternate (s/named [s/Keyword] "alternate"))
(def Path (s/named [(s/either s/Keyword Alternate)] "path"))
(def T [(s/either s/Keyword Path)])
(def example [ :a [:a :b :c] [:a [:b1 :b2] :c ] ] )
(defn annotater [schema]
(s/start-walker
(fn [s]
(let [walk (s/walker s)]
(fn [x]
(if (and (instance? clojure.lang.IObj x) (instance?
schema.core.NamedSchema s))
(with-meta (walk x) {:schema (.name s)})
(walk x)))))
schema))
(defn annotate [schema data] ((annotater schema) data))
(def r (annotate T example))
(map meta r) ; => (nil {:schema "path"} {:schema "path"})
(meta (nth (nth r 2) 1)) ; => {:schema "alternate"}
On Thu, Jun 25, 2015 at 12:35 PM, Brian Marick <[email protected]> wrote:
> Suppose we have a structure like this:
>
> [ :a [:a :b :c] [:a [:b1 :b2] :c ] ]
>
> That is a *required* list. It consists of keywords and *paths* (nested
> vector like [:a :b :c] above). A path consists of keywords and *alternates*
> (a twice-nested vector like [:b1 :b2]) above.
>
> It's easy to descend this by hand and keep track of which "type" of vector
> you're dealing with. However, this sort of thing comes up enough that it
> would be convenient if there were a function that takes a grammar and a
> structure and returns that structure annotated (metadata?) with type
> information. Coupled with Specter https://github.com/nathanmarz/specter,
> that would make a good number of data transformations easy-peasy.
>
> Is there such a library? Or a library I can build on? A quick scan shows a
> lot of parsers for dealing with strings, but we've already got one of
> those: it's called `read`.
>
> @marick
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to [email protected]
> Note that posts from new members are moderated - please be patient with
> your first post.
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
> --- You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.