-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregistry.go
More file actions
97 lines (82 loc) · 2.7 KB
/
registry.go
File metadata and controls
97 lines (82 loc) · 2.7 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
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
package sum
import (
"context"
"github.com/zoobz-io/capitan"
"github.com/zoobz-io/slush"
)
type (
// Guard is a validation function that permits or denies service access.
Guard = slush.Guard
// ServiceInfo describes a registered service for enumeration.
ServiceInfo = slush.ServiceInfo
// Key grants the capability to register services.
Key = slush.Key
)
// Handle configures a registered service with optional guards.
// Wraps slush.Handle to provide sum-specific conveniences.
type Handle[T any] struct {
*slush.Handle[T]
}
// Guard adds a custom guard function to the service.
// Returns the Handle for chaining.
func (h *Handle[T]) Guard(g Guard) *Handle[T] {
h.Handle.Guard(g)
return h
}
// For restricts service access to contexts bearing any of the provided tokens.
// Equivalent to Guard(Require(tokens...)).
func (h *Handle[T]) For(tokens ...Token) *Handle[T] {
h.Handle.Guard(Require(tokens...))
return h
}
// Error re-exports from slush.
var (
ErrNotFound = slush.ErrNotFound
ErrAccessDenied = slush.ErrAccessDenied
ErrInvalidKey = slush.ErrInvalidKey
)
// Signal re-exports from slush.
var (
SignalRegistered capitan.Signal = slush.SignalRegistered
SignalAccessed capitan.Signal = slush.SignalAccessed
SignalDenied capitan.Signal = slush.SignalDenied
SignalNotFound capitan.Signal = slush.SignalNotFound
)
// Field key re-exports from slush.
var (
KeyInterface capitan.Key = slush.KeyInterface
KeyImpl capitan.Key = slush.KeyImpl
KeyError capitan.Key = slush.KeyError
)
// Start initializes the service registry and returns a Key for registration.
// Panics if called more than once.
func Start() Key {
return slush.Start()
}
// Freeze prevents further service registration.
// Panics if key is invalid.
func Freeze(k Key) {
slush.Freeze(k)
}
// Register registers a service implementation for the contract type T.
// Returns a Handle for optional guard configuration.
// Panics if Start has not been called, key is invalid, or registry is frozen.
func Register[T any](k Key, impl T) *Handle[T] {
return &Handle[T]{slush.Register[T](k, impl)}
}
// Use retrieves a service by its contract type T.
// Runs all registered guards with the provided context.
// Returns ErrNotFound if not registered, ErrAccessDenied if a guard fails.
func Use[T any](ctx context.Context) (T, error) {
return slush.Use[T](ctx)
}
// MustUse retrieves a service by its contract type T.
// Panics if the service is not registered or a guard fails.
func MustUse[T any](ctx context.Context) T {
return slush.MustUse[T](ctx)
}
// Services returns information about all registered services.
// Returns ErrInvalidKey if the key is invalid.
func Services(k Key) ([]ServiceInfo, error) {
return slush.Services(k)
}