-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
312 lines (263 loc) · 6.96 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package di
import (
"crypto/sha1"
"errors"
"fmt"
"math/rand"
"os"
"reflect"
"strings"
"sync"
"github.com/jedib0t/go-pretty/v6/table"
)
// 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
}
var mu sync.Mutex
var addCount uint
func incrementAddCount() uint {
mu.Lock()
defer mu.Unlock()
addCount = addCount + 1
return addCount
}
func GenerateUniqueServiceKey(root string) string {
return fmt.Sprintf("%08d.%v", incrementAddCount(), root)
}
func GenerateUniqueServiceKeyFromType(rt reflect.Type) string {
return GenerateUniqueServiceKey(rt.String())
}
func GenerateUniqueServiceKeyFromInterface(in interface{}) string {
return GenerateUniqueServiceKeyFromType(reflect.TypeOf(in).Elem())
}
func RandomString(n int) string {
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
s := make([]rune, n)
for i := range s {
s[i] = letters[rand.Intn(len(letters))]
}
return string(s)
}
func getTypeFullPath(rt reflect.Type) string {
fullPath := rt.PkgPath() + "/" + rt.Name()
return fullPath
}
// generates a unique for from the type
func GenerateReproducableTypeKey(rt reflect.Type) string {
key := getTypeFullPath(rt)
h := sha1.New()
h.Write([]byte(key))
bs := h.Sum(nil)
key = fmt.Sprintf("%x.%s", bs, rt.String())
return key
}
// generates a reproducable key for from the type
func GenerateReproducableInterfaceKey(in interface{}) string {
rt := reflect.TypeOf(in).Elem()
return GenerateReproducableTypeKey(rt)
}
func GetInterfaceReflectType(i interface{}) reflect.Type {
return reflect.TypeOf(i).Elem()
}
func Dump(cnt Container) {
t := table.NewWriter()
t.SetTitle("DI Registrations")
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"#", "Implementation", "Scope", "Interface(s)"})
count := 0
for _, def := range cnt.Definitions() {
if def.Type == nil {
continue
}
typeName := def.Type.String()
scope := ""
switch def.Scope {
case App:
scope = "Singleton"
if def.Unshared {
scope = "Transient"
}
case Request:
scope = "Scoped"
}
sb := strings.Builder{}
for it := range def.ImplementedTypes {
itName := it.Name()
if itName == "" {
itName = "object"
}
sb.WriteString(fmt.Sprintf("%s\n", itName))
}
t.AppendRow([]interface{}{fmt.Sprintf("%d", count), typeName, scope, sb.String()})
t.AppendSeparator()
count++
}
t.Render()
}