-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.go
59 lines (48 loc) · 1.12 KB
/
context.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
package module
import (
"context"
"fmt"
)
// ErrNoPrivoder means that it can't find a module within a given context.
// Usually it misses adding that module to a repo.
var ErrNoPrivoder = fmt.Errorf("can't find module")
type createPanic struct {
key moduleKey
err error
}
type buildContext struct {
context.Context
providers map[moduleKey]providerWithLine
instances map[moduleKey]any
}
func (c *buildContext) Value(key any) any {
moduleKey, ok := key.(moduleKey)
if !ok {
return c.Context.Value(key)
}
if instance, ok := c.instances[moduleKey]; ok {
return instance
}
provider, ok := c.providers[moduleKey]
if !ok {
panic(createPanic{key: moduleKey, err: ErrNoPrivoder})
}
instance, err := provider.provider.value(c)
if err != nil {
panic(createPanic{key: moduleKey, err: err})
}
c.instances[moduleKey] = instance
return instance
}
type moduleContext struct {
context.Context
instances map[moduleKey]any
}
func (c *moduleContext) Value(key any) any {
if moduleKey, ok := key.(moduleKey); ok {
if instance, ok := c.instances[moduleKey]; ok {
return instance
}
}
return c.Context.Value(key)
}