forked from open-policy-agent/opa
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Filled out the casting section of builtin functions. I think the to_set
and to_array functions require no justification. For the others: I realized this would be useful to have when I was doing http calls that returned a types.A and thus when I parsed my code on occasion it only threw type errors at runtime when I attempted to use the result in another builtin. It would be great to have some way to throw these type errors at compile time as well if the user likes, hence these functions. Changed names to cast_, added test, added copyright banner, added docs. Signed-off-by: Varun Mathur <[email protected]> Signed-off-by: Varun Mathur <[email protected]> Signed-off-by: Varun Mathur <[email protected]>
- Loading branch information
Showing
4 changed files
with
227 additions
and
1 deletion.
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
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,85 @@ | ||
// Copyright 2018 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 ( | ||
"fmt" | ||
"github.com/open-policy-agent/opa/ast" | ||
"testing" | ||
) | ||
|
||
func TestToArray(t *testing.T) { | ||
|
||
// expected result | ||
expectedResult := []interface{}{1, 2, 3} | ||
resultObj, err := ast.InterfaceToValue(expectedResult) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
typeErr := fmt.Errorf("type") | ||
|
||
tests := []struct { | ||
note string | ||
rules []string | ||
expected interface{} | ||
}{ | ||
{"array input", []string{`p = x { cast_array([1,2,3], x) }`}, resultObj.String()}, | ||
{"set input", []string{`p = x { cast_array({1,2,3}, x) }`}, resultObj.String()}, | ||
{"bad type", []string{`p = x { cast_array("hello", x) }`}, typeErr}, | ||
} | ||
|
||
data := loadSmallTestData() | ||
|
||
for _, tc := range tests { | ||
runTopDownTestCase(t, data, tc.note, tc.rules, tc.expected) | ||
} | ||
} | ||
|
||
func TestToSet(t *testing.T) { | ||
|
||
typeErr := fmt.Errorf("type") | ||
|
||
tests := []struct { | ||
note string | ||
rules []string | ||
expected interface{} | ||
}{ | ||
{"array input", []string{`p = x { cast_set([1,1,1], x) }`}, "[1]"}, | ||
{"set input", []string{`p = x { cast_set({1,1,2,3}, x) }`}, "[1,2,3]"}, | ||
{"bad type", []string{`p = x { cast_set("hello", x) }`}, typeErr}, | ||
} | ||
|
||
data := loadSmallTestData() | ||
|
||
for _, tc := range tests { | ||
runTopDownTestCase(t, data, tc.note, tc.rules, tc.expected) | ||
} | ||
} | ||
|
||
func TestCasts(t *testing.T) { | ||
typeErr := fmt.Errorf("type") | ||
|
||
tests := []struct { | ||
note string | ||
rules []string | ||
expected interface{} | ||
}{ | ||
{"null valid", []string{`p = x { cast_null(null, x) }`}, "null"}, | ||
{"null invalid", []string{`p = x { cast_null({}, x) }`}, typeErr}, | ||
//{"string valid", []string{`p = x { cast_string("potato", x) }`}, "potato"}, | ||
{"string invalid", []string{`p = x { cast_string({1,1,2,3}, x) }`}, typeErr}, | ||
{"boolean valid", []string{`p = x { cast_boolean(false, x) }`}, "false"}, | ||
{"boolean valid", []string{`p = x { cast_boolean(1, x) }`}, typeErr}, | ||
{"obj valid", []string{`p = x { cast_object({}, x) }`}, "{}"}, | ||
{"obj invalid", []string{`p = x { cast_object([1,2,3], x) }`}, typeErr}, | ||
} | ||
|
||
data := loadSmallTestData() | ||
|
||
for _, tc := range tests { | ||
runTopDownTestCase(t, data, tc.note, tc.rules, tc.expected) | ||
} | ||
} |