tpl/partials: Allow any key type in partialCached
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Mon, 2 Dec 2019 20:10:27 +0000 (21:10 +0100)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Mon, 2 Dec 2019 23:13:47 +0000 (00:13 +0100)
Fixes #6572

helpers/general.go
helpers/general_test.go
hugolib/template_test.go
resources/image.go
resources/images/filters_test.go
resources/internal/key.go
resources/internal/key_test.go
tpl/partials/partials.go
tpl/partials/partials_test.go [new file with mode: 0644]

index 699ddeb538720e92dcc4884c79eed92801feb2d1..aa1e00d3a1542ed811d5bd1b269257269bb1527f 100644 (file)
@@ -23,11 +23,14 @@ import (
        "os"
        "path/filepath"
        "sort"
+       "strconv"
        "strings"
        "sync"
        "unicode"
        "unicode/utf8"
 
+       "github.com/mitchellh/hashstructure"
+
        "github.com/gohugoio/hugo/hugofs"
 
        "github.com/gohugoio/hugo/common/hugo"
@@ -482,3 +485,20 @@ func PrintFs(fs afero.Fs, path string, w io.Writer) {
                return nil
        })
 }
+
+// HashString returns a hash from the given elements.
+// It will panic if the hash cannot be calculated.
+func HashString(elements ...interface{}) string {
+       var o interface{}
+       if len(elements) == 1 {
+               o = elements[0]
+       } else {
+               o = elements
+       }
+
+       hash, err := hashstructure.Hash(o, nil)
+       if err != nil {
+               panic(err)
+       }
+       return strconv.FormatUint(hash, 10)
+}
index b8a98fb69e94b93746183fdb0b793016172818dd..104a4c35def6c545b37b712c1e8754cca9c3d499 100644 (file)
@@ -408,3 +408,10 @@ func BenchmarkUniqueStrings(b *testing.B) {
        })
 
 }
+
+func TestHashString(t *testing.T) {
+       c := qt.New(t)
+
+       c.Assert(HashString("a", "b"), qt.Equals, "2712570657419664240")
+       c.Assert(HashString("ab"), qt.Equals, "590647783936702392")
+}
index de93f1c806b8e4cca61dfb0ece91512a7fa4bdea..71b4b46c0bfdb9a9e2940e5d600e42f4feed7247 100644 (file)
@@ -308,3 +308,26 @@ complex: 80: {{ partial "complex.tpl" 38 }}
        )
 
 }
+
+func TestPartialCached(t *testing.T) {
+       b := newTestSitesBuilder(t)
+
+       b.WithTemplatesAdded(
+               "index.html", `
+{{ $key1 := (dict "a" "av" ) }}
+{{ $key2 := (dict "a" "av2" ) }}
+Partial cached1: {{ partialCached "p1" "input1" $key1 }}
+Partial cached2: {{ partialCached "p1" "input2" $key1 }}
+Partial cached3: {{ partialCached "p1" "input3" $key2 }}
+`,
+               "partials/p1.html", `partial: {{ . }}`,
+       )
+
+       b.Build(BuildCfg{})
+
+       b.AssertFileContent("public/index.html", `
+ Partial cached1: partial: input1
+ Partial cached2: partial: input1
+ Partial cached3: partial: input3
+`)
+}
index cdea8a2a7d67911f66e5e365df37658e96ed128a..076f2ae4d63b1b6e2de1e3308f6e7bdb791d4d33 100644 (file)
@@ -34,8 +34,6 @@ import (
        "github.com/gohugoio/hugo/cache/filecache"
        "github.com/gohugoio/hugo/resources/images/exif"
 
-       "github.com/gohugoio/hugo/resources/internal"
-
        "github.com/gohugoio/hugo/resources/resource"
 
        _errors "github.com/pkg/errors"
@@ -218,7 +216,7 @@ func (i *imageResource) Filter(filters ...interface{}) (resource.Image, error) {
                gfilters = append(gfilters, images.ToFilters(f)...)
        }
 
-       conf.Key = internal.HashString(gfilters)
+       conf.Key = helpers.HashString(gfilters)
        conf.TargetFormat = i.Format
 
        return i.doWithImageConfig(conf, func(src image.Image) (image.Image, error) {
@@ -362,7 +360,7 @@ func (i *imageResource) getImageMetaCacheTargetPath() string {
        }
        p1, _ := helpers.FileAndExt(df.file)
        h, _ := i.hash()
-       idStr := internal.HashString(h, i.size(), imageMetaVersionNumber, cfg)
+       idStr := helpers.HashString(h, i.size(), imageMetaVersionNumber, cfg)
        return path.Join(df.dir, fmt.Sprintf("%s_%s.json", p1, idStr))
 }
 
index 658a9a42797e041b6e9ab68622461eef6c764f31..1243e483beb7c45bdf747ed68d9de48a9d584af8 100644 (file)
@@ -16,7 +16,7 @@ package images
 import (
        "testing"
 
-       "github.com/gohugoio/hugo/resources/internal"
+       "github.com/gohugoio/hugo/helpers"
 
        qt "github.com/frankban/quicktest"
 )
@@ -26,9 +26,9 @@ func TestFilterHash(t *testing.T) {
 
        f := &Filters{}
 
-       c.Assert(internal.HashString(f.Grayscale()), qt.Equals, internal.HashString(f.Grayscale()))
-       c.Assert(internal.HashString(f.Grayscale()), qt.Not(qt.Equals), internal.HashString(f.Invert()))
-       c.Assert(internal.HashString(f.Gamma(32)), qt.Not(qt.Equals), internal.HashString(f.Gamma(33)))
-       c.Assert(internal.HashString(f.Gamma(32)), qt.Equals, internal.HashString(f.Gamma(32)))
+       c.Assert(helpers.HashString(f.Grayscale()), qt.Equals, helpers.HashString(f.Grayscale()))
+       c.Assert(helpers.HashString(f.Grayscale()), qt.Not(qt.Equals), helpers.HashString(f.Invert()))
+       c.Assert(helpers.HashString(f.Gamma(32)), qt.Not(qt.Equals), helpers.HashString(f.Gamma(33)))
+       c.Assert(helpers.HashString(f.Gamma(32)), qt.Equals, helpers.HashString(f.Gamma(32)))
 
 }
index 17543b0d49489239cf369cfe1d940029de5afdd9..d67d4a7e13cb523e9b46942c628c9e27ac21c52a 100644 (file)
 
 package internal
 
-import (
-       "strconv"
-
-       "github.com/mitchellh/hashstructure"
-)
+import "github.com/gohugoio/hugo/helpers"
 
 // ResourceTransformationKey are provided by the different transformation implementations.
 // It identifies the transformation (name) and its configuration (elements).
@@ -42,23 +38,6 @@ func (k ResourceTransformationKey) Value() string {
                return k.Name
        }
 
-       return k.Name + "_" + HashString(k.elements...)
-
-}
-
-// HashString returns a hash from the given elements.
-// It will panic if the hash cannot be calculated.
-func HashString(elements ...interface{}) string {
-       var o interface{}
-       if len(elements) == 1 {
-               o = elements[0]
-       } else {
-               o = elements
-       }
+       return k.Name + "_" + helpers.HashString(k.elements...)
 
-       hash, err := hashstructure.Hash(o, nil)
-       if err != nil {
-               panic(err)
-       }
-       return strconv.FormatUint(hash, 10)
 }
index 11a52f2e687eef5d9ac7afc10ea2ef4e0b940a2d..38286333d4a0208d596b3b68bc98206bf5506181 100644 (file)
@@ -34,10 +34,3 @@ func TestResourceTransformationKey(t *testing.T) {
        c := qt.New(t)
        c.Assert(key.Value(), qt.Equals, "testing_518996646957295636")
 }
-
-func TestHashString(t *testing.T) {
-       c := qt.New(t)
-
-       c.Assert(HashString("a", "b"), qt.Equals, "2712570657419664240")
-       c.Assert(HashString("ab"), qt.Equals, "590647783936702392")
-}
index 2599a5d0133c6af2bcf860c6c1333e4e08234c3d..da42acb20bcffa9a3d77be3f7a94558fe22d91e0 100644 (file)
 package partials
 
 import (
+       "errors"
        "fmt"
        "html/template"
        "io"
        "io/ioutil"
+       "reflect"
        "strings"
        "sync"
        texttemplate "text/template"
 
+       "github.com/gohugoio/hugo/helpers"
+
        "github.com/gohugoio/hugo/tpl"
 
        bp "github.com/gohugoio/hugo/bufferpool"
@@ -34,21 +38,26 @@ import (
 // NOTE: It's currently unused.
 var TestTemplateProvider deps.ResourceProvider
 
+type partialCacheKey struct {
+       name    string
+       variant interface{}
+}
+
 // partialCache represents a cache of partials protected by a mutex.
 type partialCache struct {
        sync.RWMutex
-       p map[string]interface{}
+       p map[partialCacheKey]interface{}
 }
 
 func (p *partialCache) clear() {
        p.Lock()
        defer p.Unlock()
-       p.p = make(map[string]interface{})
+       p.p = make(map[partialCacheKey]interface{})
 }
 
 // New returns a new instance of the templates-namespaced template functions.
 func New(deps *deps.Deps) *Namespace {
-       cache := &partialCache{p: make(map[string]interface{})}
+       cache := &partialCache{p: make(map[partialCacheKey]interface{})}
        deps.BuildStartListeners.Add(
                func() {
                        cache.clear()
@@ -151,21 +160,56 @@ func (ns *Namespace) Include(name string, contextList ...interface{}) (interface
 
 }
 
-// IncludeCached executes and caches partial templates.  An optional variant
-// string parameter (a string slice actually, but be only use a variadic
-// argument to make it optional) can be passed so that a given partial can have
-// multiple uses. The cache is created with name+variant as the key.
-func (ns *Namespace) IncludeCached(name string, context interface{}, variant ...string) (interface{}, error) {
-       key := name
-       if len(variant) > 0 {
-               for i := 0; i < len(variant); i++ {
-                       key += variant[i]
+// IncludeCached executes and caches partial templates.  The cache is created with name+variants as the key.
+func (ns *Namespace) IncludeCached(name string, context interface{}, variants ...interface{}) (interface{}, error) {
+       key, err := createKey(name, variants...)
+       if err != nil {
+               return nil, err
+       }
+
+       result, err := ns.getOrCreate(key, context)
+       if err == errUnHashable {
+               // Try one more
+               key.variant = helpers.HashString(key.variant)
+               result, err = ns.getOrCreate(key, context)
+       }
+
+       return result, err
+}
+
+func createKey(name string, variants ...interface{}) (partialCacheKey, error) {
+       var variant interface{}
+
+       if len(variants) > 1 {
+               variant = helpers.HashString(variants...)
+       } else if len(variants) == 1 {
+               variant = variants[0]
+               t := reflect.TypeOf(variant)
+               switch t.Kind() {
+               // This isn't an exhaustive list of unhashable types.
+               // There may be structs with slices,
+               // but that should be very rare. We do recover from that situation
+               // below.
+               case reflect.Slice, reflect.Array, reflect.Map:
+                       variant = helpers.HashString(variant)
                }
        }
-       return ns.getOrCreate(key, name, context)
+
+       return partialCacheKey{name: name, variant: variant}, nil
 }
 
-func (ns *Namespace) getOrCreate(key, name string, context interface{}) (interface{}, error) {
+var errUnHashable = errors.New("unhashable")
+
+func (ns *Namespace) getOrCreate(key partialCacheKey, context interface{}) (result interface{}, err error) {
+       defer func() {
+               if r := recover(); r != nil {
+                       err = r.(error)
+                       if strings.Contains(err.Error(), "unhashable type") {
+                               ns.cachedPartials.RUnlock()
+                               err = errUnHashable
+                       }
+               }
+       }()
 
        ns.cachedPartials.RLock()
        p, ok := ns.cachedPartials.p[key]
@@ -175,7 +219,7 @@ func (ns *Namespace) getOrCreate(key, name string, context interface{}) (interfa
                return p, nil
        }
 
-       p, err := ns.Include(name, context)
+       p, err = ns.Include(key.name, context)
        if err != nil {
                return nil, err
        }
diff --git a/tpl/partials/partials_test.go b/tpl/partials/partials_test.go
new file mode 100644 (file)
index 0000000..60e9dd7
--- /dev/null
@@ -0,0 +1,41 @@
+// Copyright 2019 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 partials
+
+import (
+       "testing"
+
+       qt "github.com/frankban/quicktest"
+)
+
+func TestCreateKey(t *testing.T) {
+       c := qt.New(t)
+       m := make(map[interface{}]bool)
+
+       create := func(name string, variants ...interface{}) partialCacheKey {
+               k, err := createKey(name, variants...)
+               c.Assert(err, qt.IsNil)
+               m[k] = true
+               return k
+       }
+
+       for i := 0; i < 123; i++ {
+               c.Assert(create("a", "b"), qt.Equals, partialCacheKey{name: "a", variant: "b"})
+               c.Assert(create("a", "b", "c"), qt.Equals, partialCacheKey{name: "a", variant: "9629524865311698396"})
+               c.Assert(create("a", 1), qt.Equals, partialCacheKey{name: "a", variant: 1})
+               c.Assert(create("a", map[string]string{"a": "av"}), qt.Equals, partialCacheKey{name: "a", variant: "4809626101226749924"})
+               c.Assert(create("a", []string{"a", "b"}), qt.Equals, partialCacheKey{name: "a", variant: "2712570657419664240"})
+       }
+
+}