Changeset 3720

Show
Ignore:
Timestamp:
08/01/08 14:50:18 (4 months ago)
Author:
ksprotte
Message:

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

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/bknr/web/src/web/web-macros.lisp

    r3719 r3720  
    2020     ,@body)) 
    2121 
    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 
     24has the form (VARIABLE &OPTIONAL DEFAULT-VALUE TYPE) or can be a 
     25single VARIABLE. 
     26 
     27If the TYPE is specified, the value is converted like in 
     28HUNCHENTOOT:DEFINE-EASY-HANDLER when PARAMETER-TYPE is given. 
     29 
     30With respect to the conversion of an empty string, there is a subtle 
     31difference between the TYPE specified as STRING and the TYPE left 
     32unspecified. In the former case, the converted value will still be an 
     33empty 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))) 
    3546 
    3647(defmacro form-case (&rest cases) 
  • trunk/bknr/web/src/web/web-utils.lisp

    r3719 r3720  
    7171          (when post (post-parameters*)))) 
    7272 
    73 (defun query-param (param-name &key (get t) (post t)
     73(defun query-param (param-name &key (get t) (post t) type
    7474  (let ((value (cdr (assoc param-name (query-params :get get :post post) :test #'string-equal)))) 
    75     (unless (equal value "") 
    76       value))) 
     75    (if type 
     76        (hunchentoot::convert-parameter value type) 
     77        (unless (equal value "") 
     78          value)))) 
    7779 
    7880(defun query-param-list (param-name &key (get t) (post t))