Skip to content

Commit 376704d

Browse files
committed
tpl/collections: Fix apply when function have Context as first arg
As introduced in `partial` and `partialCached` in Hugo 0.93.0. Fixes #9585
1 parent 41b5bc9 commit 376704d

File tree

4 files changed

+65
-12
lines changed

4 files changed

+65
-12
lines changed

Diff for: common/hreflect/helpers.go

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package hreflect
1818

1919
import (
20+
"context"
2021
"reflect"
2122

2223
"github.com/gohugoio/hugo/common/types"
@@ -124,3 +125,5 @@ func indirectInterface(v reflect.Value) reflect.Value {
124125
}
125126
return v.Elem()
126127
}
128+
129+
var ContextInterface = reflect.TypeOf((*context.Context)(nil)).Elem()

Diff for: tpl/collections/apply.go

+10-7
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,18 @@
1414
package collections
1515

1616
import (
17+
"context"
1718
"errors"
1819
"fmt"
1920
"reflect"
2021
"strings"
2122

23+
"github.com/gohugoio/hugo/common/hreflect"
2224
"github.com/gohugoio/hugo/tpl"
2325
)
2426

2527
// Apply takes a map, array, or slice and returns a new slice with the function fname applied over it.
26-
func (ns *Namespace) Apply(seq interface{}, fname string, args ...interface{}) (interface{}, error) {
28+
func (ns *Namespace) Apply(ctx context.Context, seq interface{}, fname string, args ...interface{}) (interface{}, error) {
2729
if seq == nil {
2830
return make([]interface{}, 0), nil
2931
}
@@ -43,15 +45,13 @@ func (ns *Namespace) Apply(seq interface{}, fname string, args ...interface{}) (
4345
return nil, errors.New("can't find function " + fname)
4446
}
4547

46-
// fnv := reflect.ValueOf(fn)
47-
4848
switch seqv.Kind() {
4949
case reflect.Array, reflect.Slice:
5050
r := make([]interface{}, seqv.Len())
5151
for i := 0; i < seqv.Len(); i++ {
5252
vv := seqv.Index(i)
5353

54-
vvv, err := applyFnToThis(fnv, vv, args...)
54+
vvv, err := applyFnToThis(ctx, fnv, vv, args...)
5555
if err != nil {
5656
return nil, err
5757
}
@@ -65,7 +65,12 @@ func (ns *Namespace) Apply(seq interface{}, fname string, args ...interface{}) (
6565
}
6666
}
6767

68-
func applyFnToThis(fn, this reflect.Value, args ...interface{}) (reflect.Value, error) {
68+
func applyFnToThis(ctx context.Context, fn, this reflect.Value, args ...interface{}) (reflect.Value, error) {
69+
num := fn.Type().NumIn()
70+
if num > 0 && fn.Type().In(0).Implements(hreflect.ContextInterface) {
71+
args = append([]interface{}{ctx}, args...)
72+
}
73+
6974
n := make([]reflect.Value, len(args))
7075
for i, arg := range args {
7176
if arg == "." {
@@ -75,8 +80,6 @@ func applyFnToThis(fn, this reflect.Value, args ...interface{}) (reflect.Value,
7580
}
7681
}
7782

78-
num := fn.Type().NumIn()
79-
8083
if fn.Type().IsVariadic() {
8184
num--
8285
}

Diff for: tpl/collections/apply_test.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -73,21 +73,23 @@ func TestApply(t *testing.T) {
7373

7474
strings := []interface{}{"a\n", "b\n"}
7575

76-
result, err := ns.Apply(strings, "print", "a", "b", "c")
76+
ctx := context.Background()
77+
78+
result, err := ns.Apply(ctx, strings, "print", "a", "b", "c")
7779
c.Assert(err, qt.IsNil)
7880
c.Assert(result, qt.DeepEquals, []interface{}{"abc", "abc"})
7981

80-
_, err = ns.Apply(strings, "apply", ".")
82+
_, err = ns.Apply(ctx, strings, "apply", ".")
8183
c.Assert(err, qt.Not(qt.IsNil))
8284

8385
var nilErr *error
84-
_, err = ns.Apply(nilErr, "chomp", ".")
86+
_, err = ns.Apply(ctx, nilErr, "chomp", ".")
8587
c.Assert(err, qt.Not(qt.IsNil))
8688

87-
_, err = ns.Apply(strings, "dobedobedo", ".")
89+
_, err = ns.Apply(ctx, strings, "dobedobedo", ".")
8890
c.Assert(err, qt.Not(qt.IsNil))
8991

90-
_, err = ns.Apply(strings, "foo.Chomp", "c\n")
92+
_, err = ns.Apply(ctx, strings, "foo.Chomp", "c\n")
9193
if err == nil {
9294
t.Errorf("apply with unknown func should fail")
9395
}

Diff for: tpl/collections/integration_test.go

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2022 The Hugo Authors. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package collections_test
15+
16+
import (
17+
"testing"
18+
19+
"github.com/gohugoio/hugo/hugolib"
20+
)
21+
22+
// Issue 9585
23+
func TestApplyWithContext(t *testing.T) {
24+
t.Parallel()
25+
26+
files := `
27+
-- config.toml --
28+
baseURL = 'http://example.com/'
29+
-- layouts/index.html --
30+
{{ apply (seq 3) "partial" "foo.html"}}
31+
-- layouts/partials/foo.html --
32+
{{ return "foo"}}
33+
`
34+
35+
b := hugolib.NewIntegrationTestBuilder(
36+
hugolib.IntegrationTestConfig{
37+
T: t,
38+
TxtarString: files,
39+
},
40+
).Build()
41+
42+
b.AssertFileContent("public/index.html", `
43+
[foo foo foo]
44+
`)
45+
}

0 commit comments

Comments
 (0)