Treat empty hash as nil
This commit is contained in:
parent
39314e3403
commit
22a98f022b
@ -10,6 +10,14 @@ https://pypi.org/project/template-nest/.
|
|||||||
|
|
||||||
* News
|
* 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
|
** v0.1.5 - 2025-02-22
|
||||||
|
|
||||||
+ Handle all pointer types generically.
|
+ 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) {
|
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]
|
tLabel, ok := hash[*nest.option.NameLabel]
|
||||||
if !ok {
|
if !ok {
|
||||||
return "", fmt.Errorf(
|
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) {
|
func TestRenderSimplePageArrays(t *testing.T) {
|
||||||
nest, err := templatenest.New(templatenest.Option{
|
nest, err := templatenest.New(templatenest.Option{
|
||||||
TemplateDir: "templates",
|
TemplateDir: "templates",
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user