diff options
author | Bjørn Erik Pedersen <[email protected]> | 2020-10-05 20:01:52 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <[email protected]> | 2020-10-06 14:06:10 +0200 |
commit | c63db7f1f6774a2d661af1d8197c6fe377e3ad25 (patch) | |
tree | bbeccc7dd4f41acdcc1cfd584ea351e6284869d5 /hugofs | |
parent | 5e2a547cb594b31ecb0f089b08db2e15c6dc381a (diff) | |
download | hugo-c63db7f1f6774a2d661af1d8197c6fe377e3ad25.tar.gz hugo-c63db7f1f6774a2d661af1d8197c6fe377e3ad25.zip |
Allow cascade to be a slice with a _target discriminator
Fixes #7782
Diffstat (limited to 'hugofs')
-rw-r--r-- | hugofs/glob/glob.go | 34 | ||||
-rw-r--r-- | hugofs/glob/glob_test.go | 9 |
2 files changed, 29 insertions, 14 deletions
diff --git a/hugofs/glob/glob.go b/hugofs/glob/glob.go index 124a3d50e..88485e1f0 100644 --- a/hugofs/glob/glob.go +++ b/hugofs/glob/glob.go @@ -23,30 +23,36 @@ import ( "github.com/gobwas/glob/syntax" ) +type globErr struct { + glob glob.Glob + err error +} + var ( - globCache = make(map[string]glob.Glob) + globCache = make(map[string]globErr) globMu sync.RWMutex ) func GetGlob(pattern string) (glob.Glob, error) { - var g glob.Glob + var eg globErr globMu.RLock() - g, found := globCache[pattern] + var found bool + eg, found = globCache[pattern] globMu.RUnlock() - if !found { - var err error - g, err = glob.Compile(strings.ToLower(pattern), '/') - if err != nil { - return nil, err - } - - globMu.Lock() - globCache[pattern] = g - globMu.Unlock() + if found { + return eg.glob, eg.err } - return g, nil + var err error + g, err := glob.Compile(strings.ToLower(pattern), '/') + eg = globErr{g, err} + + globMu.Lock() + globCache[pattern] = eg + globMu.Unlock() + + return eg.glob, eg.err } diff --git a/hugofs/glob/glob_test.go b/hugofs/glob/glob_test.go index cca8e4e0f..5a6ef5b7e 100644 --- a/hugofs/glob/glob_test.go +++ b/hugofs/glob/glob_test.go @@ -73,5 +73,14 @@ func TestGetGlob(t *testing.T) { g, err := GetGlob("**.JSON") c.Assert(err, qt.IsNil) c.Assert(g.Match("data/my.json"), qt.Equals, true) +} + +func BenchmarkGetGlob(b *testing.B) { + for i := 0; i < b.N; i++ { + _, err := GetGlob("**/foo") + if err != nil { + b.Fatal(err) + } + } } |