]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
tpl/collections: Fix apply to work with built-in funcs like len
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Wed, 31 Dec 2025 10:10:19 +0000 (11:10 +0100)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Wed, 31 Dec 2025 12:09:04 +0000 (13:09 +0100)
Fixes #13418

tpl/collections/apply.go
tpl/collections/collections_integration_test.go

index 5b3cbe54d1445da2151182b7b46915c3e6a316ee..23c6a74976ad9efc58918a52876233ce2803b64b 100644 (file)
@@ -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)) {
@@ -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())
                }
index 1adfd5abf5c69d365f9fd33de7f5b0345d891265..a1a1485241969c35f157afbd8e1a67b2acd19777 100644 (file)
@@ -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()