MCPcopy Create free account
hub / github.com/Permify/permify / Walk

Method Walk

internal/schema/walker.go:27–77  ·  view source on GitHub ↗

Walk traverses the schema based on entity type and permission

(
	entityType string,
	permission string,
)

Source from the content-addressed store, hash-verified

25
26// Walk traverses the schema based on entity type and permission
27func (w *Walker) Walk(
28 entityType string,
29 permission string,
30) error {
31 // Generate a unique key for the entityType and permission combination
32 key := utils.Key(entityType, permission)
33
34 // Check if the entity-permission combination has already been visited
35 if _, ok := w.visited[key]; ok {
36 // If already visited, exit early to avoid redundant processing or infinite recursion
37 return nil
38 }
39
40 // Mark the entity-permission combination as visited
41 w.visited[key] = struct{}{}
42
43 // Lookup the entity definition in the schema
44 def, ok := w.schema.GetEntityDefinitions()[entityType]
45 if !ok {
46 // Error is returned if entity definition is not found
47 return errors.New(base.ErrorCode_ERROR_CODE_ENTITY_DEFINITION_NOT_FOUND.String())
48 }
49
50 // Switch on the type of reference specified by the permission
51 switch def.GetReferences()[permission] {
52 case base.EntityDefinition_REFERENCE_PERMISSION:
53 // If the reference type is a permission, look up the permission
54 permission, ok := def.GetPermissions()[permission]
55 if !ok {
56 // Error is returned if permission is not found
57 return errors.New(base.ErrorCode_ERROR_CODE_PERMISSION_NOT_FOUND.String())
58 }
59 // Check if the permission has a child element
60 child := permission.GetChild()
61 // If the child has a rewrite rule, walk the rewrite rule
62 if child.GetRewrite() != nil {
63 return w.WalkRewrite(entityType, child.GetRewrite())
64 }
65 // Otherwise, walk the leaf node
66 return w.WalkLeaf(entityType, child.GetLeaf())
67 case base.EntityDefinition_REFERENCE_RELATION:
68 // If the reference type is a relation, nothing to do, return nil
69 return nil
70 case base.EntityDefinition_REFERENCE_ATTRIBUTE:
71 // If the reference type is an attribute, not implemented, return error
72 return ErrUnimplemented
73 default:
74 // For any other reference type, not implemented, return error
75 return errors.New(base.ErrorCode_ERROR_CODE_UNDEFINED_CHILD_KIND.String())
76 }
77}
78
79// WalkRewrite is a method that walks through the rewrite part of the schema
80func (w *Walker) WalkRewrite(

Callers 2

WalkComputedUserSetMethod · 0.95
walker_test.goFile · 0.80

Calls 10

WalkRewriteMethod · 0.95
WalkLeafMethod · 0.95
KeyFunction · 0.92
GetEntityDefinitionsMethod · 0.80
GetPermissionsMethod · 0.80
GetChildMethod · 0.80
GetRewriteMethod · 0.80
StringMethod · 0.65
GetReferencesMethod · 0.45
GetLeafMethod · 0.45

Tested by

no test coverage detected