Treat empty hash as nil

This commit is contained in:
Andinus 2025-12-02 00:55:32 +05:30
parent 39314e3403
commit 22a98f022b
Signed by: andinus
SSH Key Fingerprint: SHA256:rop/w9c/gwtEJhUWS5curN3KJEwRr+Fm1ojM3liQTqo
3 changed files with 27 additions and 1 deletions

@ -10,6 +10,14 @@ https://pypi.org/project/template-nest/.
* News
** v0.1.6 - 2025-12-02
+ Treat empty hash as nil
Tom: I think that if go's behaviour is to insert an empty map when nil is
returned as a templatenest.Hash, then we need to change the templatenest
behaviour to regard an empty map as being nil.
** v0.1.5 - 2025-02-22
+ Handle all pointer types generically.

@ -364,10 +364,15 @@ func (nest *TemplateNest) renderSlice(toRender interface{}) (string, error) {
}
func (nest *TemplateNest) renderHash(hash map[string]interface{}) (string, error) {
// If the hash is completely empty, return an empty string.
if len(hash) == 0 {
return "", nil
}
tLabel, ok := hash[*nest.option.NameLabel]
if !ok {
return "", fmt.Errorf(
"encountered hash with no name label (name label: `%s`)", nest.option.NameLabel,
"encountered hash with no name label (name label: `%+v`)", nest.option.NameLabel,
)
}

@ -174,6 +174,19 @@ func TestRenderNoNameLabel(t *testing.T) {
}
}
func TestRenderEmptyHash(t *testing.T) {
nest, err := templatenest.New(templatenest.Option{TemplateDir: "templates"})
if err != nil {
t.Fatalf("Failed to initialize TemplateNest: %+v", err)
}
page := templatenest.Hash{}
render, err := nest.Render(page)
assert.Nil(t, err)
assert.Equal(t, "", render)
}
func TestRenderSimplePageArrays(t *testing.T) {
nest, err := templatenest.New(templatenest.Option{
TemplateDir: "templates",