-
Notifications
You must be signed in to change notification settings - Fork 316
Implement a typechecking function in schema #2374
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
Open
barakmich
wants to merge
8
commits into
main
Choose a base branch
from
barakmich/typecheck
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+612
−0
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0b027db
[schema] add type-checking test
barakmich d680f05
attach the typechecking to typesystem instead of graph -- it didn't r…
barakmich 2a8f6af
finish tests
barakmich 36faa2f
lints
barakmich b483d25
rename and fix comment
barakmich 11c0119
include alternative check that includes subrelations
barakmich c9df04d
improve coverage to past cover tests
barakmich 4f89cd1
revert helpful change to get coverage up
barakmich File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,144 @@ | ||
package schema | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/authzed/spicedb/pkg/genutil/mapz" | ||
corev1 "github.com/authzed/spicedb/pkg/proto/core/v1" | ||
) | ||
|
||
const ellipsesRelation = "..." | ||
|
||
// GetRecursiveTypesForRelation returns, for a given definition and relation, are the potential | ||
// subject definition names of that relation. | ||
func (ts *TypeSystem) GetRecursiveTypesForRelation(ctx context.Context, defName string, relationName string) ([]string, error) { | ||
seen := mapz.NewSet[string]() | ||
set, err := ts.getTypesForRelationInternal(ctx, defName, relationName, seen, false) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return set.AsSlice(), nil | ||
} | ||
|
||
// GetRecursiveSubtypesForRelation returns, for a given definition and relation, are the potential | ||
// subject definition names of that relation, as well as any relation subtypes (eg, `group#member`) that may occur. | ||
func (ts *TypeSystem) GetRecursiveSubtypesForRelation(ctx context.Context, defName string, relationName string) ([]string, error) { | ||
seen := mapz.NewSet[string]() | ||
set, err := ts.getTypesForRelationInternal(ctx, defName, relationName, seen, true) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return set.AsSlice(), nil | ||
} | ||
|
||
func (ts *TypeSystem) getTypesForRelationInternal(ctx context.Context, defName string, relationName string, seen *mapz.Set[string], addRelations bool) (*mapz.Set[string], error) { | ||
id := fmt.Sprint(defName, "#", relationName) | ||
if seen.Has(id) { | ||
return nil, nil | ||
} | ||
seen.Add(id) | ||
def, err := ts.GetDefinition(ctx, defName) | ||
if err != nil { | ||
return nil, err | ||
} | ||
rel, ok := def.GetRelation(relationName) | ||
if !ok { | ||
return nil, asTypeError(NewRelationNotFoundErr(defName, relationName)) | ||
} | ||
if rel.TypeInformation != nil { | ||
return ts.getTypesForInfo(ctx, defName, rel.TypeInformation, seen, addRelations) | ||
} else if rel.UsersetRewrite != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. drop the else here, since all branches return |
||
return ts.getTypesForRewrite(ctx, defName, rel.UsersetRewrite, seen, addRelations) | ||
} | ||
return nil, asTypeError(NewMissingAllowedRelationsErr(defName, relationName)) | ||
} | ||
|
||
func (ts *TypeSystem) getTypesForInfo(ctx context.Context, defName string, rel *corev1.TypeInformation, seen *mapz.Set[string], addRelations bool) (*mapz.Set[string], error) { | ||
out := mapz.NewSet[string]() | ||
for _, dr := range rel.GetAllowedDirectRelations() { | ||
if dr.GetRelation() == ellipsesRelation { | ||
out.Add(dr.GetNamespace()) | ||
} else if dr.GetRelation() != "" { | ||
if addRelations { | ||
out.Add(fmt.Sprintf("%s#%s", dr.GetNamespace(), dr.GetRelation())) | ||
} | ||
rest, err := ts.getTypesForRelationInternal(ctx, dr.GetNamespace(), dr.GetRelation(), seen, addRelations) | ||
if err != nil { | ||
return nil, err | ||
} | ||
out.Merge(rest) | ||
} else { | ||
// It's a wildcard, so all things of that type count | ||
out.Add(dr.GetNamespace()) | ||
} | ||
} | ||
return out, nil | ||
} | ||
|
||
func (ts *TypeSystem) getTypesForRewrite(ctx context.Context, defName string, rel *corev1.UsersetRewrite, seen *mapz.Set[string], addRelations bool) (*mapz.Set[string], error) { | ||
out := mapz.NewSet[string]() | ||
|
||
// We're finding the union of all the things touched, regardless. | ||
toCheck := []*corev1.SetOperation{rel.GetUnion(), rel.GetIntersection(), rel.GetExclusion()} | ||
|
||
for _, op := range toCheck { | ||
if op == nil { | ||
continue | ||
} | ||
for _, child := range op.GetChild() { | ||
if computed := child.GetComputedUserset(); computed != nil { | ||
set, err := ts.getTypesForRelationInternal(ctx, defName, computed.GetRelation(), seen, addRelations) | ||
if err != nil { | ||
return nil, err | ||
} | ||
out.Merge(set) | ||
} | ||
if rewrite := child.GetUsersetRewrite(); rewrite != nil { | ||
sub, err := ts.getTypesForRewrite(ctx, defName, rewrite, seen, addRelations) | ||
if err != nil { | ||
return nil, err | ||
} | ||
out.Merge(sub) | ||
} | ||
if userset := child.GetTupleToUserset(); userset != nil { | ||
set, err := ts.getTypesForRelationInternal(ctx, defName, userset.GetTupleset().GetRelation(), seen, addRelations) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if set == nil { | ||
// We've already seen it. | ||
continue | ||
} | ||
for _, s := range set.AsSlice() { | ||
targets, err := ts.getTypesForRelationInternal(ctx, s, userset.GetComputedUserset().GetRelation(), seen, addRelations) | ||
if err != nil { | ||
return nil, err | ||
} | ||
out.Merge(targets) | ||
} | ||
} | ||
if functioned := child.GetFunctionedTupleToUserset(); functioned != nil { | ||
set, err := ts.getTypesForRelationInternal(ctx, defName, functioned.GetTupleset().GetRelation(), seen, addRelations) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if set == nil { | ||
// We've already seen it. | ||
continue | ||
} | ||
for _, s := range set.AsSlice() { | ||
targets, err := ts.getTypesForRelationInternal(ctx, s, functioned.GetComputedUserset().GetRelation(), seen, addRelations) | ||
if targets == nil { | ||
continue | ||
} | ||
if err != nil { | ||
return nil, err | ||
} | ||
out.Merge(targets) | ||
} | ||
} | ||
} | ||
} | ||
return out, nil | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
newlines after if statements
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
wsl
linter does this for you 😄 https://github.com/bombsimon/wsl.We should send a separate PR adding it