/
utils.go
214 lines (177 loc) · 4.75 KB
/
utils.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package di
import (
"errors"
"fmt"
"reflect"
"strings"
)
// objectKey is used to mark objects.
type objectKey struct {
defName string
uniqueID int
}
// builtList is used to store the objects
// that a container has already built.
type builtList struct {
// last is the name of the last inserted element.
last objectKey
// elements is used to store the inserted elements.
// The key is the name of the element,
// and the value is the number of elements
// in the map when the element is inserted.
elements map[string]int
}
// Add adds an element in the map.
func (l builtList) Add(objKey objectKey) builtList {
newL := builtList{
last: objKey,
elements: map[string]int{},
}
for k, v := range l.elements {
newL.elements[k] = v
}
newL.elements[objKey.defName] = len(newL.elements)
return newL
}
// HasDef checks if the builtList contains the given element.
func (l builtList) HasDef(name string) bool {
_, ok := l.elements[name]
return ok
}
// OrderedList returns the list of elements in the order
// they were inserted.
func (l builtList) OrderedList() []string {
s := make([]string, len(l.elements))
for name, i := range l.elements {
s[i] = name
}
return s
}
// LastElement returns the last inserted element.
func (l builtList) LastElement() (objectKey, bool) {
if len(l.elements) > 0 {
return l.last, true
}
return objectKey{}, false
}
// graph is a Directed Acyclic Graph.
// It is used to store the dependencies inside a container.
// These dependencies are then used to determine the order
// that should be used to close the objects.
type graph struct {
// names contains the keys of the "edges" field.
// It allows the vertices to be sorted.
// It makes the structure deterministic.
names []objectKey
// vertices ordered by name.
vertices map[objectKey]*graphVertex
}
// graphVertex contains the vertex data.
type graphVertex struct {
// numIn in the number of incoming edges.
numIn int
// numInTmp is used by the TopologicalOrdering to avoid messing with numIn
numInTmp int
// out contains the name the outgoing edges.
out []objectKey
// outMap is the same as "out", but in a map
// to quickly check if a vertex is in the outgoing edges.
outMap map[objectKey]struct{}
}
// newGraph creates a new graph.
func newGraph() *graph {
return &graph{
names: []objectKey{},
vertices: map[objectKey]*graphVertex{},
}
}
// AddVertex adds a vertex to the graph.
func (g *graph) AddVertex(v objectKey) {
_, ok := g.vertices[v]
if ok {
return
}
g.names = append(g.names, v)
g.vertices[v] = &graphVertex{
numIn: 0,
out: []objectKey{},
outMap: map[objectKey]struct{}{},
}
}
// AddEdge adds an edge to the graph.
func (g *graph) AddEdge(from, to objectKey) {
g.AddVertex(from)
g.AddVertex(to)
// check if the edge is aleady registered
if _, ok := g.vertices[from].outMap[to]; ok {
return
}
// update the vertices
g.vertices[from].out = append(g.vertices[from].out, to)
g.vertices[from].outMap[to] = struct{}{}
g.vertices[to].numIn++
}
// TopologicalOrdering returns a valid topological sort.
// It implements Kahn's algorithm.
// If there is a cycle in the graph, an error is returned.
// The list of vertices is also returned even if it is not ordered.
func (g *graph) TopologicalOrdering() ([]objectKey, error) {
l := []objectKey{}
q := []objectKey{}
for _, v := range g.names {
if g.vertices[v].numIn == 0 {
q = append(q, v)
}
g.vertices[v].numInTmp = g.vertices[v].numIn
}
for len(q) > 0 {
n := q[len(q)-1]
q = q[:len(q)-1]
l = append(l, n)
for _, m := range g.vertices[n].out {
g.vertices[m].numInTmp--
if g.vertices[m].numInTmp == 0 {
q = append(q, m)
}
}
}
if len(l) != len(g.names) {
return append([]objectKey{}, g.names...), errors.New("a cycle has been found in the dependencies")
}
return l, nil
}
// multiErrBuilder can accumulate errors.
type multiErrBuilder struct {
errs []error
}
// Add adds an error in the multiErrBuilder.
func (b *multiErrBuilder) Add(err error) {
if err != nil {
b.errs = append(b.errs, err)
}
}
// Build returns an errors containing all the messages
// of the accumulated errors. If there is no error
// in the builder, it returns nil.
func (b *multiErrBuilder) Build() error {
if len(b.errs) == 0 {
return nil
}
msgs := make([]string, len(b.errs))
for i, err := range b.errs {
msgs[i] = err.Error()
}
return errors.New(strings.Join(msgs, " AND "))
}
// fill copies src in dest. dest should be a pointer to src type.
func fill(src, dest interface{}) (err error) {
defer func() {
if r := recover(); r != nil {
d := reflect.TypeOf(dest)
s := reflect.TypeOf(src)
err = fmt.Errorf("the fill destination should be a pointer to a `%s`, but you used a `%s`", s, d)
}
}()
reflect.ValueOf(dest).Elem().Set(reflect.ValueOf(src))
return err
}