Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use generics for ADT #6705

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 6 additions & 7 deletions ast/check.go
Expand Up @@ -10,7 +10,6 @@ import (
"strings"

"github.com/open-policy-agent/opa/types"
"github.com/open-policy-agent/opa/util"
)

type varRewriter func(Ref) Ref
Expand Down Expand Up @@ -156,10 +155,10 @@ func (tc *typeChecker) CheckBody(env *TypeEnv, body Body) (*TypeEnv, Errors) {
// CheckTypes runs type checking on the rules returns a TypeEnv if no errors
// are found. The resulting TypeEnv wraps the provided one. The resulting
// TypeEnv will be able to resolve types of refs that refer to rules.
func (tc *typeChecker) CheckTypes(env *TypeEnv, sorted []util.T, as *AnnotationSet) (*TypeEnv, Errors) {
func (tc *typeChecker) CheckTypes(env *TypeEnv, sorted []*Rule, as *AnnotationSet) (*TypeEnv, Errors) {
env = tc.newEnv(env)
for _, s := range sorted {
tc.checkRule(env, as, s.(*Rule))
tc.checkRule(env, as, s)
}
tc.errs.Sort()
return env, tc.errs
Expand Down Expand Up @@ -744,8 +743,8 @@ func (rc *refChecker) checkRef(curr *TypeEnv, node *typeTreeNode, ref Ref, idx i

case RootDocumentNames.Contains(ref[0]):
if idx != 0 {
node.Children().Iter(func(_, child util.T) bool {
_ = rc.checkRef(curr, child.(*typeTreeNode), ref, idx+1) // ignore error
node.Children().Iter(func(_ Value, child *typeTreeNode) bool {
_ = rc.checkRef(curr, child, ref, idx+1) // ignore error
return false
})
return nil
Expand Down Expand Up @@ -1090,8 +1089,8 @@ func newArgError(loc *Location, builtinName Ref, msg string, have []types.Type,
}

func getOneOfForNode(node *typeTreeNode) (result []Value) {
node.Children().Iter(func(k, _ util.T) bool {
result = append(result, k.(Value))
node.Children().Iter(func(k Value, v *typeTreeNode) bool {
result = append(result, k)
return false
})

Expand Down
14 changes: 7 additions & 7 deletions ast/check_test.go
Expand Up @@ -661,7 +661,7 @@ func TestCheckInferenceRules(t *testing.T) {

for _, tc := range tests {
t.Run(tc.note, func(t *testing.T) {
var elems []util.T
var elems []*Rule

// Convert test rules into rule slice for call.
for i := range tc.rules {
Expand Down Expand Up @@ -732,7 +732,7 @@ func TestCheckInferenceOverlapWithRules(t *testing.T) {

for _, tc := range tests {
t.Run(tc.note, func(t *testing.T) {
var elems []util.T
var elems []*Rule

// Convert test rules into rule slice for "warmup" call.
for i := range tc.rules {
Expand Down Expand Up @@ -1325,7 +1325,7 @@ func TestFunctionTypeInferenceUnappliedWithObjectVarKey(t *testing.T) {
f(x) = y { y = {x: 1} }
`)

elems := []util.T{
elems := []*Rule{
module.Rules[0],
}

Expand Down Expand Up @@ -1513,8 +1513,8 @@ func TestCheckErrorOrdering(t *testing.T) {
p { data.test.q = 2 } # type error: bool = number
`)

input := make([]util.T, len(mod.Rules))
inputReversed := make([]util.T, len(mod.Rules))
input := make([]*Rule, len(mod.Rules))
inputReversed := make([]*Rule, len(mod.Rules))

for i := range input {
input[i] = mod.Rules[i]
Expand Down Expand Up @@ -1562,7 +1562,7 @@ func newTestEnv(rs []string) *TypeEnv {

// We preallocate enough for at least the base rules.
// Else cases will cause reallocs, but that's okay.
elems := make([]util.T, 0, len(rs))
elems := make([]*Rule, 0, len(rs))

for i := range rs {
rule := MustParseRule(rs[i])
Expand Down Expand Up @@ -2305,7 +2305,7 @@ p { input = "foo" }`}},
for _, tc := range tests {
t.Run(tc.note, func(t *testing.T) {
var modules []*Module
var elems []util.T
var elems []*Rule

for i, module := range tc.modules {
mod, err := ParseModuleWithOpts(fmt.Sprintf("test%d.rego", i+1), module, ParserOptions{
Expand Down