marijnh / Postmodern

A Common Lisp PostgreSQL programming interface
http://marijnhaverbeke.nl/postmodern
Other
400 stars 90 forks source link

Introduce define-dao-class macro? #341

Closed kilianmh closed 7 months ago

kilianmh commented 7 months ago

For a project I defined a convenience macro define-dao-class because we basically always want to define the :col-type slot and don't want to forget the metaclass. I was thinking it might be a nice addition.

  1. The second slot is used as value for :col-type if its not a keyword.
  2. Automatically add the slot (:metaclass dao-class)
(eval-when (:compile-toplevel :load-toplevel :execute)
  (defun expand-slot (rest)
    (let ((second
        (second rest)))
      (if (keywordp second)
      `(,(first rest)
        ,@(cdr rest))
      `(,(first rest)
        :col-type ,second
        ,@(cddr rest))))))

(defmacro define-dao-class (name direct-superclasses direct-slots &rest options)
  `(defclass ,name ,direct-superclasses
     (,@(mapcar #'expand-slot direct-slots))
     ,@(append options
    '((:metaclass dao-class)))))

What do you think?

sabracrolleton commented 7 months ago

I will consider it if you think about appropriate tests.

sabracrolleton commented 7 months ago

I probably will not get to it until late next week.