AddEdge adds a relationship of dependency between vertices `source` and `destination`
(source string, destination string)
| 307 | |
| 308 | // AddEdge adds a relationship of dependency between vertices `source` and `destination` |
| 309 | func (g *Graph) AddEdge(source string, destination string) error { |
| 310 | g.lock.Lock() |
| 311 | defer g.lock.Unlock() |
| 312 | |
| 313 | sourceVertex := g.Vertices[source] |
| 314 | destinationVertex := g.Vertices[destination] |
| 315 | |
| 316 | if sourceVertex == nil { |
| 317 | return fmt.Errorf("could not find %s: %w", source, api.ErrNotFound) |
| 318 | } |
| 319 | if destinationVertex == nil { |
| 320 | return fmt.Errorf("could not find %s: %w", destination, api.ErrNotFound) |
| 321 | } |
| 322 | |
| 323 | // If they are already connected |
| 324 | if _, ok := sourceVertex.Children[destination]; ok { |
| 325 | return nil |
| 326 | } |
| 327 | |
| 328 | sourceVertex.Children[destination] = destinationVertex |
| 329 | destinationVertex.Parents[source] = sourceVertex |
| 330 | |
| 331 | return nil |
| 332 | } |
| 333 | |
| 334 | // Leaves returns the slice of leaves of the graph |
| 335 | func (g *Graph) Leaves() []*Vertex { |