Skip to content

Commit

Permalink
tpl/collections: Fix apply when function have Context as first arg
Browse files Browse the repository at this point in the history
As introduced in `partial` and `partialCached` in Hugo 0.93.0.

Fixes #9585
  • Loading branch information
bep committed Mar 1, 2022
1 parent 41b5bc9 commit 376704d
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 12 deletions.
3 changes: 3 additions & 0 deletions common/hreflect/helpers.go
Expand Up @@ -17,6 +17,7 @@
package hreflect

import (
"context"
"reflect"

"github.com/gohugoio/hugo/common/types"
Expand Down Expand Up @@ -124,3 +125,5 @@ func indirectInterface(v reflect.Value) reflect.Value {
}
return v.Elem()
}

var ContextInterface = reflect.TypeOf((*context.Context)(nil)).Elem()
17 changes: 10 additions & 7 deletions tpl/collections/apply.go
Expand Up @@ -14,16 +14,18 @@
package collections

import (
"context"
"errors"
"fmt"
"reflect"
"strings"

"github.com/gohugoio/hugo/common/hreflect"
"github.com/gohugoio/hugo/tpl"
)

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

// fnv := reflect.ValueOf(fn)

switch seqv.Kind() {
case reflect.Array, reflect.Slice:
r := make([]interface{}, seqv.Len())
for i := 0; i < seqv.Len(); i++ {
vv := seqv.Index(i)

vvv, err := applyFnToThis(fnv, vv, args...)
vvv, err := applyFnToThis(ctx, fnv, vv, args...)
if err != nil {
return nil, err
}
Expand All @@ -65,7 +65,12 @@ func (ns *Namespace) Apply(seq interface{}, fname string, args ...interface{}) (
}
}

func applyFnToThis(fn, this reflect.Value, args ...interface{}) (reflect.Value, error) {
func applyFnToThis(ctx context.Context, fn, this reflect.Value, args ...interface{}) (reflect.Value, error) {
num := fn.Type().NumIn()
if num > 0 && fn.Type().In(0).Implements(hreflect.ContextInterface) {
args = append([]interface{}{ctx}, args...)
}

n := make([]reflect.Value, len(args))
for i, arg := range args {
if arg == "." {
Expand All @@ -75,8 +80,6 @@ func applyFnToThis(fn, this reflect.Value, args ...interface{}) (reflect.Value,
}
}

num := fn.Type().NumIn()

if fn.Type().IsVariadic() {
num--
}
Expand Down
12 changes: 7 additions & 5 deletions tpl/collections/apply_test.go
Expand Up @@ -73,21 +73,23 @@ func TestApply(t *testing.T) {

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

result, err := ns.Apply(strings, "print", "a", "b", "c")
ctx := context.Background()

result, err := ns.Apply(ctx, strings, "print", "a", "b", "c")
c.Assert(err, qt.IsNil)
c.Assert(result, qt.DeepEquals, []interface{}{"abc", "abc"})

_, err = ns.Apply(strings, "apply", ".")
_, err = ns.Apply(ctx, strings, "apply", ".")
c.Assert(err, qt.Not(qt.IsNil))

var nilErr *error
_, err = ns.Apply(nilErr, "chomp", ".")
_, err = ns.Apply(ctx, nilErr, "chomp", ".")
c.Assert(err, qt.Not(qt.IsNil))

_, err = ns.Apply(strings, "dobedobedo", ".")
_, err = ns.Apply(ctx, strings, "dobedobedo", ".")
c.Assert(err, qt.Not(qt.IsNil))

_, err = ns.Apply(strings, "foo.Chomp", "c\n")
_, err = ns.Apply(ctx, strings, "foo.Chomp", "c\n")
if err == nil {
t.Errorf("apply with unknown func should fail")
}
Expand Down
45 changes: 45 additions & 0 deletions tpl/collections/integration_test.go
@@ -0,0 +1,45 @@
// Copyright 2022 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package collections_test

import (
"testing"

"github.com/gohugoio/hugo/hugolib"
)

// Issue 9585
func TestApplyWithContext(t *testing.T) {
t.Parallel()

files := `
-- config.toml --
baseURL = 'http://example.com/'
-- layouts/index.html --
{{ apply (seq 3) "partial" "foo.html"}}
-- layouts/partials/foo.html --
{{ return "foo"}}
`

b := hugolib.NewIntegrationTestBuilder(
hugolib.IntegrationTestConfig{
T: t,
TxtarString: files,
},
).Build()

b.AssertFileContent("public/index.html", `
[foo foo foo]
`)
}

0 comments on commit 376704d

Please sign in to comment.