@@ -22,17 +22,21 @@ type Node interface {
22
22
// Type returns the type of the registry element a Node refers to
23
23
Type () Type
24
24
// AncestorNames returns a set of strings containing the names of all of the node's ancestors
25
- AncestorNames () sets. String
25
+ Ancestors () [] Node
26
26
// DescendantNames returns a set of strings containing the names of all of the node's descendants
27
- DescendantNames () sets. String
27
+ Descendants () [] Node
28
28
// ParentNames returns a set of strings containing the names of all the node's parents
29
- ParentNames () sets. String
29
+ Parents () [] Node
30
30
// ChildrenNames returns a set of strings containing the names of all the node's children
31
- ChildrenNames () sets. String
31
+ Childrens () [] Node
32
32
}
33
33
34
34
// NodeByName provides a mapping from node name to the Node interface
35
- type NodeByName map [string ]Node
35
+ type NodeByName struct {
36
+ References map [string ]Node
37
+ Chains map [string ]Node
38
+ Workflows map [string ]Node
39
+ }
36
40
37
41
type nodeWithName struct {
38
42
name string
@@ -107,51 +111,51 @@ func (*referenceNode) Type() Type {
107
111
return Reference
108
112
}
109
113
110
- func (n * nodeWithParents ) ParentNames () sets. String {
111
- parents := sets . NewString ()
114
+ func (n * nodeWithParents ) Parents () [] Node {
115
+ var parents [] Node
112
116
for parent := range n .workflowParents {
113
- parents . Insert ( parent . Name () )
117
+ parents = append ( parents , parent )
114
118
}
115
119
for parent := range n .chainParents {
116
- parents . Insert ( parent . Name () )
120
+ parents = append ( parents , parent )
117
121
}
118
122
return parents
119
123
}
120
124
121
- func (* workflowNode ) ParentNames () sets. String { return sets . NewString () }
125
+ func (* workflowNode ) Parents () [] Node { return [] Node {} }
122
126
123
- func (n * nodeWithChildren ) ChildrenNames () sets. String {
124
- children := sets . NewString ()
127
+ func (n * nodeWithChildren ) Childrens () [] Node {
128
+ var children [] Node
125
129
for child := range n .referenceChildren {
126
- children . Insert ( child . Name () )
130
+ children = append ( children , child )
127
131
}
128
132
for child := range n .chainChildren {
129
- children . Insert ( child . Name () )
133
+ children = append ( children , child )
130
134
}
131
135
return children
132
136
}
133
137
134
- func (* referenceNode ) ChildrenNames () sets. String { return sets . NewString () }
138
+ func (* referenceNode ) Childrens () [] Node { return [] Node {} }
135
139
136
- func (n * nodeWithParents ) AncestorNames () sets. String {
137
- ancestors := n .ParentNames ()
140
+ func (n * nodeWithParents ) Ancestors () [] Node {
141
+ ancestors := n .Parents ()
138
142
for parent := range n .chainParents {
139
- ancestors . Insert ( parent .AncestorNames (). List ()... )
143
+ ancestors = append ( ancestors , parent .Ancestors ()... )
140
144
}
141
145
return ancestors
142
146
}
143
147
144
- func (* workflowNode ) AncestorNames () sets. String { return sets . NewString () }
148
+ func (* workflowNode ) Ancestors () [] Node { return [] Node {} }
145
149
146
- func (n * nodeWithChildren ) DescendantNames () sets. String {
147
- descendants := n .ChildrenNames ()
150
+ func (n * nodeWithChildren ) Descendants () [] Node {
151
+ descendants := n .Childrens ()
148
152
for child := range n .chainChildren {
149
- descendants . Insert ( child .DescendantNames (). List ()... )
153
+ descendants = append ( descendants , child .Descendants ()... )
150
154
}
151
155
return descendants
152
156
}
153
157
154
- func (* referenceNode ) DescendantNames () sets. String { return sets . NewString () }
158
+ func (* referenceNode ) Descendants () [] Node { return [] Node {} }
155
159
156
160
func (n * workflowNode ) addChainChild (child * chainNode ) {
157
161
n .chainChildren .insert (child )
@@ -216,7 +220,11 @@ func hasCycles(node *chainNode, ancestors sets.String, traversedPath []string) e
216
220
217
221
// NewGraph returns a NodeByType map representing the provided step references, chains, and workflows as a directed graph.
218
222
func NewGraph (stepsByName ReferenceByName , chainsByName ChainByName , workflowsByName WorkflowByName ) (NodeByName , error ) {
219
- nodesByName := make (NodeByName )
223
+ nodesByName := NodeByName {
224
+ References : make (map [string ]Node ),
225
+ Chains : make (map [string ]Node ),
226
+ Workflows : make (map [string ]Node ),
227
+ }
220
228
// References can only be children; load them so they can be added as children by workflows and chains
221
229
referenceNodes := make (referenceNodeByName )
222
230
for name := range stepsByName {
@@ -225,10 +233,10 @@ func NewGraph(stepsByName ReferenceByName, chainsByName ChainByName, workflowsBy
225
233
nodeWithParents : newNodeWithParents (),
226
234
}
227
235
referenceNodes [name ] = node
228
- nodesByName [name ] = node
236
+ nodesByName . References [name ] = node
229
237
}
230
238
// since we may load the parent chain before a child chain, we need to make the parent->child links after loading all chains
231
- parentChildChain := make (map [* chainNode ]string )
239
+ parentChildChain := make (map [* chainNode ][] string )
232
240
chainNodes := make (chainNodeByName )
233
241
for name , chain := range chainsByName {
234
242
node := & chainNode {
@@ -237,29 +245,31 @@ func NewGraph(stepsByName ReferenceByName, chainsByName ChainByName, workflowsBy
237
245
nodeWithParents : newNodeWithParents (),
238
246
}
239
247
chainNodes [name ] = node
240
- nodesByName [name ] = node
248
+ nodesByName . Chains [name ] = node
241
249
for _ , step := range chain {
242
250
if step .Reference != nil {
243
251
if _ , exists := referenceNodes [* step .Reference ]; ! exists {
244
- return nil , fmt .Errorf ("Chain %s contains non-existent reference %s" , name , * step .Reference )
252
+ return nodesByName , fmt .Errorf ("Chain %s contains non-existent reference %s" , name , * step .Reference )
245
253
}
246
254
node .addReferenceChild (referenceNodes [* step .Reference ])
247
255
}
248
256
if step .Chain != nil {
249
- parentChildChain [node ] = * step .Chain
257
+ parentChildChain [node ] = append ( parentChildChain [ node ], * step .Chain )
250
258
}
251
259
}
252
260
}
253
- for parent , child := range parentChildChain {
254
- if _ , exists := chainNodes [child ]; ! exists {
255
- return nil , fmt .Errorf ("Chain %s contains non-existent chain %s" , parent .Name (), child )
261
+ for parent , children := range parentChildChain {
262
+ for _ , child := range children {
263
+ if _ , exists := chainNodes [child ]; ! exists {
264
+ return nodesByName , fmt .Errorf ("Chain %s contains non-existent chain %s" , parent .Name (), child )
265
+ }
266
+ parent .addChainChild (chainNodes [child ])
256
267
}
257
- parent .addChainChild (chainNodes [child ])
258
268
}
259
269
// verify that no cycles exist
260
270
for _ , chain := range chainNodes {
261
271
if err := hasCycles (chain , sets .NewString (), []string {}); err != nil {
262
- return nil , err
272
+ return nodesByName , err
263
273
}
264
274
}
265
275
workflowNodes := make (workflowNodeByName )
@@ -269,18 +279,18 @@ func NewGraph(stepsByName ReferenceByName, chainsByName ChainByName, workflowsBy
269
279
nodeWithChildren : newNodeWithChildren (),
270
280
}
271
281
workflowNodes [name ] = node
272
- nodesByName [name ] = node
282
+ nodesByName . Workflows [name ] = node
273
283
steps := append (workflow .Pre , append (workflow .Test , workflow .Post ... )... )
274
284
for _ , step := range steps {
275
285
if step .Reference != nil {
276
286
if _ , exists := referenceNodes [* step .Reference ]; ! exists {
277
- return nil , fmt .Errorf ("Workflow %s contains non-existent reference %s" , name , * step .Reference )
287
+ return nodesByName , fmt .Errorf ("Workflow %s contains non-existent reference %s" , name , * step .Reference )
278
288
}
279
289
node .addReferenceChild (referenceNodes [* step .Reference ])
280
290
}
281
291
if step .Chain != nil {
282
292
if _ , exists := chainNodes [* step .Chain ]; ! exists {
283
- return nil , fmt .Errorf ("Workflow %s contains non-existent chain %s" , name , * step .Chain )
293
+ return nodesByName , fmt .Errorf ("Workflow %s contains non-existent chain %s" , name , * step .Chain )
284
294
}
285
295
node .addChainChild (chainNodes [* step .Chain ])
286
296
}
0 commit comments