Update dependencies (#5518)
This commit is contained in:
50
vendor/github.com/open-policy-agent/opa/topdown/glob.go
generated
vendored
50
vendor/github.com/open-policy-agent/opa/topdown/glob.go
generated
vendored
@@ -13,30 +13,31 @@ import (
|
||||
var globCacheLock = sync.Mutex{}
|
||||
var globCache map[string]glob.Glob
|
||||
|
||||
func builtinGlobMatch(a, b, c ast.Value) (ast.Value, error) {
|
||||
pattern, err := builtins.StringOperand(a, 1)
|
||||
func builtinGlobMatch(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
|
||||
pattern, err := builtins.StringOperand(operands[0].Value, 1)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
|
||||
var delimiters []rune
|
||||
switch b.(type) {
|
||||
switch operands[1].Value.(type) {
|
||||
case ast.Null:
|
||||
delimiters = []rune{}
|
||||
case *ast.Array:
|
||||
delimiters, err = builtins.RuneSliceOperand(b, 2)
|
||||
delimiters, err = builtins.RuneSliceOperand(operands[1].Value, 2)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
|
||||
if len(delimiters) == 0 {
|
||||
delimiters = []rune{'.'}
|
||||
}
|
||||
default:
|
||||
return nil, builtins.NewOperandTypeErr(2, b, "array", "null")
|
||||
return builtins.NewOperandTypeErr(2, operands[1].Value, "array", "null")
|
||||
}
|
||||
match, err := builtins.StringOperand(c, 3)
|
||||
|
||||
match, err := builtins.StringOperand(operands[2].Value, 3)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
|
||||
builder := strings.Builder{}
|
||||
@@ -47,32 +48,39 @@ func builtinGlobMatch(a, b, c ast.Value) (ast.Value, error) {
|
||||
}
|
||||
id := builder.String()
|
||||
|
||||
m, err := globCompileAndMatch(id, string(pattern), string(match), delimiters)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return iter(ast.BooleanTerm(m))
|
||||
}
|
||||
|
||||
func globCompileAndMatch(id, pattern, match string, delimiters []rune) (bool, error) {
|
||||
globCacheLock.Lock()
|
||||
defer globCacheLock.Unlock()
|
||||
p, ok := globCache[id]
|
||||
if !ok {
|
||||
var err error
|
||||
if p, err = glob.Compile(string(pattern), delimiters...); err != nil {
|
||||
return nil, err
|
||||
if p, err = glob.Compile(pattern, delimiters...); err != nil {
|
||||
return false, err
|
||||
}
|
||||
globCache[id] = p
|
||||
}
|
||||
|
||||
m := p.Match(string(match))
|
||||
return ast.Boolean(m), nil
|
||||
out := p.Match(match)
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func builtinGlobQuoteMeta(a ast.Value) (ast.Value, error) {
|
||||
pattern, err := builtins.StringOperand(a, 1)
|
||||
func builtinGlobQuoteMeta(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
|
||||
pattern, err := builtins.StringOperand(operands[0].Value, 1)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
|
||||
return ast.String(glob.QuoteMeta(string(pattern))), nil
|
||||
return iter(ast.StringTerm(glob.QuoteMeta(string(pattern))))
|
||||
}
|
||||
|
||||
func init() {
|
||||
globCache = map[string]glob.Glob{}
|
||||
RegisterFunctionalBuiltin3(ast.GlobMatch.Name, builtinGlobMatch)
|
||||
RegisterFunctionalBuiltin1(ast.GlobQuoteMeta.Name, builtinGlobQuoteMeta)
|
||||
RegisterBuiltinFunc(ast.GlobMatch.Name, builtinGlobMatch)
|
||||
RegisterBuiltinFunc(ast.GlobQuoteMeta.Name, builtinGlobQuoteMeta)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user