[bknr-cvs] ksprotte changed trunk/bknr/web/src/web/web-

BKNR Commits bknr at bknr.net
Fri Aug 1 12:50:18 UTC 2008


Revision: 3720
Author: ksprotte
URL: http://bknr.net/trac/changeset/3720

BKNR web: QUERY-PARAM and WITH-QUERY-PARAMS now support conversion
from the value string to an optionally specified type.

U   trunk/bknr/web/src/web/web-macros.lisp
U   trunk/bknr/web/src/web/web-utils.lisp

Modified: trunk/bknr/web/src/web/web-macros.lisp
===================================================================
--- trunk/bknr/web/src/web/web-macros.lisp	2008-08-01 12:08:44 UTC (rev 3719)
+++ trunk/bknr/web/src/web/web-macros.lisp	2008-08-01 12:50:18 UTC (rev 3720)
@@ -19,20 +19,31 @@
                  names)
      , at body))
 
-(defmacro with-query-params ((&rest params) &body body)
-  (let ((vars (loop for param in params
-                 when (and (symbolp param)
-                           (not (null param)))
-                 collect (list param `(query-param ,(string-downcase (symbol-name param))))
-                 when (consp param)
-                 collect (list (car param)
-                               `(or (parameter ,(string-downcase (symbol-name (car param))))
-                                    ,(second param))))))
-    (if vars
-        `(let ,vars
-           , at body)
-        (first body))))
+(defmacro with-query-params ((&rest parameters) &body body)
+  "PARAMETERS is a list of parameter-specifiers. A parameter-specifier
+has the form (VARIABLE &OPTIONAL DEFAULT-VALUE TYPE) or can be a
+single VARIABLE.
 
+If the TYPE is specified, the value is converted like in
+HUNCHENTOOT:DEFINE-EASY-HANDLER when PARAMETER-TYPE is given.
+
+With respect to the conversion of an empty string, there is a subtle
+difference between the TYPE specified as STRING and the TYPE left
+unspecified. In the former case, the converted value will still be an
+empty string, while in the latter VARIABLE will be bound to NIL."
+  (flet ((parameter-binding (parameter-specifier)
+           (destructuring-bind (variable &optional default-value type)
+               (ensure-list parameter-specifier)
+             (let ((query-param-form (if type
+                                         `(query-param ,(string-downcase variable) :type ',type)
+                                         `(query-param ,(string-downcase variable)))))
+               `(,variable
+                 ,(if default-value
+                      `(or ,query-param-form ,default-value)
+                      query-param-form))))))
+    `(let ,(mapcar #'parameter-binding parameters)
+       , at body)))
+
 (defmacro form-case (&rest cases)
   `(cond
      ,@(mapcar #'(lambda (c)

Modified: trunk/bknr/web/src/web/web-utils.lisp
===================================================================
--- trunk/bknr/web/src/web/web-utils.lisp	2008-08-01 12:08:44 UTC (rev 3719)
+++ trunk/bknr/web/src/web/web-utils.lisp	2008-08-01 12:50:18 UTC (rev 3720)
@@ -70,10 +70,12 @@
   (append (when get (get-parameters*))
           (when post (post-parameters*))))
 
-(defun query-param (param-name &key (get t) (post t))
+(defun query-param (param-name &key (get t) (post t) type)
   (let ((value (cdr (assoc param-name (query-params :get get :post post) :test #'string-equal))))
-    (unless (equal value "")
-      value)))
+    (if type
+        (hunchentoot::convert-parameter value type)
+        (unless (equal value "")
+          value))))
 
 (defun query-param-list (param-name &key (get t) (post t))
   (assoc-values param-name (query-params :get get :post post)




More information about the Bknr-cvs mailing list