fukamachi / ningle

Super micro framework for Common Lisp
http://8arrow.org/ningle/
273 stars 25 forks source link

params accumulating data from requirements #44

Closed garlic0x1 closed 11 months ago

garlic0x1 commented 11 months ago

It seems like when i make multiple requests to a route with a requirement, weird things happen to the params input

(defvar *app* (make-instance 'ningle:app))

(setf (ningle:requirement *app* :test)
      #'(lambda (_)
          (declare (ignore _))
          (format t "Hit Requirement :test~%")
          t))

(setf (ningle:route *app* "/test" :test t)
      #'(lambda (params)
          (format t "~A~%" params)
          ""))

(defvar *server* (clack:clackup
                 (lack:builder *app*)
                 :server :hunchentoot :port 5001))

With this code, the first time i request /test the following prints to stdout

((TEST))

The second time the request is made the output is

((TEST) (TEST))

And so on as I keep making requests. Am I supposed to do something to reset the value?

Thanks for any help.

fukamachi commented 11 months ago

Thank you for reporting! At first glance, it seems to be a bug of compile-requirements. I'll look into it and try to fix it.

fukamachi commented 11 months ago

Can you try the fix/multiple-requirements branch if it makes any changes?

garlic0x1 commented 11 months ago

Thank you it works now. By the way, one thing that might be nice is if the truthy value returned from the requirement was in the parameters, so you could do something like this

 (setf (ningle:requirement *app* :test)
      #'(lambda (_)
          (find-dao 'user :username ....)))

 (setf (ningle:route *app* "/test" :test t)
      #'(lambda (params)
          (assoc-value params 'test)
          ""
          ))

Although its not a big deal since i can just modify the context

fukamachi commented 11 months ago

Positive feedback is always welcomed. The second value of the requirement function will be pushed to params:

 (setf (ningle:requirement *app* :test)
      #'(lambda (_)
          ;; Return multiple values
          (values t
                  ;; DAO will be accessible in `params`
                  (find-dao 'user :username ....))))