Using macros in Clojure

I'm specifically trying to generate the boilerplate for crud functions to work with the Google App Engine datastore using appengine-magic in Clojure. I'm having difficulty working out how to generate values from a model that I've reproduced below.

(def *model* {:users [{:name "Adam"
                       :email "adam@gmail.com"
                       :registered-on "07-05-2011"}
                      {:name "Greg"
                       :email "gregory@gmail.com"
                       :registered-on "11-05-2011"}]
              :post [{:title "A"
                      :authour "Adam"}
                     {:title "B"
                      :author "Greg"}]})

I'm fairly new to appengine-magic, but it provides a defentity which allows you to define entities that you can put into the datastore and save! which allows you to save predefined entities into the datastore.

These take the form of:

(ds/defentity Post [title author])
(ds/save! (Post. title author))

Now just to start with I've defined:

(defn list-entities [model]
  "Takes a representation of the model and lists the entities in preparation for generating defentities"
  (interleave (vec (map first (partition 1 (map (comp symbol capitalize #(str % ".") name) (keys model)))))
    (map vec (map keys (map first (vals model))))))

Calling it with:

(list-entities *model*)

Outputs:

(Users. [:name :email :registered-on] Post. [:title :author])

Now I am having difficulty defining gen-entities which will take the output above and repeatedly call ds/defentities defining as many entities as my model requires.

(defmacro gen-entities [entity fields]
  `(ds/defentity 'entity 'fields))

Additionally I am in no way certain that this is a reasonable way to go about solving this problem. I'm still very new to macros and probably making several mistakes. Any help/clarity would be appreciated.

NOTE:

That model I've realised is badly designed, the one below is a lot better:

(def *model* {:users [:name :email :registered-on]
              :post [:title :author]})

However it is more complex in terms of writing a macro so I will leave it as is.

6
задан toofarsideways 8 May 2011 в 21:02
поделиться