| Conditions | 6 |
| Total Lines | 60 |
| Code Lines | 39 |
| 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 |
||
| 34 | func EntityToGraph(entity *base.EntityDefinition) (g Graph, err error) { |
||
| 35 | // Create a node for the entity |
||
| 36 | enNode := &Node{ |
||
| 37 | Type: "entity", |
||
| 38 | ID: entity.GetName(), |
||
| 39 | Label: entity.GetName(), |
||
| 40 | } |
||
| 41 | g.AddNode(enNode) |
||
| 42 | |||
| 43 | // Iterate through the relations in the entity |
||
| 44 | for _, re := range entity.GetRelations() { |
||
| 45 | // Create a node for each relation |
||
| 46 | reNode := &Node{ |
||
| 47 | Type: "relation", |
||
| 48 | ID: fmt.Sprintf("%s#%s", entity.GetName(), re.GetName()), |
||
| 49 | Label: re.Name, |
||
| 50 | } |
||
| 51 | |||
| 52 | // Iterate through the relation references |
||
| 53 | for _, ref := range re.GetRelationReferences() { |
||
| 54 | if ref.GetRelation() != "" { |
||
| 55 | g.AddEdge(reNode, &Node{ |
||
| 56 | Type: "relation", |
||
| 57 | ID: fmt.Sprintf("%s#%s", ref.GetType(), ref.GetRelation()), |
||
| 58 | Label: re.Name, |
||
| 59 | }, nil) |
||
| 60 | } else { |
||
| 61 | g.AddEdge(reNode, &Node{ |
||
| 62 | Type: "entity", |
||
| 63 | ID: fmt.Sprintf("%s", ref.GetType()), |
||
| 64 | Label: re.Name, |
||
| 65 | }, nil) |
||
| 66 | } |
||
| 67 | } |
||
| 68 | |||
| 69 | // Add relation node and edge to the graph |
||
| 70 | g.AddNode(reNode) |
||
| 71 | g.AddEdge(enNode, reNode, nil) |
||
| 72 | } |
||
| 73 | |||
| 74 | // Iterate through the permissions in the entity |
||
| 75 | for _, permission := range entity.GetPermissions() { |
||
| 76 | // Create a node for each permission |
||
| 77 | acNode := &Node{ |
||
| 78 | Type: "permission", |
||
| 79 | ID: fmt.Sprintf("%s#%s", entity.GetName(), permission.GetName()), |
||
| 80 | Label: permission.GetName(), |
||
| 81 | } |
||
| 82 | g.AddNode(acNode) |
||
| 83 | g.AddEdge(enNode, acNode, nil) |
||
| 84 | // Build permission graph for each permission |
||
| 85 | ag, err := buildPermissionGraph(entity, acNode, []*base.Child{permission.GetChild()}) |
||
| 86 | if err != nil { |
||
| 87 | return Graph{}, err |
||
| 88 | } |
||
| 89 | // Add nodes and edges from permission graph to entity graph |
||
| 90 | g.AddNodes(ag.Nodes()) |
||
| 91 | g.AddEdges(ag.Edges()) |
||
| 92 | } |
||
| 93 | return |
||
| 94 | } |
||
| 167 |