-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkey.go
116 lines (98 loc) · 3.05 KB
/
key.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
package genjector
// baseKeySource is a concrete implementation for KeyOption interface.
type baseKeySource[T any] struct{}
// Key returns the instance of Key that represents a Container key for T type.
//
// It respects KeyOption interface.
func (baseKeySource[T]) Key() Key {
return Key{
Value: (*T)(nil),
}
}
// Container returns the same instance of Container struct provided as an argument.
//
// It respects KeyOption interface.
func (baseKeySource[T]) Container(container Container) Container {
return container
}
// sliceKeySource is a concrete implementation for KeyOption interface.
type sliceKeySource[T any] struct{}
// Key returns the instance of Key that represents a Container key for slice T types.
//
// It respects KeyOption interface.
func (sliceKeySource[T]) Key() Key {
return Key{
Value: (*[]T)(nil),
}
}
// Container returns the same instance of Container struct provided as an argument.
//
// It respects KeyOption interface.
func (sliceKeySource[T]) Container(container Container) Container {
return container
}
// mapKeySource is a concrete implementation for KeyOption interface.
type mapKeySource[K comparable, T any] struct{}
// Key returns the instance of Key that represents a Container key for map K-T pairs.
//
// It respects KeyOption interface.
func (mapKeySource[K, T]) Key() Key {
return Key{
Value: (*map[K]T)(nil),
}
}
// Container returns the same instance of Container struct provided as an argument.
//
// It respects KeyOption interface.
func (mapKeySource[K, T]) Container(container Container) Container {
return container
}
// sameKeyOption is a concrete implementation for KeyOption interface.
type sameKeyOption struct{}
// Key returns the same instance of Key struct provided as an argument.
//
// It respects KeyOption interface.
func (sameKeyOption) Key(key Key) Key {
return key
}
// Container returns the same instance of Container struct provided as an argument.
//
// It respects KeyOption interface.
func (sameKeyOption) Container(container Container) Container {
return container
}
// annotatedKeyOption is a concrete implementation for KeyOption interface.
type annotatedKeyOption struct {
annotation string
}
// Key wrapped instance of Key with a new value for the annotation.
//
// It respects KeyOption interface.
func (o *annotatedKeyOption) Key(key Key) Key {
return Key{
Annotation: o.annotation,
Value: key.Value,
}
}
// Container returns the same instance of Container struct provided as an argument.
//
// It respects KeyOption interface.
func (*annotatedKeyOption) Container(container Container) Container {
return container
}
// containerKeyOption is a concrete implementation for KeyOption interface.
type containerKeyOption struct {
container Container
}
// Key returns the same instance of Key struct provided as an argument.
//
// It respects KeyOption interface.
func (*containerKeyOption) Key(key Key) Key {
return key
}
// Container returns an inner instance of Container.
//
// It respects KeyOption interface.
func (o *containerKeyOption) Container(Container) Container {
return o.container
}