diff options
author | Boutade <thegoofist@protonmail.com> | 2019-09-27 11:38:32 -0500 |
---|---|---|
committer | Boutade <thegoofist@protonmail.com> | 2019-09-27 11:38:32 -0500 |
commit | cee07c415eafd0a1111ddb02c15d3a8fc357383e (patch) | |
tree | d44eff8a2ce026740622fb29ff89cef5420a1acb | |
parent | 91c3334389235a1f6992e76e54dac3ba7144f517 (diff) |
created macros.lisp, bugfix
-rw-r--r-- | granolin.asd | 1 | ||||
-rw-r--r-- | granolin.lisp | 32 | ||||
-rw-r--r-- | macros.lisp | 48 | ||||
-rw-r--r-- | package.lisp | 9 | ||||
-rw-r--r-- | utility-apps.lisp | 4 |
5 files changed, 58 insertions, 36 deletions
diff --git a/granolin.asd b/granolin.asd index 5e24104..1e3c8fd 100644 --- a/granolin.asd +++ b/granolin.asd @@ -8,5 +8,6 @@ :serial t :depends-on (#:drakma #:jonathan) :components ((:file "package") + (:file "macros") (:file "granolin") (:file "utility-apps"))) diff --git a/granolin.lisp b/granolin.lisp index 3445bff..be252c4 100644 --- a/granolin.lisp +++ b/granolin.lisp @@ -125,33 +125,6 @@ ;;; Utilities for working with parsed JSON data -(defmacro getob (ob key &rest keys) - "OB should be a nested PLIST, KEYS are lists of keys into that PLIST. Gets the - result of nested GETF calls into the list. This form is SETF-able." - (let ((form `(getf ,ob ,key))) - (dolist (k keys) - (setf form `(getf ,form ,k))) - form)) - -(defmacro def-json-wrap (name &rest field-specs) - "Defines a struct named the value of NAME, a symbol, with a single slot called - DATA. DATA holds a PLIST as returned by JONATHAN:PARSE. - - Each FIELD-SPEC is a list of the form (METHOD-NAME KEY1 ... KEYN) - - For each FIELD-SPEC, a method called METHOD-NAME will be defined as a reader - that accesses a value, the path to which is formed of the KEY values. - - E.g. If a JSON value `ob` has a descendent at `ob.x.y.z` then the FIELD-SPEC - could be (get-z :|x| :|y| :|z|) - " - `(progn - (defstruct ,name data) - ,@(loop :for (method . keys) :in field-specs :collect - `(defmethod ,method ((ob ,name)) - (with-slots (data) ob - (getob data ,@keys)))))) - (def-json-wrap login-response (user-id :|user_id|) (access-token :|access_token|)) @@ -172,11 +145,6 @@ (msg-type :|content| :|msgtype|) (msg-body :|content| :|body|)) -(defmacro def-timeline-event-pred (name etype mtype) - `(defun ,name (event) - (and (equal ,etype (getob event :|type|)) - (equal ,mtype (getob event :|content| :|msgtype|))))) - (defstruct (text-message-event (:include timeline-event))) (def-timeline-event-pred text-message-event-p* "m.room.message" "m.text") diff --git a/macros.lisp b/macros.lisp new file mode 100644 index 0000000..4466105 --- /dev/null +++ b/macros.lisp @@ -0,0 +1,48 @@ +(in-package :granolin) + +(defmacro let-cond ((var) &body forms) + `(let (,var) + (cond + ,@(loop :for form :in forms + :collect (destructuring-bind (test . actions) form + `((setf ,var ,test) ,@actions)))))) +(defmacro let-when ((var test) &body body) + `(let ((,var ,test)) + (when ,test ,@body))) + +(defmacro let-if ((var test) then &optional else) + `(let ((,var ,test)) + (if ,test ,then ,else))) + +(defmacro getob (ob key &rest keys) + "OB should be a nested PLIST, KEYS are lists of keys into that PLIST. Gets the + result of nested GETF calls into the list. This form is SETF-able." + (let ((form `(getf ,ob ,key))) + (dolist (k keys) + (setf form `(getf ,form ,k))) + form)) + + +(defmacro def-json-wrap (name &rest field-specs) + "Defines a struct named the value of NAME, a symbol, with a single slot called + DATA. DATA holds a PLIST as returned by JONATHAN:PARSE. + + Each FIELD-SPEC is a list of the form (METHOD-NAME KEY1 ... KEYN) + + For each FIELD-SPEC, a method called METHOD-NAME will be defined as a reader + that accesses a value, the path to which is formed of the KEY values. + + E.g. If a JSON value `ob` has a descendent at `ob.x.y.z` then the FIELD-SPEC + could be (get-z :|x| :|y| :|z|) + " + `(progn + (defstruct ,name data) + ,@(loop :for (method . keys) :in field-specs :collect + `(defmethod ,method ((ob ,name)) + (with-slots (data) ob + (getob data ,@keys)))))) + +(defmacro def-timeline-event-pred (name etype mtype) + `(defun ,name (event) + (and (equal ,etype (getob event :|type|)) + (equal ,mtype (getob event :|content| :|msgtype|))))) diff --git a/package.lisp b/package.lisp index ad251e3..1a8e50e 100644 --- a/package.lisp +++ b/package.lisp @@ -4,6 +4,13 @@ (:use #:cl) (:export + ;; macros + #:let-cond + #:let-if + #:let-when + #:getob + #:def-json-wrap + ;; main class #:client #:homeserver @@ -23,11 +30,9 @@ #:auto-joiner ;; json data utilities & accessors - #:def-json-wrap #:event-content #:event-id #:event-type - #:getob #:msg-body #:msg-type #:prev-content diff --git a/utility-apps.lisp b/utility-apps.lisp index b52baf0..16df32f 100644 --- a/utility-apps.lisp +++ b/utility-apps.lisp @@ -58,7 +58,7 @@ (name :accessor room-name :initarg :name :initform "") (aliases :accessor room-aliases :initarg :aliases :initform nil) (members :accessor room-members :initarg :members :initform nil) - (direct-p :accessor direct-p :initarg :direct-p :initform))) + (direct-p :accessor direct-p :initarg :direct-p :initform nil))) (defclass server-directory () ((directory-table @@ -166,7 +166,7 @@ (find-if (lambda (contact) (or (equal name contact) (and like (search name contact :test #'string-equal)))) - (client-contacts client))) + (client-contacts client)))) ;;; Basic Joiner Bot |