Conditions | 4 |
Total Lines | 57 |
Code Lines | 34 |
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 cache |
||
55 | func (c *CheckEngineWithCache) Check(ctx context.Context, request *base.PermissionCheckRequest) (response *base.PermissionCheckResponse, err error) { |
||
56 | // Retrieve entity definition |
||
57 | var en *base.EntityDefinition |
||
58 | en, _, err = c.schemaReader.ReadEntityDefinition(ctx, request.GetTenantId(), request.GetEntity().GetType(), request.GetMetadata().GetSchemaVersion()) |
||
59 | if err != nil { |
||
60 | return &base.PermissionCheckResponse{ |
||
61 | Can: base.CheckResult_CHECK_RESULT_DENIED, |
||
62 | Metadata: &base.PermissionCheckResponseMetadata{ |
||
63 | CheckCount: 0, |
||
64 | }, |
||
65 | }, err |
||
66 | } |
||
67 | |||
68 | isRelational := engines.IsRelational(en, request.GetPermission()) |
||
69 | |||
70 | // Try to get the cached result for the given request. |
||
71 | res, found := c.getCheckKey(request, isRelational) |
||
72 | |||
73 | // If a cached result is found, handle exclusion and return the result. |
||
74 | if found { |
||
75 | ctx, span := tracer.Start(ctx, "hit") |
||
76 | defer span.End() |
||
77 | start := time.Now() |
||
78 | |||
79 | // Increase the check count in the metrics. |
||
80 | c.cacheCounter.Add(ctx, 1) |
||
81 | |||
82 | duration := time.Now().Sub(start) |
||
83 | c.cacheHitDurationHistogram.Record(ctx, duration.Microseconds()) |
||
84 | |||
85 | // If the request doesn't have the exclusion flag set, return the cached result. |
||
86 | return &base.PermissionCheckResponse{ |
||
87 | Can: res.GetCan(), |
||
88 | Metadata: &base.PermissionCheckResponseMetadata{}, |
||
89 | }, nil |
||
90 | } |
||
91 | |||
92 | // Perform the actual permission check using the provided request. |
||
93 | cres, err := c.checker.Check(ctx, request) |
||
94 | // Check if there's an error or the response is nil, and return the result. |
||
95 | if err != nil { |
||
96 | return &base.PermissionCheckResponse{ |
||
97 | Can: base.CheckResult_CHECK_RESULT_DENIED, |
||
98 | Metadata: &base.PermissionCheckResponseMetadata{ |
||
99 | CheckCount: 0, |
||
100 | }, |
||
101 | }, err |
||
102 | } |
||
103 | |||
104 | // Add to histogram the response |
||
105 | |||
106 | c.setCheckKey(request, &base.PermissionCheckResponse{ |
||
107 | Can: cres.GetCan(), |
||
108 | Metadata: &base.PermissionCheckResponseMetadata{}, |
||
109 | }, isRelational) |
||
110 | // Return the result of the permission check. |
||
111 | return cres, err |
||
112 | } |
||
180 |