aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Andreas Benkard <code@mail.matthias.benkard.de>2011-03-02 18:45:42 +0100
committerMatthias Andreas Benkard <code@mail.matthias.benkard.de>2011-03-02 18:45:42 +0100
commitd309f7d3d1336d6167fec9f16bf8599e691559d7 (patch)
tree15ada84caa1d77c7157756bf4f74769da0e0db14
parentf24a39aa7937c706981a14695f2e75be824cd79c (diff)
Support compound substitutions (i.e. forms like “a.b.c”).
-rw-r--r--json-template.lisp33
1 files changed, 24 insertions, 9 deletions
diff --git a/json-template.lisp b/json-template.lisp
index 4ea4b9a..2acc811 100644
--- a/json-template.lisp
+++ b/json-template.lisp
@@ -131,18 +131,33 @@
(with-output-to-string (out)
(expand-template-to-stream template (list context) out)))
-(defun getcontext (context key)
- (getf context (intern (string-upcase (string key)) '#:keyword) nil))
+(defun getcontext (context key &aux (result context))
+ (dolist (key-component key result)
+ (setq result
+ (getf result
+ key-component
+ nil))))
+
+(defun listify-key (key &optional (start 0))
+ (let ((dot (position #\. key :start start)))
+ (if dot
+ (cons (intern (string-upcase (subseq key start dot))
+ '#:keyword)
+ (listify-key key (1+ dot)))
+ (list (intern (string-upcase (subseq key start))
+ '#:keyword)))))
(defun lookup-context (contexts key)
+ (when (string= key "@")
+ (return-from lookup-context (first contexts)))
+ (unless (listp key)
+ (setq key (listify-key key)))
(labels ((lookup-in-stack (context-stack)
- (if (endp context-stack)
- nil
- (or (getcontext (first context-stack) key)
- (lookup-in-stack (rest context-stack))))))
- (if (string= key "@")
- (first contexts)
- (lookup-in-stack contexts))))
+ (if (endp context-stack)
+ nil
+ (or (getcontext (first context-stack) key)
+ (lookup-in-stack (rest context-stack))))))
+ (lookup-in-stack contexts)))
(defun expand-template-to-stream (template contexts stream)