-
Notifications
You must be signed in to change notification settings - Fork 344
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
fix: adding functionality to handle cycles in expand and check engine #623
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
12c2eba
fix: adding functionality to handle cycles in expand and check engine
aravindarc 4e9e8b2
fix: adding functionality to handle cycles in expand and check engine
aravindarc ea171aa
fix: adding functionality to handle cycles in expand and check engine
aravindarc 4ac5ccc
fix: adding functionality to handle cycles in expand and check engine
aravindarc 8c810d2
Merge branch 'master' into 411-add-cycle-handling
zepatrik 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 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
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,35 @@ | ||
package graph | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/ory/keto/internal/relationtuple" | ||
) | ||
|
||
type contextKey string | ||
|
||
const visitedMapKey = contextKey("visitedMap") | ||
|
||
func CheckAndAddVisited(ctx context.Context, current relationtuple.Subject) (context.Context, bool) { | ||
visitedMap, ok := ctx.Value(visitedMapKey).(map[string]struct{}) | ||
if !ok { | ||
// for the first time initialize the map | ||
visitedMap = make(map[string]struct{}) | ||
visitedMap[current.String()] = struct{}{} | ||
return context.WithValue(ctx, visitedMapKey, visitedMap), false | ||
} | ||
|
||
// check if current node was already visited | ||
if _, ok := visitedMap[current.String()]; ok { | ||
return ctx, true | ||
} | ||
|
||
// set current entry to visited | ||
visitedMap[current.String()] = struct{}{} | ||
|
||
return context.WithValue( | ||
ctx, | ||
visitedMapKey, | ||
visitedMap, | ||
), false | ||
} |
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,84 @@ | ||
package graph | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/ory/keto/internal/relationtuple" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestEngineUtilsProvider_CheckVisited(t *testing.T) { | ||
t.Run("case=finds cycle", func(t *testing.T) { | ||
|
||
linkedList := []relationtuple.SubjectSet{{ | ||
Namespace: "default", | ||
Object: "A", | ||
Relation: "connected", | ||
}, { | ||
Namespace: "default", | ||
Object: "B", | ||
Relation: "connected", | ||
}, { | ||
Namespace: "default", | ||
Object: "C", | ||
Relation: "connected", | ||
}, { | ||
Namespace: "default", | ||
Object: "B", | ||
Relation: "connected", | ||
}, { | ||
Namespace: "default", | ||
Object: "D", | ||
Relation: "connected", | ||
}} | ||
|
||
ctx := context.Background() | ||
var isThereACycle bool | ||
for i := range linkedList { | ||
ctx, isThereACycle = CheckAndAddVisited(ctx, &linkedList[i]) | ||
if isThereACycle { | ||
break | ||
} | ||
} | ||
|
||
assert.Equal(t, isThereACycle, true) | ||
}) | ||
|
||
t.Run("case=ignores if no cycle", func(t *testing.T) { | ||
|
||
list := []relationtuple.SubjectSet{{ | ||
Namespace: "default", | ||
Object: "A", | ||
Relation: "connected", | ||
}, { | ||
Namespace: "default", | ||
Object: "B", | ||
Relation: "connected", | ||
}, { | ||
Namespace: "default", | ||
Object: "C", | ||
Relation: "connected", | ||
}, { | ||
Namespace: "default", | ||
Object: "D", | ||
Relation: "connected", | ||
}, { | ||
Namespace: "default", | ||
Object: "E", | ||
Relation: "connected", | ||
}} | ||
|
||
ctx := context.Background() | ||
var isThereACycle bool | ||
for i := range list { | ||
ctx, isThereACycle = CheckAndAddVisited(ctx, &list[i]) | ||
if isThereACycle { | ||
break | ||
} | ||
} | ||
|
||
assert.Equal(t, isThereACycle, false) | ||
}) | ||
} |
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.
I don't know if that was on purpose, but I like that we have can see the circle still in the tree. It makes it possible for the client to see circular references as well.
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.
yes, it was on purpose, or it will be confusing for the client and might be flagged as an unexpected behaviour.