validateDependencies checks that all dependency IDs exist and that setting them would not create a circular dependency.
(task *model.Task, depIDs []string, tasks []*model.Task)
| 454 | // validateDependencies checks that all dependency IDs exist and that |
| 455 | // setting them would not create a circular dependency. |
| 456 | func validateDependencies(task *model.Task, depIDs []string, tasks []*model.Task) error { |
| 457 | tasksByID := make(map[string]*model.Task, len(tasks)) |
| 458 | for _, t := range tasks { |
| 459 | tasksByID[t.ID] = t |
| 460 | } |
| 461 | |
| 462 | // Check existence. |
| 463 | for _, id := range depIDs { |
| 464 | if _, ok := tasksByID[id]; !ok { |
| 465 | return fmt.Errorf("dependency %q not found", id) |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | // Self-dependency check. |
| 470 | for _, id := range depIDs { |
| 471 | if id == task.ID { |
| 472 | return fmt.Errorf("task cannot depend on itself: %s", id) |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | // Check for circular dependencies by temporarily setting the new deps. |
| 477 | origDeps := task.Dependencies |
| 478 | task.Dependencies = depIDs |
| 479 | defer func() { task.Dependencies = origDeps }() |
| 480 | |
| 481 | g := graph.NewGraph(tasks) |
| 482 | if cycles := g.DetectCycles(); len(cycles) > 0 { |
| 483 | return fmt.Errorf("circular dependency detected: %s", formatCycle(cycles[0])) |
| 484 | } |
| 485 | |
| 486 | return nil |
| 487 | } |
| 488 | |
| 489 | func formatCycle(cycle []string) string { |
| 490 | return strings.Join(cycle, " -> ") |
no test coverage detected