macro unifying request-query-value, request-variable-value, and websession-variable + ARGS alist
Andrew Philpot <[email protected]> Thu, 11 Dec 2003 11:51:38 -0800
| Newsgroups | gmane.lisp.open-source.franz |
|---|---|
| Message-ID | <[email protected]> |
This is a multipart MIME message.
--==_Exmh_6360535950
Content-Type: text/plain; charset=us-ascii
The directive <clp_value ... query|session|request/> allows CLP pages
to be written using a single variable access mechanism; variables can
be back-ended to the current session, query or request.
A similar capability can be used in other places, particularly in
implementation of clp_functions, to make the code arguably shorter and
cleaner. Furthermore, in clp functions you might want to access the
"args" parameter in the same manner, although mutating the "args"
parameter seems of more questionable utility.
It seems to me that
(def-clp-function myapp_handle (req ent args body)
(declare (ignore ent body))
(with-vars (req args)
(handle (var "myvar1" :session)
(var "myvar2" :args)
(var "myvar3" :request)
(var "myvar4" :query))))
is easier to follow and more maintainable than the equivalent
(def-clp-function myapp_handle (req ent args body)
(declare (ignore ent body))
(handle (websession-variable (websession-from-req req) "myvar1")
(cdr (assoc "myvar2" args :test #'equal))
(request-variable-value req "myvar3")
(request-query-value req "myvar4")))
Attached are an accessor, mutator, and a wrapper macro to do just
this. Please note that the wrapper macro uses LABELS to define the
local functions. If you don't use both the setter and the mutator in
the same macro invocation you will get a warning that I don't know how
to avoid. This has been previously reported to Franz [tem spr28060].
Enjoy.
Andrew
--==_Exmh_6360535950
Content-Type: text/plain ; name="webactionsutil.lisp"; charset=us-ascii
Content-Description: webactionsutil.lisp
Content-Disposition: attachment; filename="webactionsutil.lisp"
;;; -*- Mode:Lisp; Syntax:Common-lisp; Package:NET.ASERVE; Base:10 -*-
(in-package :user)
(provide :webactionsutil)
(eval-when (compile load eval)
(require :allegroserve))
(in-package :net.aserve)
(eval-when (compile load eval)
(export '(WITH-VARS VAR WEBACTIONS-VAR) :net.aserve))
(defun webactions-var (req args name &optional (where :session))
(ecase where
(:args (cdr (assoc name args :test #'equal)))
(:query (request-query-value name req))
(:request (request-variable-value req name))
(:session (websession-variable
(websession-from-req req) name))))
(defun (setf webactions-var) (new-value req args name &optional (where :session))
(ecase where
(:args (let ((pair (assoc name args :test #'equal)))
(if pair
(setf (cdr pair) new-value)
(push (cons name new-value) args))))
(:query (setf (request-query-value name req) new-value))
(:request (setf (request-variable-value req name) new-value))
(:session (setf (websession-variable
(websession-from-req req) name) new-value))))
(defmacro with-vars ((req &optional args) &body body)
`(labels ((var (name &optional (where :session))
(webactions-var ,req ,args name where))
((setf var) (new-value name &optional (where :session))
(setf (webactions-var ,req ,args name where) new-value)))
,@body))
--==_Exmh_6360535950--