| Conditions | 9 |
| Total Lines | 55 |
| Code Lines | 36 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | package graph |
||
| 99 | func buildPermissionGraph(entity *base.EntityDefinition, from *Node, children []*base.Child) (g Graph, err error) { |
||
| 100 | // Iterate through the children |
||
| 101 | for _, child := range children { |
||
| 102 | switch child.GetType().(type) { |
||
| 103 | case *base.Child_Rewrite: |
||
| 104 | // Create a node for the rewrite operation |
||
| 105 | rw := &Node{ |
||
| 106 | Type: "operation", |
||
| 107 | ID: xid.New().String(), |
||
| 108 | Label: child.GetRewrite().GetRewriteOperation().String(), |
||
| 109 | } |
||
| 110 | |||
| 111 | // Add the rewrite node to the graph and connect it to the parent node |
||
| 112 | g.AddNode(rw) |
||
| 113 | g.AddEdge(from, rw, nil) |
||
| 114 | // Recursively process the children of the rewrite node |
||
| 115 | ag, err := buildPermissionGraph(entity, rw, child.GetRewrite().GetChildren()) |
||
| 116 | if err != nil { |
||
| 117 | return Graph{}, err |
||
| 118 | } |
||
| 119 | // Add the nodes and edges from the child graph to the current graph |
||
| 120 | g.AddNodes(ag.Nodes()) |
||
| 121 | g.AddEdges(ag.Edges()) |
||
| 122 | case *base.Child_Leaf: |
||
| 123 | // Process the leaf node |
||
| 124 | leaf := child.GetLeaf() |
||
| 125 | |||
| 126 | switch leaf.GetType().(type) { |
||
| 127 | case *base.Leaf_TupleToUserSet: |
||
| 128 | // Find the relation in the entity definition |
||
| 129 | re, err := schema.GetRelationByNameInEntityDefinition(entity, leaf.GetTupleToUserSet().GetTupleSet().GetRelation()) |
||
| 130 | if err != nil { |
||
| 131 | return Graph{}, errors.New(base.ErrorCode_ERROR_CODE_RELATION_DEFINITION_NOT_FOUND.String()) |
||
| 132 | } |
||
| 133 | |||
| 134 | // Add an edge between the parent node and the tuple set relation node |
||
| 135 | g.AddEdge(from, &Node{ |
||
| 136 | Type: "relation", |
||
| 137 | ID: fmt.Sprintf("%s#%s", GetTupleSetReferenceReference(re), leaf.GetTupleToUserSet().GetComputed().GetRelation()), |
||
|
|
|||
| 138 | Label: leaf.GetTupleToUserSet().GetComputed().GetRelation(), |
||
| 139 | }, leaf.GetExclusion()) |
||
| 140 | |||
| 141 | case *base.Leaf_ComputedUserSet: |
||
| 142 | // Add an edge between the parent node and the computed user set relation node |
||
| 143 | g.AddEdge(from, &Node{ |
||
| 144 | Type: "relation", |
||
| 145 | ID: fmt.Sprintf("%s#%s", entity.GetName(), leaf.GetComputedUserSet().GetRelation()), |
||
| 146 | Label: leaf.GetComputedUserSet().GetRelation(), |
||
| 147 | }, leaf.GetExclusion()) |
||
| 148 | default: |
||
| 149 | break |
||
| 150 | } |
||
| 151 | } |
||
| 152 | } |
||
| 153 | return |
||
| 154 | } |
||
| 167 |