Conditions | 38 |
Total Lines | 381 |
Code Lines | 240 |
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 development.*Development.RunWithShape 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 development |
||
141 | func (c *Development) RunWithShape(ctx context.Context, shape *file.Shape) (errors []Error) { |
||
142 | // Parse the schema using the parser library |
||
143 | sch, err := parser.NewParser(shape.Schema).Parse() |
||
144 | if err != nil { |
||
145 | errors = append(errors, Error{ |
||
146 | Type: "schema", |
||
147 | Key: "", |
||
148 | Message: err.Error(), |
||
149 | }) |
||
150 | return |
||
151 | } |
||
152 | |||
153 | // Compile the parsed schema |
||
154 | _, _, err = compiler.NewCompiler(true, sch).Compile() |
||
155 | if err != nil { |
||
156 | errors = append(errors, Error{ |
||
157 | Type: "schema", |
||
158 | Key: "", |
||
159 | Message: err.Error(), |
||
160 | }) |
||
161 | return |
||
162 | } |
||
163 | |||
164 | // Generate a new unique ID for this version of the schema |
||
165 | version := xid.New().String() |
||
166 | |||
167 | // Create a slice of SchemaDefinitions, one for each statement in the schema |
||
168 | cnf := make([]storage.SchemaDefinition, 0, len(sch.Statements)) |
||
169 | for _, st := range sch.Statements { |
||
170 | cnf = append(cnf, storage.SchemaDefinition{ |
||
171 | TenantID: "t1", |
||
172 | Version: version, |
||
173 | Name: st.GetName(), |
||
174 | SerializedDefinition: []byte(st.String()), |
||
175 | }) |
||
176 | } |
||
177 | |||
178 | // Write the schema definitions into the storage |
||
179 | err = c.Container.SW.WriteSchema(ctx, cnf) |
||
180 | if err != nil { |
||
181 | errors = append(errors, Error{ |
||
182 | Type: "schema", |
||
183 | Key: "", |
||
184 | Message: err.Error(), |
||
185 | }) |
||
186 | return |
||
187 | } |
||
188 | |||
189 | // Each item in the Relationships slice is processed individually |
||
190 | for _, t := range shape.Relationships { |
||
191 | tup, err := tuple.Tuple(t) |
||
192 | if err != nil { |
||
193 | errors = append(errors, Error{ |
||
194 | Type: "relationships", |
||
195 | Key: t, |
||
196 | Message: err.Error(), |
||
197 | }) |
||
198 | continue |
||
199 | } |
||
200 | |||
201 | // Read the schema definition for this relationship |
||
202 | definition, _, err := c.Container.SR.ReadEntityDefinition(ctx, "t1", tup.GetEntity().GetType(), version) |
||
203 | if err != nil { |
||
204 | errors = append(errors, Error{ |
||
205 | Type: "relationships", |
||
206 | Key: t, |
||
207 | Message: err.Error(), |
||
208 | }) |
||
209 | continue |
||
210 | } |
||
211 | |||
212 | // Validate the relationship tuple against the schema definition |
||
213 | err = validation.ValidateTuple(definition, tup) |
||
214 | if err != nil { |
||
215 | errors = append(errors, Error{ |
||
216 | Type: "relationships", |
||
217 | Key: t, |
||
218 | Message: err.Error(), |
||
219 | }) |
||
220 | continue |
||
221 | } |
||
222 | |||
223 | // Write the relationship to the database |
||
224 | _, err = c.Container.DW.Write(ctx, "t1", database.NewTupleCollection(tup), database.NewAttributeCollection()) |
||
225 | // Continue to the next relationship if an error occurred |
||
226 | if err != nil { |
||
227 | errors = append(errors, Error{ |
||
228 | Type: "relationships", |
||
229 | Key: t, |
||
230 | Message: err.Error(), |
||
231 | }) |
||
232 | continue |
||
233 | } |
||
234 | } |
||
235 | |||
236 | // Each item in the Attributes slice is processed individually |
||
237 | for _, a := range shape.Attributes { |
||
238 | attr, err := attribute.Attribute(a) |
||
239 | if err != nil { |
||
240 | errors = append(errors, Error{ |
||
241 | Type: "attributes", |
||
242 | Key: a, |
||
243 | Message: err.Error(), |
||
244 | }) |
||
245 | continue |
||
246 | } |
||
247 | |||
248 | // Read the schema definition for this attribute |
||
249 | definition, _, err := c.Container.SR.ReadEntityDefinition(ctx, "t1", attr.GetEntity().GetType(), version) |
||
250 | if err != nil { |
||
251 | errors = append(errors, Error{ |
||
252 | Type: "attributes", |
||
253 | Key: a, |
||
254 | Message: err.Error(), |
||
255 | }) |
||
256 | continue |
||
257 | } |
||
258 | |||
259 | // Validate the attribute against the schema definition |
||
260 | err = validation.ValidateAttribute(definition, attr) |
||
261 | if err != nil { |
||
262 | errors = append(errors, Error{ |
||
263 | Type: "attributes", |
||
264 | Key: a, |
||
265 | Message: err.Error(), |
||
266 | }) |
||
267 | continue |
||
268 | } |
||
269 | |||
270 | // Write the attribute to the database |
||
271 | _, err = c.Container.DW.Write(ctx, "t1", database.NewTupleCollection(), database.NewAttributeCollection(attr)) |
||
272 | // Continue to the next attribute if an error occurred |
||
273 | if err != nil { |
||
274 | errors = append(errors, Error{ |
||
275 | Type: "attributes", |
||
276 | Key: a, |
||
277 | Message: err.Error(), |
||
278 | }) |
||
279 | continue |
||
280 | } |
||
281 | } |
||
282 | |||
283 | // Each item in the Scenarios slice is processed individually |
||
284 | for i, scenario := range shape.Scenarios { |
||
285 | |||
286 | // Each Check in the current scenario is processed |
||
287 | for _, check := range scenario.Checks { |
||
288 | entity, err := tuple.E(check.Entity) |
||
289 | if err != nil { |
||
290 | errors = append(errors, Error{ |
||
291 | Type: "scenarios", |
||
292 | Key: i, |
||
293 | Message: err.Error(), |
||
294 | }) |
||
295 | continue |
||
296 | } |
||
297 | |||
298 | ear, err := tuple.EAR(check.Subject) |
||
299 | if err != nil { |
||
300 | errors = append(errors, Error{ |
||
301 | Type: "scenarios", |
||
302 | Key: i, |
||
303 | Message: err.Error(), |
||
304 | }) |
||
305 | continue |
||
306 | } |
||
307 | |||
308 | cont, err := Context(check.Context) |
||
309 | if err != nil { |
||
310 | errors = append(errors, Error{ |
||
311 | Type: "scenarios", |
||
312 | Key: i, |
||
313 | Message: err.Error(), |
||
314 | }) |
||
315 | continue |
||
316 | } |
||
317 | |||
318 | subject := &v1.Subject{ |
||
319 | Type: ear.GetEntity().GetType(), |
||
320 | Id: ear.GetEntity().GetId(), |
||
321 | Relation: ear.GetRelation(), |
||
322 | } |
||
323 | |||
324 | // Each Assertion in the current check is processed |
||
325 | for permission, expected := range check.Assertions { |
||
326 | exp := v1.CheckResult_CHECK_RESULT_ALLOWED |
||
327 | if !expected { |
||
328 | exp = v1.CheckResult_CHECK_RESULT_DENIED |
||
329 | } |
||
330 | |||
331 | // A Permission Check is made for the current entity, permission and subject |
||
332 | res, err := c.Container.Invoker.Check(ctx, &v1.PermissionCheckRequest{ |
||
333 | TenantId: "t1", |
||
334 | Metadata: &v1.PermissionCheckRequestMetadata{ |
||
335 | SchemaVersion: version, |
||
336 | SnapToken: token.NewNoopToken().Encode().String(), |
||
337 | Depth: 100, |
||
338 | }, |
||
339 | Context: cont, |
||
340 | Entity: entity, |
||
341 | Permission: permission, |
||
342 | Subject: subject, |
||
343 | }) |
||
344 | if err != nil { |
||
345 | errors = append(errors, Error{ |
||
346 | Type: "scenarios", |
||
347 | Key: i, |
||
348 | Message: err.Error(), |
||
349 | }) |
||
350 | continue |
||
351 | } |
||
352 | |||
353 | query := tuple.SubjectToString(subject) + " " + permission + " " + tuple.EntityToString(entity) |
||
354 | |||
355 | // Check if the permission check result matches the expected result |
||
356 | if res.Can != exp { |
||
357 | var expectedStr, actualStr string |
||
358 | if exp == v1.CheckResult_CHECK_RESULT_ALLOWED { |
||
359 | expectedStr = "true" |
||
360 | } else { |
||
361 | expectedStr = "false" |
||
362 | } |
||
363 | |||
364 | if res.Can == v1.CheckResult_CHECK_RESULT_ALLOWED { |
||
365 | actualStr = "true" |
||
366 | } else { |
||
367 | actualStr = "false" |
||
368 | } |
||
369 | |||
370 | // Construct a detailed error message with the expected result, actual result, and the query |
||
371 | errorMsg := fmt.Sprintf("Query: %s, Expected: %s, Actual: %s", query, expectedStr, actualStr) |
||
372 | |||
373 | errors = append(errors, Error{ |
||
374 | Type: "scenarios", |
||
375 | Key: i, |
||
376 | Message: errorMsg, |
||
377 | }) |
||
378 | } |
||
379 | } |
||
380 | } |
||
381 | |||
382 | // Each EntityFilter in the current scenario is processed |
||
383 | for _, filter := range scenario.EntityFilters { |
||
384 | ear, err := tuple.EAR(filter.Subject) |
||
385 | if err != nil { |
||
386 | errors = append(errors, Error{ |
||
387 | Type: "scenarios", |
||
388 | Key: i, |
||
389 | Message: err.Error(), |
||
390 | }) |
||
391 | continue |
||
392 | } |
||
393 | |||
394 | cont, err := Context(filter.Context) |
||
395 | if err != nil { |
||
396 | errors = append(errors, Error{ |
||
397 | Type: "scenarios", |
||
398 | Key: i, |
||
399 | Message: err.Error(), |
||
400 | }) |
||
401 | continue |
||
402 | } |
||
403 | |||
404 | subject := &v1.Subject{ |
||
405 | Type: ear.GetEntity().GetType(), |
||
406 | Id: ear.GetEntity().GetId(), |
||
407 | Relation: ear.GetRelation(), |
||
408 | } |
||
409 | |||
410 | // Each Assertion in the current filter is processed |
||
411 | |||
412 | for permission, expected := range filter.Assertions { |
||
413 | // Perform a lookup for the entity with the given subject and permission |
||
414 | res, err := c.Container.Invoker.LookupEntity(ctx, &v1.PermissionLookupEntityRequest{ |
||
415 | TenantId: "t1", |
||
416 | Metadata: &v1.PermissionLookupEntityRequestMetadata{ |
||
417 | SchemaVersion: version, |
||
418 | SnapToken: token.NewNoopToken().Encode().String(), |
||
419 | Depth: 100, |
||
420 | }, |
||
421 | Context: cont, |
||
422 | EntityType: filter.EntityType, |
||
423 | Permission: permission, |
||
424 | Subject: subject, |
||
425 | }) |
||
426 | if err != nil { |
||
427 | errors = append(errors, Error{ |
||
428 | Type: "scenarios", |
||
429 | Key: i, |
||
430 | Message: err.Error(), |
||
431 | }) |
||
432 | continue |
||
433 | } |
||
434 | |||
435 | query := tuple.SubjectToString(subject) + " " + permission + " " + filter.EntityType |
||
436 | |||
437 | // Check if the actual result of the entity lookup does NOT match the expected result |
||
438 | if !isSameArray(res.GetEntityIds(), expected) { |
||
439 | expectedStr := strings.Join(expected, ", ") |
||
440 | actualStr := strings.Join(res.GetEntityIds(), ", ") |
||
441 | |||
442 | errorMsg := fmt.Sprintf("Query: %s, Expected: [%s], Actual: [%s]", query, expectedStr, actualStr) |
||
443 | |||
444 | errors = append(errors, Error{ |
||
445 | Type: "scenarios", |
||
446 | Key: i, |
||
447 | Message: errorMsg, |
||
448 | }) |
||
449 | } |
||
450 | } |
||
451 | } |
||
452 | |||
453 | // Each SubjectFilter in the current scenario is processed |
||
454 | for _, filter := range scenario.SubjectFilters { |
||
455 | subjectReference := tuple.RelationReference(filter.SubjectReference) |
||
456 | |||
457 | cont, err := Context(filter.Context) |
||
458 | if err != nil { |
||
459 | errors = append(errors, Error{ |
||
460 | Type: "scenarios", |
||
461 | Key: i, |
||
462 | Message: err.Error(), |
||
463 | }) |
||
464 | continue |
||
465 | } |
||
466 | |||
467 | var entity *v1.Entity |
||
468 | entity, err = tuple.E(filter.Entity) |
||
469 | if err != nil { |
||
470 | errors = append(errors, Error{ |
||
471 | Type: "scenarios", |
||
472 | Key: i, |
||
473 | Message: err.Error(), |
||
474 | }) |
||
475 | continue |
||
476 | } |
||
477 | |||
478 | // Each Assertion in the current filter is processed |
||
479 | for permission, expected := range filter.Assertions { |
||
480 | // Perform a lookup for the subject with the given entity and permission |
||
481 | res, err := c.Container.Invoker.LookupSubject(ctx, &v1.PermissionLookupSubjectRequest{ |
||
482 | TenantId: "t1", |
||
483 | Metadata: &v1.PermissionLookupSubjectRequestMetadata{ |
||
484 | SchemaVersion: version, |
||
485 | SnapToken: token.NewNoopToken().Encode().String(), |
||
486 | Depth: 100, |
||
487 | }, |
||
488 | Context: cont, |
||
489 | SubjectReference: subjectReference, |
||
490 | Permission: permission, |
||
491 | Entity: entity, |
||
492 | }) |
||
493 | if err != nil { |
||
494 | errors = append(errors, Error{ |
||
495 | Type: "scenarios", |
||
496 | Key: i, |
||
497 | Message: err.Error(), |
||
498 | }) |
||
499 | continue |
||
500 | } |
||
501 | |||
502 | query := tuple.EntityToString(entity) + " " + permission + " " + filter.SubjectReference |
||
503 | |||
504 | // Check if the actual result of the subject lookup does NOT match the expected result |
||
505 | if !isSameArray(res.GetSubjectIds(), expected) { |
||
506 | expectedStr := strings.Join(expected, ", ") |
||
507 | actualStr := strings.Join(res.GetSubjectIds(), ", ") |
||
508 | |||
509 | errorMsg := fmt.Sprintf("Query: %s, Expected: [%s], Actual: [%s]", query, expectedStr, actualStr) |
||
510 | |||
511 | errors = append(errors, Error{ |
||
512 | Type: "scenarios", |
||
513 | Key: i, |
||
514 | Message: errorMsg, |
||
515 | }) |
||
516 | } |
||
517 | } |
||
518 | } |
||
519 | } |
||
520 | |||
521 | return |
||
522 | } |
||
595 |