Conditions | 16 |
Total Lines | 97 |
Code Lines | 59 |
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:
Complex classes like graph.Builder.buildPermissionGraph often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | package graph |
||
140 | func (b Builder) buildPermissionGraph(entity *base.EntityDefinition, from *Node, children []*base.Child) (g Graph, err error) { |
||
141 | // Iterate through the list of children |
||
142 | for _, child := range children { |
||
143 | switch child.GetType().(type) { |
||
144 | // Handle Rewrite type children |
||
145 | case *base.Child_Rewrite: |
||
146 | rw := &Node{ |
||
147 | Type: "operation", |
||
148 | ID: xid.New().String(), |
||
149 | Label: child.GetRewrite().GetRewriteOperation().String(), |
||
150 | } |
||
151 | |||
152 | // Add the Rewrite node to the graph |
||
153 | g.AddNode(rw) |
||
154 | |||
155 | // Connect the Rewrite node to the current 'from' node |
||
156 | g.AddEdge(from, rw) |
||
157 | |||
158 | // Recursively build the permission graph for Rewrite children |
||
159 | ag, err := b.buildPermissionGraph(entity, rw, child.GetRewrite().GetChildren()) |
||
160 | if err != nil { |
||
161 | return Graph{}, err |
||
162 | } |
||
163 | |||
164 | // Add the nodes and edges from the recursively built graph to the current graph |
||
165 | g.AddNodes(ag.Nodes()) |
||
166 | g.AddEdges(ag.Edges()) |
||
167 | |||
168 | // Handle Leaf type children |
||
169 | case *base.Child_Leaf: |
||
170 | leaf := child.GetLeaf() |
||
171 | |||
172 | switch leaf.GetType().(type) { |
||
173 | // Handle TupleToUserSet type leaf |
||
174 | case *base.Leaf_TupleToUserSet: |
||
175 | re, err := schema.GetRelationByNameInEntityDefinition(entity, leaf.GetTupleToUserSet().GetTupleSet().GetRelation()) |
||
176 | if err != nil { |
||
177 | return Graph{}, errors.New(base.ErrorCode_ERROR_CODE_RELATION_DEFINITION_NOT_FOUND.String()) |
||
178 | } |
||
179 | |||
180 | // Add edges from the current 'from' node to referenced relations |
||
181 | for _, r := range re.GetRelationReferences() { |
||
182 | ag, err := b.addEdgeFromRelation(from, r, leaf) |
||
183 | if err != nil { |
||
184 | return Graph{}, err |
||
185 | } |
||
186 | g.AddNodes(ag.Nodes()) |
||
187 | g.AddEdges(ag.Edges()) |
||
188 | } |
||
189 | |||
190 | // Handle ComputedUserSet type leaf |
||
191 | case *base.Leaf_ComputedUserSet: |
||
192 | g.AddEdge(from, &Node{ |
||
193 | Type: "relation", |
||
194 | ID: fmt.Sprintf("%s#%s", entity.GetName(), leaf.GetComputedUserSet().GetRelation()), |
||
195 | Label: leaf.GetComputedUserSet().GetRelation(), |
||
196 | }) |
||
197 | |||
198 | // Handle ComputedAttribute type leaf |
||
199 | case *base.Leaf_ComputedAttribute: |
||
200 | g.AddEdge(from, &Node{ |
||
201 | Type: "attribute", |
||
202 | ID: fmt.Sprintf("%s$%s", entity.GetName(), leaf.GetComputedAttribute().GetName()), |
||
203 | Label: leaf.GetComputedAttribute().GetName(), |
||
204 | }) |
||
205 | |||
206 | // Handle Call type leaf |
||
207 | case *base.Leaf_Call: |
||
208 | g.AddEdge(from, &Node{ |
||
209 | Type: "rule", |
||
210 | ID: leaf.GetCall().GetRuleName(), |
||
211 | Label: leaf.GetCall().GetRuleName(), |
||
212 | }) |
||
213 | |||
214 | // Add edges for arguments of Call type leaf |
||
215 | for _, arg := range leaf.GetCall().GetArguments() { |
||
216 | switch op := arg.GetType().(type) { |
||
217 | case *base.Argument_ComputedAttribute: |
||
218 | g.AddEdge(&Node{ |
||
219 | Type: "attribute", |
||
220 | ID: fmt.Sprintf("%s$%s", entity.GetName(), op.ComputedAttribute.GetName()), |
||
221 | Label: op.ComputedAttribute.GetName(), |
||
222 | }, &Node{ |
||
223 | Type: "rule", |
||
224 | ID: leaf.GetCall().GetRuleName(), |
||
225 | Label: leaf.GetCall().GetRuleName(), |
||
226 | }) |
||
227 | default: |
||
228 | break |
||
229 | } |
||
230 | } |
||
231 | default: |
||
232 | break |
||
233 | } |
||
234 | } |
||
235 | } |
||
236 | return |
||
237 | } |
||
268 |