diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2019-12-02 21:10:27 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2019-12-03 00:13:47 +0100 |
commit | 0efb00c2a86ec3f52000a643f26f54bb2a9dfbd6 (patch) | |
tree | 370d64135277eaa825ead0c25d33e99eb218087e /helpers | |
parent | 40a092b0687d44ecb53ef1fd53001a6299345780 (diff) |
tpl/partials: Allow any key type in partialCached
Fixes #6572
Diffstat (limited to 'helpers')
-rw-r--r-- | helpers/general.go | 20 | ||||
-rw-r--r-- | helpers/general_test.go | 7 |
2 files changed, 27 insertions, 0 deletions
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") +} |