Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion tpl/collections/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ func (ns *Namespace) Apply(ctx context.Context, c any, fname string, args ...any
}
}

var typeOfReflectValue = reflect.TypeOf(reflect.Value{})

func applyFnToThis(ctx context.Context, fn, this reflect.Value, args ...any) (reflect.Value, error) {
num := fn.Type().NumIn()
if num > 0 && hreflect.IsContextType(fn.Type().In(0)) {
Expand Down Expand Up @@ -91,7 +93,11 @@ func applyFnToThis(ctx context.Context, fn, this reflect.Value, args ...any) (re
}*/

for i := range num {
// AssignableTo reports whether xt is assignable to type targ.
// Go's built-in template funcs (e.g. len) use reflect.Value as argument type.
if fn.Type().In(i) == typeOfReflectValue && n[i].Type() != typeOfReflectValue {
n[i] = reflect.ValueOf(n[i])
}

if xt, targ := n[i].Type(), fn.Type().In(i); !xt.AssignableTo(targ) {
return reflect.ValueOf(nil), errors.New("called apply using " + xt.String() + " as type " + targ.String())
}
Expand Down
15 changes: 15 additions & 0 deletions tpl/collections/collections_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,21 @@ baseURL = 'http://example.com/'
`)
}

func TestApplyBuiltInIssue13418(t *testing.T) {
t.Parallel()

files := `
-- hugo.toml --
baseURL = 'http://example.com/'
-- layouts/home.html --
len: {{ apply (slice "hello") "len" "." }}
not: {{ apply (slice "hello") "not" "." }}
`
b := hugolib.Test(t, files)

b.AssertFileContent("public/index.html", "len: [5]", "not: [false]")
}

// Issue 9865
func TestSortStable(t *testing.T) {
t.Parallel()
Expand Down