forked from open-policy-agent/opa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ineq.go
51 lines (42 loc) · 1.09 KB
/
ineq.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
// 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/ast"
"github.com/open-policy-agent/opa/util"
)
type compareFunc func(a, b interface{}) bool
func compareGreaterThan(a, b interface{}) bool {
return util.Compare(a, b) > 0
}
func compareGreaterThanEq(a, b interface{}) bool {
return util.Compare(a, b) >= 0
}
func compareLessThan(a, b interface{}) bool {
return util.Compare(a, b) < 0
}
func compareLessThanEq(a, b interface{}) bool {
return util.Compare(a, b) <= 0
}
func compareNotEq(a, b interface{}) bool {
return util.Compare(a, b) != 0
}
func evalIneq(cmp compareFunc) BuiltinFunc {
return func(ctx *Context, expr *ast.Expr, iter Iterator) error {
ops := expr.Terms.([]*ast.Term)
a, b := ops[1].Value, ops[2].Value
av, err := ValueToInterface(a, ctx)
if err != nil {
return err
}
bv, err := ValueToInterface(b, ctx)
if err != nil {
return err
}
if cmp(av, bv) {
return iter(ctx)
}
return nil
}
}