forked from open-policy-agent/opa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
regex.go
55 lines (48 loc) · 1.2 KB
/
regex.go
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
45
46
47
48
49
50
51
52
53
54
55
// Copyright 2016 The OPA Authors. All rights reserved.
// Use of this source code is governed by an Apache2
// license that can be found in the LICENSE file.
package topdown
import (
"regexp"
"sync"
"github.com/open-policy-agent/opa/ast"
"github.com/pkg/errors"
)
var regexpCacheLock = sync.Mutex{}
var regexpCache map[string]*regexp.Regexp
func evalRegexMatch(t *Topdown, expr *ast.Expr, iter Iterator) error {
ops := expr.Terms.([]*ast.Term)
pat, err := ValueToString(ops[1].Value, t)
if err != nil {
return errors.Wrapf(err, "re_match: pattern value must be a string")
}
input, err := ValueToString(ops[2].Value, t)
if err != nil {
return errors.Wrapf(err, "re_match: input value must be a string")
}
re, err := getRegexp(pat)
if err != nil {
return err
}
if re.Match([]byte(input)) {
return iter(t)
}
return nil
}
func getRegexp(pat string) (*regexp.Regexp, error) {
regexpCacheLock.Lock()
defer regexpCacheLock.Unlock()
re, ok := regexpCache[pat]
if !ok {
var err error
re, err = regexp.Compile(string(pat))
if err != nil {
return nil, errors.Wrapf(err, "re_match")
}
regexpCache[pat] = re
}
return re, nil
}
func init() {
regexpCache = map[string]*regexp.Regexp{}
}