Hi Everyone,
Background to my problem:
I am developing a compojure application, and there is lots of
duplication in listing field names in my current data model:
(i) in the defstruct
(ii) in the public constructor's argument list
(iii) in the hiccup form fields
(iv) in the compojure argument destructuring
(v) in the handler's argument list
Ideally I would like to declare my data model in one place like this:
(def person-entity
{:name {:type String :validator name-validator}
:id-number {:type String :validator id-number-validator}
:height {:type Float :default 0.0}
:weight {:type Float :default 0.0}
:bmi {:type Float :internal true}})
And generate everything from there so that it is trivial to add fields
etc. (:internal true just means that this is a calculated field or for
some other reason is not supplied by the user - it is therefore not
needed by the constructor, and will not show up on the edit-person
form).
I have tried to generate the defrecord etc from such a definition and
have battled a bit, so in the interest of making some progress with my
experiment I have tried this instead:
(defn name-validator [val] val)
(defn id-number-validator [val] val)
(defn nil-validator [val] val)
(defrecord Person
[#^String name
#^String id-number
#^Float height
#^Float weight
#^Float bmi])
(def person-traits
{:name {:validator name-validator}
:id-number {:validator id-number-validator}
:height {:default 100.0}
:weight {:default 100.0}
:bmi {:internal true}})
(def person-constructor (make-constructor Person person-traits))
(def person-editor (make-editor Person person-traits))
(def bob (person-constructor {:name "Bob" :id-number "123"}))
;;; I don't really know how bmi is calculated!! Let's pretend it is
weight (kg) / height (cm)
(bob :bmi)
1.0
(bob :id-number)
"123"
And the person-editor is a snippet of hiccup which has all of the
relevant fields (e.g. text fields by default).
I plan to use flutter validators, and not to use compojure
destructuring (I will have to pull the fields out of the request in
the generated form handlers).
I am looking for advice in the following areas:
(i) Is it possible to generate the (defrecord Person ...) from the
person-entity hash-map that I have shown?
(ii) Is this a totally crazy idea?
(iii) Am I neglecting to see a much simpler way of achieving my core
objective (reduce all of the duplication)?
Thanks very much.
Cheers,
David
--
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