1
- package Container
1
+ package container
2
2
3
3
import (
4
4
"reflect"
@@ -7,25 +7,26 @@ import (
7
7
"golang.org/x/exp/maps"
8
8
)
9
9
10
- // IocContainerConfig - Holds configuration values... soon I will add some more, make them work fully
10
+ // ContainerConfig - Holds configuration values... soon I will add some more, make them work fully
11
11
// Right now this is a placeholder
12
- type IocContainerConfig struct {
12
+ type ContainerConfig struct {
13
13
OnlyInjectStructFieldsWithInjectTag bool
14
14
}
15
15
16
- type IocContainer struct {
17
- Config * IocContainerConfig
16
+ // ContainerInstance - Holds all of our container registration
17
+ type ContainerInstance struct {
18
+ Config * ContainerConfig
18
19
19
20
// Store our singleton instances
20
- // instances map[reflect.Type]*IocContainerBinding
21
+ // instances map[reflect.Type]*Binding
21
22
22
23
// Our resolved singleton instances
23
24
resolved map [reflect.Type ]any
24
25
25
26
// Store our abstract -> concrete bindings
26
27
// If a type doesn't have an abstract type
27
28
// We'll store concrete -> concrete
28
- bindings map [reflect.Type ]* IocContainerBinding
29
+ bindings map [reflect.Type ]* Binding
29
30
30
31
// Store aliases of Concrete -> Abstract, so we can resolve from concrete
31
32
// when we only bound Abstract -> Concrete
@@ -36,16 +37,16 @@ type IocContainer struct {
36
37
tagged map [string ][]reflect.Type
37
38
38
39
// If our container is a child container, we'll have a pointer to our parent
39
- parent * IocContainer
40
+ parent * ContainerInstance
40
41
}
41
42
42
43
// CreateContainer - Create a new container instance
43
- func CreateContainer () * IocContainer {
44
- c := & IocContainer {
45
- Config : & IocContainerConfig {OnlyInjectStructFieldsWithInjectTag : false },
44
+ func CreateContainer () * ContainerInstance {
45
+ c := & ContainerInstance {
46
+ Config : & ContainerConfig {OnlyInjectStructFieldsWithInjectTag : false },
46
47
47
48
resolved : make (map [reflect.Type ]any ),
48
- bindings : make (map [reflect.Type ]* IocContainerBinding ),
49
+ bindings : make (map [reflect.Type ]* Binding ),
49
50
concretes : make (map [reflect.Type ]reflect.Type ),
50
51
tagged : make (map [string ][]reflect.Type ),
51
52
}
@@ -60,11 +61,11 @@ var containerInstances = []unsafe.Pointer{}
60
61
61
62
// CreateChildContainer - Returns a new container, any failed look-ups of our
62
63
// child container, will then be looked up in the parent, or returned nil
63
- func (container * IocContainer ) CreateChildContainer () * IocContainer {
64
- c := & IocContainer {
65
- Config : & IocContainerConfig {OnlyInjectStructFieldsWithInjectTag : false },
64
+ func (container * ContainerInstance ) CreateChildContainer () * ContainerInstance {
65
+ c := & ContainerInstance {
66
+ Config : & ContainerConfig {OnlyInjectStructFieldsWithInjectTag : false },
66
67
resolved : make (map [reflect.Type ]any ),
67
- bindings : make (map [reflect.Type ]* IocContainerBinding ),
68
+ bindings : make (map [reflect.Type ]* Binding ),
68
69
concretes : make (map [reflect.Type ]reflect.Type ),
69
70
tagged : make (map [string ][]reflect.Type ),
70
71
}
@@ -78,19 +79,21 @@ func (container *IocContainer) CreateChildContainer() *IocContainer {
78
79
79
80
// ClearInstances - This will just remove any singleton instances from the container
80
81
// When they are next resolved via Make/MakeTo, they will be instantiated again
81
- func (container * IocContainer ) ClearInstances () {
82
+ func (container * ContainerInstance ) ClearInstances () {
82
83
maps .Clear (container .resolved )
83
84
}
84
85
85
86
// Reset - Reset will empty all bindings in this container, you will have to register
86
87
// any bindings again before you can resolve them.
87
- func (container * IocContainer ) Reset () {
88
+ func (container * ContainerInstance ) Reset () {
88
89
maps .Clear (container .resolved )
89
90
maps .Clear (container .bindings )
90
91
maps .Clear (container .concretes )
92
+ maps .Clear (container .tagged )
93
+ container .parent = nil
91
94
}
92
95
93
96
// ParentContainer - Returns the parent container, if one exists
94
- func (container * IocContainer ) ParentContainer () * IocContainer {
97
+ func (container * ContainerInstance ) ParentContainer () * ContainerInstance {
95
98
return container .parent
96
99
}
0 commit comments