| 22 | | (defmacro with-query-params ((&rest params) &body body) |
|---|
| 23 | | (let ((vars (loop for param in params |
|---|
| 24 | | when (and (symbolp param) |
|---|
| 25 | | (not (null param))) |
|---|
| 26 | | collect (list param `(query-param ,(string-downcase (symbol-name param)))) |
|---|
| 27 | | when (consp param) |
|---|
| 28 | | collect (list (car param) |
|---|
| 29 | | `(or (parameter ,(string-downcase (symbol-name (car param)))) |
|---|
| 30 | | ,(second param)))))) |
|---|
| 31 | | (if vars |
|---|
| 32 | | `(let ,vars |
|---|
| 33 | | ,@body) |
|---|
| 34 | | (first body)))) |
|---|
| | 22 | (defmacro with-query-params ((&rest parameters) &body body) |
|---|
| | 23 | "PARAMETERS is a list of parameter-specifiers. A parameter-specifier |
|---|
| | 24 | has the form (VARIABLE &OPTIONAL DEFAULT-VALUE TYPE) or can be a |
|---|
| | 25 | single VARIABLE. |
|---|
| | 26 | |
|---|
| | 27 | If the TYPE is specified, the value is converted like in |
|---|
| | 28 | HUNCHENTOOT:DEFINE-EASY-HANDLER when PARAMETER-TYPE is given. |
|---|
| | 29 | |
|---|
| | 30 | With respect to the conversion of an empty string, there is a subtle |
|---|
| | 31 | difference between the TYPE specified as STRING and the TYPE left |
|---|
| | 32 | unspecified. In the former case, the converted value will still be an |
|---|
| | 33 | empty string, while in the latter VARIABLE will be bound to NIL." |
|---|
| | 34 | (flet ((parameter-binding (parameter-specifier) |
|---|
| | 35 | (destructuring-bind (variable &optional default-value type) |
|---|
| | 36 | (ensure-list parameter-specifier) |
|---|
| | 37 | (let ((query-param-form (if type |
|---|
| | 38 | `(query-param ,(string-downcase variable) :type ',type) |
|---|
| | 39 | `(query-param ,(string-downcase variable))))) |
|---|
| | 40 | `(,variable |
|---|
| | 41 | ,(if default-value |
|---|
| | 42 | `(or ,query-param-form ,default-value) |
|---|
| | 43 | query-param-form)))))) |
|---|
| | 44 | `(let ,(mapcar #'parameter-binding parameters) |
|---|
| | 45 | ,@body))) |
|---|