-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbindings.go
More file actions
68 lines (58 loc) · 1.64 KB
/
Copy pathbindings.go
File metadata and controls
68 lines (58 loc) · 1.64 KB
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
package flowy
import "context"
type bindingsKey struct{}
// BindingKey identifies a typed dependency slot in RunBindings.
// Use package-level sentinels (var DBKey BindingKey[*sql.DB]). One zero-value key per type T.
type BindingKey[T any] struct{}
// RunBindings holds ephemeral runtime dependencies that are never persisted in snapshots.
type RunBindings struct {
m map[any]any
}
// NewRunBindings creates an empty binding container.
func NewRunBindings() *RunBindings {
return &RunBindings{m: make(map[any]any)}
}
// Bind stores a typed dependency under key.
func Bind[T any](b *RunBindings, key BindingKey[T], val T) {
if b == nil {
return
}
if b.m == nil {
b.m = make(map[any]any)
}
b.m[key] = val
}
// Extract returns a typed dependency bound under key.
func Extract[T any](b *RunBindings, key BindingKey[T]) (T, bool) {
var zero T
if b == nil || b.m == nil {
return zero, false
}
raw, ok := b.m[key]
if !ok {
return zero, false
}
typed, ok := raw.(T)
return typed, ok
}
// WithContext attaches bindings to ctx for node handlers.
func (b *RunBindings) WithContext(ctx context.Context) context.Context {
if b == nil {
return ctx
}
return context.WithValue(ctx, bindingsKey{}, b)
}
// BindingsFromContext returns bindings attached to ctx.
func BindingsFromContext(ctx context.Context) (*RunBindings, bool) {
b, ok := ctx.Value(bindingsKey{}).(*RunBindings)
return b, ok
}
// BindingFromContext loads a typed binding from ctx.
func BindingFromContext[T any](ctx context.Context, key BindingKey[T]) (T, bool) {
bindings, ok := BindingsFromContext(ctx)
if !ok {
var zero T
return zero, false
}
return Extract(bindings, key)
}