github.com/rajeev159/opa@v0.45.0/topdown/comparison.go (about)

     1  // Copyright 2016 The OPA Authors.  All rights reserved.
     2  // Use of this source code is governed by an Apache2
     3  // license that can be found in the LICENSE file.
     4  
     5  package topdown
     6  
     7  import "github.com/open-policy-agent/opa/ast"
     8  
     9  type compareFunc func(a, b ast.Value) bool
    10  
    11  func compareGreaterThan(a, b ast.Value) bool {
    12  	return ast.Compare(a, b) > 0
    13  }
    14  
    15  func compareGreaterThanEq(a, b ast.Value) bool {
    16  	return ast.Compare(a, b) >= 0
    17  }
    18  
    19  func compareLessThan(a, b ast.Value) bool {
    20  	return ast.Compare(a, b) < 0
    21  }
    22  
    23  func compareLessThanEq(a, b ast.Value) bool {
    24  	return ast.Compare(a, b) <= 0
    25  }
    26  
    27  func compareNotEq(a, b ast.Value) bool {
    28  	return ast.Compare(a, b) != 0
    29  }
    30  
    31  func compareEq(a, b ast.Value) bool {
    32  	return ast.Compare(a, b) == 0
    33  }
    34  
    35  func builtinCompare(cmp compareFunc) FunctionalBuiltin2 {
    36  	return func(a, b ast.Value) (ast.Value, error) {
    37  		return ast.Boolean(cmp(a, b)), nil
    38  	}
    39  }
    40  
    41  func init() {
    42  	RegisterFunctionalBuiltin2(ast.GreaterThan.Name, builtinCompare(compareGreaterThan))
    43  	RegisterFunctionalBuiltin2(ast.GreaterThanEq.Name, builtinCompare(compareGreaterThanEq))
    44  	RegisterFunctionalBuiltin2(ast.LessThan.Name, builtinCompare(compareLessThan))
    45  	RegisterFunctionalBuiltin2(ast.LessThanEq.Name, builtinCompare(compareLessThanEq))
    46  	RegisterFunctionalBuiltin2(ast.NotEqual.Name, builtinCompare(compareNotEq))
    47  	RegisterFunctionalBuiltin2(ast.Equal.Name, builtinCompare(compareEq))
    48  }