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

Method EntityToGraph

pkg/development/graph/schema.go:44–118  ·  view source on GitHub ↗

EntityToGraph takes an entity definition and converts it into a graph representation, returning the created graph and an error if any occurs.

(entity *base.EntityDefinition)

Source from the content-addressed store, hash-verified

42// EntityToGraph takes an entity definition and converts it into a graph
43// representation, returning the created graph and an error if any occurs.
44func (b Builder) EntityToGraph(entity *base.EntityDefinition) (g Graph, err error) {
45 // Create a node for the entity
46 enNode := &Node{
47 Type: "entity",
48 ID: entity.GetName(),
49 Label: entity.GetName(),
50 }
51 g.AddNode(enNode)
52
53 // Iterate through the relations in the entity
54 for _, re := range entity.GetRelations() {
55 // Create a node for each relation
56 reNode := &Node{
57 Type: "relation",
58 ID: fmt.Sprintf("%s#%s", entity.GetName(), re.GetName()),
59 Label: re.GetName(),
60 }
61
62 // Iterate through the relation references
63 for _, ref := range re.GetRelationReferences() {
64 if ref.GetRelation() != "" {
65 g.AddEdge(reNode, &Node{
66 Type: "relation",
67 ID: fmt.Sprintf("%s#%s", ref.GetType(), ref.GetRelation()),
68 Label: re.GetName(),
69 })
70 } else {
71 g.AddEdge(reNode, &Node{
72 Type: "entity",
73 ID: ref.GetType(),
74 Label: re.GetName(),
75 })
76 }
77 }
78
79 // Add relation node and edge to the graph
80 g.AddNode(reNode)
81 g.AddEdge(enNode, reNode)
82 }
83
84 // Iterate through the permissions in the entity
85 for _, permission := range entity.GetPermissions() {
86 // Create a node for each permission
87 acNode := &Node{
88 Type: "permission",
89 ID: fmt.Sprintf("%s#%s", entity.GetName(), permission.GetName()),
90 Label: permission.GetName(),
91 }
92 g.AddNode(acNode)
93 g.AddEdge(enNode, acNode)
94 // Build permission graph for each permission
95 ag, err := b.buildPermissionGraph(entity, acNode, []*base.Child{permission.GetChild()})
96 if err != nil {
97 return Graph{}, err
98 }
99 // Add nodes and edges from permission graph to entity graph
100 g.AddNodes(ag.Nodes())
101 g.AddEdges(ag.Edges())

Callers 1

SchemaToGraphMethod · 0.95

Calls 15

buildPermissionGraphMethod · 0.95
AddNodeMethod · 0.80
GetRelationsMethod · 0.80
GetRelationReferencesMethod · 0.80
AddEdgeMethod · 0.80
GetPermissionsMethod · 0.80
GetChildMethod · 0.80
AddNodesMethod · 0.80
NodesMethod · 0.80
AddEdgesMethod · 0.80
EdgesMethod · 0.80
GetNameMethod · 0.65

Tested by

no test coverage detected