-
Notifications
You must be signed in to change notification settings - Fork 0
/
godi.go
76 lines (62 loc) · 2.09 KB
/
godi.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
// Package godi is the wrapper around dependency injection container
package godi
import (
"reflect"
"github.com/olbrichattila/godi/internal/container"
)
// Container is an just a wrapper around the internal container to be able to modify the implementation without effecting end users if they would think of referencing the sub package
type Container interface {
Set(string, interface{})
Get(interface{}) (interface{}, error)
Build(map[string]interface{})
GetDependency(string) (interface{}, error)
GetDependencies() map[string]interface{}
Flush()
Delete(paramName string)
Count() int
Call(interface{}, ...interface{}) ([]reflect.Value, error)
}
// Cont is the container structure which follows Container interface
type Cont struct {
c *container.Cont
}
// New creates a new container
func New() Container {
return &Cont{c: container.New()}
}
// Build entire dependency tree
func (t *Cont) Build(dependencies map[string]interface{}) {
t.c.Build(dependencies)
}
// Set Set dependencies to the container
func (t *Cont) Set(paramName string, dependency interface{}) {
t.c.Set(paramName, dependency)
}
// Get resolves dependencies. Use a construct function with your dependency interface type hints. They will be resolved recursively.
func (t *Cont) Get(obj interface{}) (interface{}, error) {
return t.c.Get(obj)
}
// GetDependency retrieve the dependency, or returns error
func (t *Cont) GetDependency(paramName string) (interface{}, error) {
return t.c.GetDependency(paramName)
}
// GetDependencies returns the entire dependency map
func (t *Cont) GetDependencies() map[string]interface{} {
return t.c.GetDependencies()
}
// Flush dependencies
func (t *Cont) Flush() {
t.c.Flush()
}
// Delete one dependency
func (t *Cont) Delete(paramName string) {
t.c.Delete(paramName)
}
// Count returns how any dependencies provided
func (t *Cont) Count() int {
return t.c.Count()
}
// Call can invoke a function auto resolving dependencies and passing optional extra parameters at the beginning
func (t *Cont) Call(fn interface{}, params ...interface{}) ([]reflect.Value, error) {
return t.c.Call(fn, params...)
}