From aec0893df2f4bb8fa8da7c71cb8dca09c0bd5f86 Mon Sep 17 00:00:00 2001 From: Colin Okay Date: Wed, 9 Feb 2022 16:09:48 -0600 Subject: defined map-parameters --- lazybones.lisp | 17 +++++++++++++++++ package.lisp | 1 + 2 files changed, 18 insertions(+) diff --git a/lazybones.lisp b/lazybones.lisp index 2b28be7..5ccbf6f 100644 --- a/lazybones.lisp +++ b/lazybones.lisp @@ -379,6 +379,23 @@ of each NAME, or NIL if there is no such parameter." collect `(,name (cdr (assoc ,string-name ,params :test #'string-equal)))) ,@body)))) +(defmacro map-parameters ((&rest params) &body body) + "PARAMS is a list of pairs (NAME PARSER). MAP-PARAMETERS behaves +exactly like LET-PARAMETERS except that the values boudn to NAMEs are +first mapped with the PARSER function." + (assert (loop for (name parser) in params + always (and (symbolp name) + (or (symbolp parser) (functionp parser)))) + () + "Malformed PARAMS in MAP-PARAMETERS macro") + + (let ((names (mapcar #'car params))) + `(let-parameters ,names + (let ,(loop for name in names + collect `(,name (when ,name (funcall ',(second (assoc name params)) ,name)))) + ,@body)))) + + (defun http-ok (content) "Content should be a string, a byte-vector, or a pathname to a local file. CONTENT-TYPE should be a MIME type string." diff --git a/package.lisp b/package.lisp index 64b0649..1e7965e 100644 --- a/package.lisp +++ b/package.lisp @@ -52,6 +52,7 @@ #:defendpoint* #:endpoint #:let-parameters + #:map-parameters #:http-err #:http-ok #:http-respond -- cgit v1.2.3