forked from yourheropaul/inj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
77 lines (65 loc) · 2.56 KB
/
api.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
// +build !noglobals
package inj
//////////////////////////////////////////////
// Interface definitions
//////////////////////////////////////////////
// A Grapher is anything that can represent an application graph
type Grapher interface {
Provide(inputs ...interface{}) error
Inject(fn interface{}, args ...interface{})
Assert() (valid bool, errors []string)
AddDatasource(...interface{}) error
}
//////////////////////////////////////////////
// The one true global variable
//////////////////////////////////////////////
// A default grapher to use in the public API
var graph Grapher = NewGraph()
// Fetch the current grapher instance (in other words, get the global graph)
func GetGrapher() Grapher {
return graph
}
// Set a specific grapher instance, which will replace the global graph.
func SetGrapher(g Grapher) {
graph = g
}
//////////////////////////////////////////////
// Public API
//////////////////////////////////////////////
// Insert zero or more objected into the graph, and then attempt to wire up any unmet
// dependencies in the graph.
//
// As explained in the main documentation (https://godoc.org/github.com/yourheropaul/inj),
// a graph consists of what is essentially a map of types to values. If the same type is
// provided twice with different values, the *last* value will be stored in the graph.
func Provide(inputs ...interface{}) error {
return graph.Provide(inputs...)
}
// Given a function, call it with arguments assigned
// from the graph. Additional arguments can be provided
// for the sake of utility.
//
// Inject() will panic if the provided argument isn't a function,
// or if the provided function accepts variadic arguments (because
// that's not currently supported in the scope of inj).
func Inject(fn interface{}, args ...interface{}) {
graph.Inject(fn, args...)
}
// Make sure that all provided dependencies have their
// requirements met, and return a list of errors if they
// haven't. A graph is never really finalised, so Provide() and
// Assert() can be called any number of times.
func Assert() (valid bool, errors []string) {
return graph.Assert()
}
// Add any number of Datasources, DatasourceReaders or DatasourceWriters
// to the graph. Returns an error if any of the supplied arguments aren't
// one of the accepted types.
//
// Once added, the datasources will be active immediately, and the graph
// will automatically re-Provide itself, so that any depdendencies that
// can only be met by an external datasource will be wired up automatically.
//
func AddDatasource(ds ...interface{}) error {
return graph.AddDatasource(ds...)
}