mirror of
https://github.com/open-policy-agent/opa.git
synced 2026-08-12 19:32:48 -06:00
511fe48b5b
The `ast.Compare(any, any)` function is a beast better avoided, and the `any` args type mean some AST values (like strings) escape to the heap when boxed. Previous work already ensured it wasn't called too often — this just moves it further along by having all `ast.Value`s do their own comparisons with the help of a new function to easily compare 2 different value types. Also: - topdown: slightly cheaper object.union_n implementation - eval: remove unused expr field on evalNot - eval: rename fmtVarTerm -> fmtVar - term: remove unused termSlice type - builtins: cheaper Builtin.Ref() Signed-off-by: Anders Eknert <anders.eknert@apple.com>
49 lines
1.4 KiB
Go
49 lines
1.4 KiB
Go
// 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 "github.com/open-policy-agent/opa/v1/ast"
|
|
|
|
type compareFunc func(a, b ast.Value) bool
|
|
|
|
func compareGreaterThan(a, b ast.Value) bool {
|
|
return a.Compare(b) > 0
|
|
}
|
|
|
|
func compareGreaterThanEq(a, b ast.Value) bool {
|
|
return a.Compare(b) >= 0
|
|
}
|
|
|
|
func compareLessThan(a, b ast.Value) bool {
|
|
return a.Compare(b) < 0
|
|
}
|
|
|
|
func compareLessThanEq(a, b ast.Value) bool {
|
|
return a.Compare(b) <= 0
|
|
}
|
|
|
|
func compareNotEq(a, b ast.Value) bool {
|
|
return a.Compare(b) != 0
|
|
}
|
|
|
|
func compareEq(a, b ast.Value) bool {
|
|
return a.Compare(b) == 0
|
|
}
|
|
|
|
func builtinCompare(cmp compareFunc) BuiltinFunc {
|
|
return func(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
|
|
return iter(ast.InternedTerm(cmp(operands[0].Value, operands[1].Value)))
|
|
}
|
|
}
|
|
|
|
func init() {
|
|
RegisterBuiltinFunc(ast.GreaterThan.Name, builtinCompare(compareGreaterThan))
|
|
RegisterBuiltinFunc(ast.GreaterThanEq.Name, builtinCompare(compareGreaterThanEq))
|
|
RegisterBuiltinFunc(ast.LessThan.Name, builtinCompare(compareLessThan))
|
|
RegisterBuiltinFunc(ast.LessThanEq.Name, builtinCompare(compareLessThanEq))
|
|
RegisterBuiltinFunc(ast.NotEqual.Name, builtinCompare(compareNotEq))
|
|
RegisterBuiltinFunc(ast.Equal.Name, builtinCompare(compareEq))
|
|
}
|