-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtopology.go
More file actions
executable file
·76 lines (60 loc) · 1.38 KB
/
Copy pathtopology.go
File metadata and controls
executable file
·76 lines (60 loc) · 1.38 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
package streams
import "github.com/katallaxie/pkg/slices"
var _ Node = (*node)(nil)
// Node is a node in a topology.
type Node interface {
// AddChildren adds children to a node.
AddChild(nodes ...Node)
// Children returns the children of a node.
Children() []Node
// Name returns the name of a node.
Name(names ...string) string
}
type node struct {
id int64
name string
children []Node
}
// NewNode is a constructor for a new node in the topology.
func NewNode(name string) Node {
n := new(node)
n.name = name
return n
}
// ID return the ID of a node.
func (n *node) ID() int64 {
return n.id
}
// AddChild adds a child to a node.
func (n *node) AddChild(nodes ...Node) {
n.children = append(n.children, nodes...)
}
// Children returns the children of a node.
func (n *node) Children() []Node {
return n.children
}
// Name returns the name of a node.
func (n *node) Name(name ...string) string {
if slices.GreaterThen(0, name...) {
n.name = slices.First(name...)
}
return n.name
}
// Topology is a graph of nodes.
type Topology interface {
// Root returns the root node of a topology.
Root() Node
}
type topology struct {
root Node
}
// NewTopology is a constructor for Topology.
func NewTopology(root Node) Topology {
t := new(topology)
t.root = root
return t
}
// Root returns the root node of a topology.
func (t *topology) Root() Node {
return t.root
}