Last active
December 14, 2015 17:59
-
-
Save nicferrier/5125633 to your computer and use it in GitHub Desktop.
map a url to variable names, suggested by #emacs jaimef. like what sinatra does
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defun elnode/bind-path (pattern httpcon) | |
(let* ((path (elnode-http-pathinfo httpcon)) | |
(lst (split-string pattern "/" t)) | |
(patlst | |
(let ((i 0)) | |
(loop for part in lst | |
if (string-match-p "^:.*" part) | |
collect (cons i (intern (substring part 1))) | |
do (setq i (+ 1 i))))) | |
(splt (split-string path "/"))) | |
(let ((i 0)) | |
(loop for part in splt | |
if (aget patlst i) | |
collect (cons (aget patlst i) (list part)) | |
do (setq i (+ i 1)))))) | |
(defmacro elnode-bind-path (pattern httpcon &rest body) | |
(declare (indent 2)) | |
`(let (,@(elnode/bind-path pattern httpcon)) | |
,@body)) | |
;; Demo of the underlying func | |
(flet ((elnode-http-pathinfo (httpcon) "/one/a/b")) | |
(elnode/bind-path "/one/:one/:two" :httpcon)) | |
;; Now the macro | |
(flet ((elnode-http-pathinfo (httpcon) "/one/a/b")) | |
(pp-macroexpand-expression | |
'(elnode-bind-path "/one/:one/:two" :httpcon | |
(+ 1 2)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Works if the omit-nulls parameter is dropped from the first split-string