Upgrade dependent version: github.com/open-policy-agent/opa (#5315)
Upgrade dependent version: github.com/open-policy-agent/opa v0.18.0 -> v0.45.0 Signed-off-by: hongzhouzi <hongzhouzi@kubesphere.io> Signed-off-by: hongzhouzi <hongzhouzi@kubesphere.io>
This commit is contained in:
134
vendor/github.com/open-policy-agent/opa/topdown/aggregates.go
generated
vendored
134
vendor/github.com/open-policy-agent/opa/topdown/aggregates.go
generated
vendored
@@ -13,30 +13,31 @@ import (
|
||||
|
||||
func builtinCount(a ast.Value) (ast.Value, error) {
|
||||
switch a := a.(type) {
|
||||
case ast.Array:
|
||||
return ast.IntNumberTerm(len(a)).Value, nil
|
||||
case *ast.Array:
|
||||
return ast.IntNumberTerm(a.Len()).Value, nil
|
||||
case ast.Object:
|
||||
return ast.IntNumberTerm(a.Len()).Value, nil
|
||||
case ast.Set:
|
||||
return ast.IntNumberTerm(a.Len()).Value, nil
|
||||
case ast.String:
|
||||
return ast.IntNumberTerm(len(a)).Value, nil
|
||||
return ast.IntNumberTerm(len([]rune(a))).Value, nil
|
||||
}
|
||||
return nil, builtins.NewOperandTypeErr(1, a, "array", "object", "set")
|
||||
return nil, builtins.NewOperandTypeErr(1, a, "array", "object", "set", "string")
|
||||
}
|
||||
|
||||
func builtinSum(a ast.Value) (ast.Value, error) {
|
||||
switch a := a.(type) {
|
||||
case ast.Array:
|
||||
case *ast.Array:
|
||||
sum := big.NewFloat(0)
|
||||
for _, x := range a {
|
||||
err := a.Iter(func(x *ast.Term) error {
|
||||
n, ok := x.Value.(ast.Number)
|
||||
if !ok {
|
||||
return nil, builtins.NewOperandElementErr(1, a, x.Value, "number")
|
||||
return builtins.NewOperandElementErr(1, a, x.Value, "number")
|
||||
}
|
||||
sum = new(big.Float).Add(sum, builtins.NumberToFloat(n))
|
||||
}
|
||||
return builtins.FloatToNumber(sum), nil
|
||||
return nil
|
||||
})
|
||||
return builtins.FloatToNumber(sum), err
|
||||
case ast.Set:
|
||||
sum := big.NewFloat(0)
|
||||
err := a.Iter(func(x *ast.Term) error {
|
||||
@@ -54,16 +55,17 @@ func builtinSum(a ast.Value) (ast.Value, error) {
|
||||
|
||||
func builtinProduct(a ast.Value) (ast.Value, error) {
|
||||
switch a := a.(type) {
|
||||
case ast.Array:
|
||||
case *ast.Array:
|
||||
product := big.NewFloat(1)
|
||||
for _, x := range a {
|
||||
err := a.Iter(func(x *ast.Term) error {
|
||||
n, ok := x.Value.(ast.Number)
|
||||
if !ok {
|
||||
return nil, builtins.NewOperandElementErr(1, a, x.Value, "number")
|
||||
return builtins.NewOperandElementErr(1, a, x.Value, "number")
|
||||
}
|
||||
product = new(big.Float).Mul(product, builtins.NumberToFloat(n))
|
||||
}
|
||||
return builtins.FloatToNumber(product), nil
|
||||
return nil
|
||||
})
|
||||
return builtins.FloatToNumber(product), err
|
||||
case ast.Set:
|
||||
product := big.NewFloat(1)
|
||||
err := a.Iter(func(x *ast.Term) error {
|
||||
@@ -81,16 +83,16 @@ func builtinProduct(a ast.Value) (ast.Value, error) {
|
||||
|
||||
func builtinMax(a ast.Value) (ast.Value, error) {
|
||||
switch a := a.(type) {
|
||||
case ast.Array:
|
||||
if len(a) == 0 {
|
||||
case *ast.Array:
|
||||
if a.Len() == 0 {
|
||||
return nil, BuiltinEmpty{}
|
||||
}
|
||||
var max = ast.Value(ast.Null{})
|
||||
for i := range a {
|
||||
if ast.Compare(max, a[i].Value) <= 0 {
|
||||
max = a[i].Value
|
||||
a.Foreach(func(x *ast.Term) {
|
||||
if ast.Compare(max, x.Value) <= 0 {
|
||||
max = x.Value
|
||||
}
|
||||
}
|
||||
})
|
||||
return max, nil
|
||||
case ast.Set:
|
||||
if a.Len() == 0 {
|
||||
@@ -110,16 +112,16 @@ func builtinMax(a ast.Value) (ast.Value, error) {
|
||||
|
||||
func builtinMin(a ast.Value) (ast.Value, error) {
|
||||
switch a := a.(type) {
|
||||
case ast.Array:
|
||||
if len(a) == 0 {
|
||||
case *ast.Array:
|
||||
if a.Len() == 0 {
|
||||
return nil, BuiltinEmpty{}
|
||||
}
|
||||
min := a[0].Value
|
||||
for i := range a {
|
||||
if ast.Compare(min, a[i].Value) >= 0 {
|
||||
min = a[i].Value
|
||||
min := a.Elem(0).Value
|
||||
a.Foreach(func(x *ast.Term) {
|
||||
if ast.Compare(min, x.Value) >= 0 {
|
||||
min = x.Value
|
||||
}
|
||||
}
|
||||
})
|
||||
return min, nil
|
||||
case ast.Set:
|
||||
if a.Len() == 0 {
|
||||
@@ -146,7 +148,7 @@ func builtinMin(a ast.Value) (ast.Value, error) {
|
||||
|
||||
func builtinSort(a ast.Value) (ast.Value, error) {
|
||||
switch a := a.(type) {
|
||||
case ast.Array:
|
||||
case *ast.Array:
|
||||
return a.Sorted(), nil
|
||||
case ast.Set:
|
||||
return a.Sorted(), nil
|
||||
@@ -159,20 +161,24 @@ func builtinAll(a ast.Value) (ast.Value, error) {
|
||||
case ast.Set:
|
||||
res := true
|
||||
match := ast.BooleanTerm(true)
|
||||
val.Foreach(func(term *ast.Term) {
|
||||
if !term.Equal(match) {
|
||||
val.Until(func(term *ast.Term) bool {
|
||||
if !match.Equal(term) {
|
||||
res = false
|
||||
return true
|
||||
}
|
||||
return false
|
||||
})
|
||||
return ast.Boolean(res), nil
|
||||
case ast.Array:
|
||||
case *ast.Array:
|
||||
res := true
|
||||
match := ast.BooleanTerm(true)
|
||||
for _, term := range val {
|
||||
if !term.Equal(match) {
|
||||
val.Until(func(term *ast.Term) bool {
|
||||
if !match.Equal(term) {
|
||||
res = false
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
})
|
||||
return ast.Boolean(res), nil
|
||||
default:
|
||||
return nil, builtins.NewOperandTypeErr(1, a, "array", "set")
|
||||
@@ -182,28 +188,64 @@ func builtinAll(a ast.Value) (ast.Value, error) {
|
||||
func builtinAny(a ast.Value) (ast.Value, error) {
|
||||
switch val := a.(type) {
|
||||
case ast.Set:
|
||||
res := false
|
||||
match := ast.BooleanTerm(true)
|
||||
val.Foreach(func(term *ast.Term) {
|
||||
if term.Equal(match) {
|
||||
res = true
|
||||
}
|
||||
})
|
||||
res := val.Len() > 0 && val.Contains(ast.BooleanTerm(true))
|
||||
return ast.Boolean(res), nil
|
||||
case ast.Array:
|
||||
case *ast.Array:
|
||||
res := false
|
||||
match := ast.BooleanTerm(true)
|
||||
for _, term := range val {
|
||||
if term.Equal(match) {
|
||||
val.Until(func(term *ast.Term) bool {
|
||||
if match.Equal(term) {
|
||||
res = true
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
})
|
||||
return ast.Boolean(res), nil
|
||||
default:
|
||||
return nil, builtins.NewOperandTypeErr(1, a, "array", "set")
|
||||
}
|
||||
}
|
||||
|
||||
func builtinMember(_ BuiltinContext, args []*ast.Term, iter func(*ast.Term) error) error {
|
||||
containee := args[0]
|
||||
switch c := args[1].Value.(type) {
|
||||
case ast.Set:
|
||||
return iter(ast.BooleanTerm(c.Contains(containee)))
|
||||
case *ast.Array:
|
||||
ret := false
|
||||
c.Until(func(v *ast.Term) bool {
|
||||
if v.Value.Compare(containee.Value) == 0 {
|
||||
ret = true
|
||||
}
|
||||
return ret
|
||||
})
|
||||
return iter(ast.BooleanTerm(ret))
|
||||
case ast.Object:
|
||||
ret := false
|
||||
c.Until(func(_, v *ast.Term) bool {
|
||||
if v.Value.Compare(containee.Value) == 0 {
|
||||
ret = true
|
||||
}
|
||||
return ret
|
||||
})
|
||||
return iter(ast.BooleanTerm(ret))
|
||||
}
|
||||
return iter(ast.BooleanTerm(false))
|
||||
}
|
||||
|
||||
func builtinMemberWithKey(_ BuiltinContext, args []*ast.Term, iter func(*ast.Term) error) error {
|
||||
key, val := args[0], args[1]
|
||||
switch c := args[2].Value.(type) {
|
||||
case interface{ Get(*ast.Term) *ast.Term }:
|
||||
ret := false
|
||||
if act := c.Get(key); act != nil {
|
||||
ret = act.Value.Compare(val.Value) == 0
|
||||
}
|
||||
return iter(ast.BooleanTerm(ret))
|
||||
}
|
||||
return iter(ast.BooleanTerm(false))
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterFunctionalBuiltin1(ast.Count.Name, builtinCount)
|
||||
RegisterFunctionalBuiltin1(ast.Sum.Name, builtinSum)
|
||||
@@ -213,4 +255,6 @@ func init() {
|
||||
RegisterFunctionalBuiltin1(ast.Sort.Name, builtinSort)
|
||||
RegisterFunctionalBuiltin1(ast.Any.Name, builtinAny)
|
||||
RegisterFunctionalBuiltin1(ast.All.Name, builtinAll)
|
||||
RegisterBuiltinFunc(ast.Member.Name, builtinMember)
|
||||
RegisterBuiltinFunc(ast.MemberWithKey.Name, builtinMemberWithKey)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user