forked from baron-chain/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule_key.go
78 lines (65 loc) · 2.33 KB
/
module_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
package depinject
import (
"reflect"
)
// ModuleKey is a special type used to scope a provider to a "module".
//
// Special module-scoped providers can be used with Provide and ProvideInModule
// by declaring a provider with an input parameter of type ModuleKey. These
// providers may construct a unique value of a dependency for each module and
// will be called at most once per module.
//
// When being used with ProvideInModule, the provider will not receive its
// own ModuleKey but rather the key of the module requesting the dependency
// so that modules can provide module-scoped dependencies to other modules.
//
// In order for a module to retrieve their own module key they can define
// a provider which requires the OwnModuleKey type and DOES NOT require ModuleKey.
type ModuleKey struct {
*moduleKey
}
type moduleKey struct {
name string
}
// Name returns the module key's name.
func (k ModuleKey) Name() string {
return k.name
}
// Equals checks if the module key is equal to another module key. Module keys
// will be equal only if they have the same name and come from the same
// ModuleKeyContext.
func (k ModuleKey) Equals(other ModuleKey) bool {
return k.moduleKey == other.moduleKey
}
var moduleKeyType = reflect.TypeOf(ModuleKey{})
// OwnModuleKey is a type which can be used in a module to retrieve its own
// ModuleKey. It MUST NOT be used together with a ModuleKey dependency.
type OwnModuleKey ModuleKey
var ownModuleKeyType = reflect.TypeOf((*OwnModuleKey)(nil)).Elem()
// ModuleKeyContext defines a context for non-forgeable module keys.
// All module keys with the same name from the same context should be equal
// and module keys with the same name but from different contexts should be
// not equal.
//
// Usage:
//
// moduleKeyCtx := &ModuleKeyContext{}
// fooKey := moduleKeyCtx.For("foo")
type ModuleKeyContext struct {
moduleKeys map[string]*moduleKey
}
// For returns a new or existing module key for the given name within the context.
func (c *ModuleKeyContext) For(moduleName string) ModuleKey {
return ModuleKey{c.createOrGetModuleKey(moduleName)}
}
func (c *ModuleKeyContext) createOrGetModuleKey(moduleName string) *moduleKey {
if c.moduleKeys == nil {
c.moduleKeys = map[string]*moduleKey{}
}
if k, ok := c.moduleKeys[moduleName]; ok {
return k
}
k := &moduleKey{moduleName}
c.moduleKeys[moduleName] = k
return k
}