diff options
author | Matthias Andreas Benkard <code@mail.matthias.benkard.de> | 2017-05-14 17:38:52 +0200 |
---|---|---|
committer | Matthias Andreas Benkard <code@mail.matthias.benkard.de> | 2017-05-14 17:38:52 +0200 |
commit | 36838f04f1b3967181993530dc5a03c5bcb9908a (patch) | |
tree | e8e7c4a72839ffa85c6c763522c68589ecb535e7 | |
parent | 8e45362a510236f46f97b6c275b2b8014678f77d (diff) |
Previously, the variable expander would look upwards in the context
stack in case it encountered NIL as a value in the context. This
made it impossible to explicitly assign NIL to a value in case its
name clashed with an enclosing context.
-rw-r--r-- | json-template.lisp | 28 |
1 files changed, 19 insertions, 9 deletions
diff --git a/json-template.lisp b/json-template.lisp index 5607004..032714e 100644 --- a/json-template.lisp +++ b/json-template.lisp @@ -24,6 +24,12 @@ (in-package #:json-template) +(defconstant +not-found+ + (if (boundp '+not-found+) + (symbol-value '+not-found+) + '#:not-found)) + + (defclass template () ((things :initarg :things :accessor template-things))) @@ -141,12 +147,15 @@ (expand-template-to-stream template (list context) out))) (defun getcontext (context key &aux (result context)) - (ignore-errors - (dolist (key-component key result) - (setq result - (getf result - key-component - nil))))) + (dolist (key-component key result) + (handler-case + (setq result + (getf result + key-component + +not-found+)) + (error (condition) + (declare (ignore condition)) + (return-from getcontext +not-found+))))) (defun listify-key (key &optional (start 0)) (let ((dot (position #\. key :start start))) @@ -165,11 +174,12 @@ (labels ((lookup-in-stack (context-stack) (if (endp context-stack) nil - (or (getcontext (first context-stack) key) - (lookup-in-stack (rest context-stack)))))) + (let ((thing (getcontext (first context-stack) key))) + (if (eql thing +not-found+) + (lookup-in-stack (rest context-stack)) + thing))))) (lookup-in-stack contexts))) - (defun expand-things-to-stream (template contexts stream) (dolist (thing template) (ecase (first thing) |