-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathpattern.go
More file actions
44 lines (37 loc) · 788 Bytes
/
pattern.go
File metadata and controls
44 lines (37 loc) · 788 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package glob
import (
"github.com/gobwas/glob"
"github.com/pkg/errors"
"github.com/stackrox/rox/pkg/sync"
)
// Pattern is expected to be a string with a glob pattern.
type Pattern string
var globCache = sync.Map{}
func (p *Pattern) Compile() error {
if p == nil {
return nil
}
_, err := p.compile()
return err
}
func (p *Pattern) compile() (glob.Glob, error) {
g, err := glob.Compile(string(*p))
if err != nil {
return nil, errors.WithMessagef(err, "failed to compile %q", string(*p))
}
globCache.Store(*p, g)
return g, nil
}
func (p *Pattern) Match(s string) bool {
v, ok := globCache.Load(*p)
if !ok {
var err error
if v, err = p.compile(); err != nil {
return false
}
}
return v.(glob.Glob).Match(s)
}
func (p Pattern) Ptr() *Pattern {
return &p
}