From 0efb00c2a86ec3f52000a643f26f54bb2a9dfbd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Mon, 2 Dec 2019 21:10:27 +0100 Subject: tpl/partials: Allow any key type in partialCached Fixes #6572 --- helpers/general.go | 20 ++++++++++++++++++++ helpers/general_test.go | 7 +++++++ 2 files changed, 27 insertions(+) (limited to 'helpers') diff --git a/helpers/general.go b/helpers/general.go index 699ddeb53..aa1e00d3a 100644 --- a/helpers/general.go +++ b/helpers/general.go @@ -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) +} diff --git a/helpers/general_test.go b/helpers/general_test.go index b8a98fb69..104a4c35d 100644 --- a/helpers/general_test.go +++ b/helpers/general_test.go @@ -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") +} -- cgit v1.2.3