Skip to content

Commit 7ab1310

Browse files
greynewellclaude
andauthored
fix(render): last() silently returns nil for []*entity.Entity slices (#101)
last() handled []string and []interface{} but was missing the []*entity.Entity case that first() already had. Any template calling {{ last .Entities }} on an entity list would receive nil and produce a silent empty output instead of the last entity. Also adds TestFirstLast_EntitySlice covering both functions with entity slices, including empty-slice behaviour. Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 88cb8c7 commit 7ab1310

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

internal/archdocs/pssg/render/funcs.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,10 @@ func last(list interface{}) interface{} {
227227
if len(v) > 0 {
228228
return v[len(v)-1]
229229
}
230+
case []*entity.Entity:
231+
if len(v) > 0 {
232+
return v[len(v)-1]
233+
}
230234
}
231235
return nil
232236
}

internal/archdocs/pssg/render/funcs_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package render
22

33
import (
44
"testing"
5+
6+
"github.com/supermodeltools/cli/internal/archdocs/pssg/entity"
57
)
68

79
func TestFormatNumber(t *testing.T) {
@@ -76,6 +78,28 @@ func TestSliceHelper(t *testing.T) {
7678
}
7779
}
7880

81+
func TestFirstLast_EntitySlice(t *testing.T) {
82+
a := &entity.Entity{Slug: "a"}
83+
b := &entity.Entity{Slug: "b"}
84+
c := &entity.Entity{Slug: "c"}
85+
entities := []*entity.Entity{a, b, c}
86+
87+
if got := first(entities); got != a {
88+
t.Errorf("first([]*entity.Entity) = %v, want %v", got, a)
89+
}
90+
if got := last(entities); got != c {
91+
t.Errorf("last([]*entity.Entity) = %v, want %v", got, c)
92+
}
93+
94+
var empty []*entity.Entity
95+
if got := first(empty); got != nil {
96+
t.Errorf("first(empty []*entity.Entity) = %v, want nil", got)
97+
}
98+
if got := last(empty); got != nil {
99+
t.Errorf("last(empty []*entity.Entity) = %v, want nil", got)
100+
}
101+
}
102+
79103
func TestSortStrings(t *testing.T) {
80104
input := []string{"c", "a", "b"}
81105
got := sortStrings(input)

0 commit comments

Comments
 (0)