-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
topdown: Add new numbers.range built-in function
This commit adds a new built-in function to generate a range of integers between two values (inclusive). This is useful in certain cases where users need to enumerate a set of values (e.g., port numbers). Fixes #2479 Signed-off-by: Torin Sandall <torinsandall@gmail.com>
- Loading branch information
1 parent
338583c
commit 616d947
Showing
4 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright 2020 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 ( | ||
"math/big" | ||
|
||
"github.com/open-policy-agent/opa/ast" | ||
"github.com/open-policy-agent/opa/topdown/builtins" | ||
) | ||
|
||
var one = big.NewInt(1) | ||
|
||
func builtinNumbersRange(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error { | ||
|
||
x, err := builtins.BigIntOperand(operands[0].Value, 1) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
y, err := builtins.BigIntOperand(operands[1].Value, 2) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var result ast.Array | ||
cmp := x.Cmp(y) | ||
|
||
if cmp <= 0 { | ||
for i := new(big.Int).Set(x); i.Cmp(y) <= 0; i = i.Add(i, one) { | ||
result = append(result, ast.NewTerm(builtins.IntToNumber(i))) | ||
} | ||
} else { | ||
for i := new(big.Int).Set(x); i.Cmp(y) >= 0; i = i.Sub(i, one) { | ||
result = append(result, ast.NewTerm(builtins.IntToNumber(i))) | ||
} | ||
} | ||
|
||
return iter(ast.NewTerm(result)) | ||
} | ||
|
||
func init() { | ||
RegisterBuiltinFunc(ast.NumbersRange.Name, builtinNumbersRange) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright 2020 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 ( | ||
"testing" | ||
) | ||
|
||
func TestBuiltinNumbersRange(t *testing.T) { | ||
cases := []struct { | ||
note string | ||
stmt string | ||
exp interface{} | ||
}{ | ||
{ | ||
note: "one", | ||
stmt: "p = x { x := numbers.range(0, 0) }", | ||
exp: "[0]", | ||
}, | ||
{ | ||
note: "ascending", | ||
stmt: "p = x { x := numbers.range(-2, 3) }", | ||
exp: "[-2, -1, 0, 1, 2, 3]", | ||
}, | ||
{ | ||
note: "descending", | ||
stmt: "p = x { x := numbers.range(2, -3) }", | ||
exp: "[2, 1, 0, -1, -2, -3]", | ||
}, | ||
{ | ||
note: "precision", | ||
stmt: "p { numbers.range(49649733057, 49649733060, [49649733057, 49649733058, 49649733059, 49649733060]) }", | ||
exp: "true", | ||
}, | ||
{ | ||
note: "error: floating-point number pos 1", | ||
stmt: "p { numbers.range(3.14, 4) }", | ||
exp: &Error{Code: TypeErr, Message: "numbers.range: operand 1 must be integer number but got floating-point number"}, | ||
}, | ||
{ | ||
note: "error: floating-point number pos 2", | ||
stmt: "p { numbers.range(3, 3.14) }", | ||
exp: &Error{Code: TypeErr, Message: "numbers.range: operand 2 must be integer number but got floating-point number"}, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
runTopDownTestCase(t, map[string]interface{}{}, tc.note, []string{tc.stmt}, tc.exp) | ||
} | ||
} |