Ensure non-nil maps

This commit is contained in:
Elara 2023-11-01 12:04:54 -07:00
parent 4530664371
commit ba986b9c23
2 changed files with 18 additions and 8 deletions

View File

@ -43,7 +43,13 @@ func New() *Namespace {
func (n *Namespace) WithVarMap(m map[string]any) *Namespace {
n.mu.Lock()
defer n.mu.Unlock()
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

View File

@ -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 {
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
}
return t
}