From ba986b9c2350b58c87065ae29aaac2b4d051a9d9 Mon Sep 17 00:00:00 2001 From: Elara Musayelyan Date: Wed, 1 Nov 2023 12:04:54 -0700 Subject: [PATCH] Ensure non-nil maps --- namespace.go | 14 ++++++++++---- salix.go | 12 ++++++++---- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/namespace.go b/namespace.go index a15d3d3..58f4ce2 100644 --- a/namespace.go +++ b/namespace.go @@ -43,7 +43,13 @@ func New() *Namespace { func (n *Namespace) WithVarMap(m map[string]any) *Namespace { n.mu.Lock() defer n.mu.Unlock() - n.vars = m + + if m == nil { + n.vars = map[string]any{} + } else { + n.vars = m + } + return n } @@ -52,10 +58,10 @@ func (n *Namespace) WithTagMap(m map[string]Tag) *Namespace { n.mu.Lock() defer n.mu.Unlock() - if m != nil { - n.tags = m - } else { + if m == nil { n.tags = map[string]Tag{} + } else { + n.tags = m } return n diff --git a/salix.go b/salix.go index f943bda..1c3f6a0 100644 --- a/salix.go +++ b/salix.go @@ -67,17 +67,21 @@ type Template struct { // WithVarMap returns a copy of the template with its variable map set to m. func (t Template) WithVarMap(m map[string]any) Template { - t.vars = m + if m == nil { + t.vars = map[string]any{} + } else { + t.vars = m + } return t } // WithTagMap returns a copy of the template with its tag map set to m. func (t Template) WithTagMap(m map[string]Tag) Template { - // Make sure the tag map is never nil to avoid panics if m == nil { - m = map[string]Tag{} + t.tags = map[string]Tag{} + } else { + t.tags = m } - t.tags = m return t }