Conditions | 6 |
Total Lines | 69 |
Code Lines | 46 |
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 engines |
||
25 | func (filter *MassSubjectFilter) SubjectFilter( |
||
26 | ctx context.Context, // The context in which the method is executed. |
||
27 | request *base.PermissionLookupSubjectRequest, // The request containing the subject to look up. |
||
28 | publisher *BulkSubjectPublisher, // The publisher to publish the filtered subjects to. |
||
29 | ) (err error) { |
||
30 | // Make an empty set to avoid duplicates. |
||
31 | contextIds := make(map[string]struct{}) |
||
32 | |||
33 | // Query relationships based on the given filter criteria. |
||
34 | tit, err := storageContext.NewContextualTuples(request.GetContext().GetTuples()...).QueryRelationships(&base.TupleFilter{ |
||
35 | Subject: &base.SubjectFilter{ |
||
36 | Type: request.GetSubjectReference().GetType(), |
||
37 | Relation: request.GetSubjectReference().GetRelation(), |
||
38 | }, |
||
39 | }, database.NewCursorPagination(database.Cursor(request.GetContinuousToken()), database.Sort("subject_id"))) |
||
40 | // Return any error encountered during the query. |
||
41 | if err != nil { |
||
42 | return err |
||
43 | } |
||
44 | |||
45 | // Add each unique subject ID to the set. |
||
46 | for tit.HasNext() { |
||
47 | tuple := tit.GetNext() |
||
48 | contextIds[tuple.GetSubject().GetId()] = struct{}{} |
||
49 | } |
||
50 | |||
51 | // Publish each subject in the set. |
||
52 | for id := range contextIds { |
||
53 | publisher.Publish(&base.Subject{ |
||
54 | Type: request.GetSubjectReference().GetType(), |
||
55 | Id: id, |
||
56 | Relation: request.GetSubjectReference().GetRelation(), |
||
57 | }, &base.PermissionCheckRequestMetadata{ |
||
58 | SnapToken: request.GetMetadata().GetSnapToken(), |
||
59 | SchemaVersion: request.GetMetadata().GetSchemaVersion(), |
||
60 | Depth: request.GetMetadata().GetDepth(), |
||
61 | }, request.GetContext(), base.CheckResult_CHECK_RESULT_UNSPECIFIED) |
||
62 | } |
||
63 | |||
64 | // Prepare the initial pagination object. |
||
65 | pagination := database.NewPagination(database.Token(request.GetContinuousToken())) |
||
66 | |||
67 | ids, _, err := filter.dataReader.QueryUniqueSubjectReferences( |
||
68 | ctx, |
||
69 | request.GetTenantId(), |
||
70 | request.GetSubjectReference(), |
||
71 | request.GetMetadata().GetSnapToken(), |
||
72 | pagination, |
||
73 | ) |
||
74 | // Return any error encountered during the query. |
||
75 | if err != nil { |
||
76 | return err |
||
77 | } |
||
78 | |||
79 | // Publish each subject retrieved. |
||
80 | for _, id := range ids { |
||
81 | publisher.Publish(&base.Subject{ |
||
82 | Type: request.GetSubjectReference().GetType(), |
||
83 | Id: id, |
||
84 | Relation: request.GetSubjectReference().GetRelation(), |
||
85 | }, &base.PermissionCheckRequestMetadata{ |
||
86 | SnapToken: request.GetMetadata().GetSnapToken(), |
||
87 | SchemaVersion: request.GetMetadata().GetSchemaVersion(), |
||
88 | Depth: request.GetMetadata().GetDepth(), |
||
89 | }, request.GetContext(), base.CheckResult_CHECK_RESULT_UNSPECIFIED) |
||
90 | } |
||
91 | |||
92 | // Return all IDs retrieved. |
||
93 | return nil |
||
94 | } |
||
95 |