Walk traverses the schema based on entity type and permission
( entityType string, permission string, )
| 25 | |
| 26 | // Walk traverses the schema based on entity type and permission |
| 27 | func (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 |
| 80 | func (w *Walker) WalkRewrite( |
no test coverage detected