-
Notifications
You must be signed in to change notification settings - Fork 0
/
component_graph.go
64 lines (60 loc) · 2.16 KB
/
component_graph.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright 2024 HUMAN Security.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package envite
// ComponentGraph represents a graph of components organized in layers.
// Each layer can contain one or more components that can depend on components from the previous layers.
// A layer is represented as a map, mapping from component ID to a component. Layer components are assumed
// to not depend on each other and can be operated on concurrently.
//
// This structure is useful for initializing, starting, and stopping components in the correct order,
// ensuring that dependencies are correctly managed.
type ComponentGraph struct {
components []map[string]Component
}
// NewComponentGraph creates a new instance of ComponentGraph.
// It initializes an empty graph with no components and returns a pointer to it.
// This function is the starting point for building a graph of components by adding layers.
//
// Example:
//
// graph := NewComponentGraph().
// .AddLayer({
// "component-a": componentA,
// })
// .AddLayer({
// "component-b": componentB,
// "component-c": componentC,
// })
//
// This example creates a new component graph and adds two layers to it.
func NewComponentGraph() *ComponentGraph {
return &ComponentGraph{}
}
// AddLayer adds a new layer of components to the ComponentGraph.
// Each call to AddLayer represents a new level in the graph, with the given components being added as a single layer.
// Components within the same layer are considered to have no dependencies on each other,
// but depend on components from all previous layers.
//
// Parameters:
//
// components map[string]Component: A mapping from component ID to a component implementation.
//
// Example:
//
// graph := NewComponentGraph().
// .AddLayer({
// "component-a": componentA,
// })
// .AddLayer({
// "component-b": componentB,
// "component-c": componentC,
// })
//
// This example creates a new component graph and adds two layers to it.
func (c *ComponentGraph) AddLayer(components map[string]Component) *ComponentGraph {
if len(components) > 0 {
c.components = append(c.components, components)
}
return c
}