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