From 36838f04f1b3967181993530dc5a03c5bcb9908a Mon Sep 17 00:00:00 2001 From: Matthias Andreas Benkard Date: Sun, 14 May 2017 17:38:52 +0200 Subject: Fix handling of NIL values. 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. --- json-template.lisp | 28 +++++++++++++++++++--------- 1 file 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) -- cgit v1.2.3