-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregistry.go
More file actions
151 lines (124 loc) · 3.98 KB
/
registry.go
File metadata and controls
151 lines (124 loc) · 3.98 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package capability
import (
"fmt"
"reflect"
"sort"
"sync"
)
// ProviderEntry represents a plugin that implements a capability.
type ProviderEntry struct {
// PluginName is the name of the plugin providing this capability.
PluginName string
// Priority determines provider selection order; higher values win.
Priority int
// InterfaceImpl is the reflect.Type of the concrete type implementing the capability.
InterfaceImpl reflect.Type
}
// Registry manages capability contracts and their providers.
// It is safe for concurrent use.
type Registry struct {
mu sync.RWMutex
contracts map[string]Contract
providers map[string][]ProviderEntry
}
// NewRegistry creates a new empty capability registry.
func NewRegistry() *Registry {
return &Registry{
contracts: make(map[string]Contract),
providers: make(map[string][]ProviderEntry),
}
}
// RegisterContract adds a capability contract to the registry.
// Returns an error if a contract with the same name already exists
// but has a different InterfaceType.
func (r *Registry) RegisterContract(c Contract) error {
if c.Name == "" {
return fmt.Errorf("capability: contract name is required")
}
r.mu.Lock()
defer r.mu.Unlock()
if existing, ok := r.contracts[c.Name]; ok {
if existing.InterfaceType != c.InterfaceType {
return fmt.Errorf("capability: contract %q already registered with different interface type (existing: %v, new: %v)",
c.Name, existing.InterfaceType, c.InterfaceType)
}
// Same name and same type — allow re-registration (idempotent).
return nil
}
r.contracts[c.Name] = c
return nil
}
// RegisterProvider registers a plugin as a provider for a capability.
// Returns an error if the capability has not been registered.
func (r *Registry) RegisterProvider(capabilityName, pluginName string, priority int, implType reflect.Type) error {
r.mu.Lock()
defer r.mu.Unlock()
if _, ok := r.contracts[capabilityName]; !ok {
return fmt.Errorf("capability: %q is not a registered capability", capabilityName)
}
r.providers[capabilityName] = append(r.providers[capabilityName], ProviderEntry{
PluginName: pluginName,
Priority: priority,
InterfaceImpl: implType,
})
return nil
}
// Resolve returns the highest-priority provider for a capability.
// Returns an error if no providers are registered for the capability.
func (r *Registry) Resolve(capabilityName string) (*ProviderEntry, error) {
r.mu.RLock()
defer r.mu.RUnlock()
entries, ok := r.providers[capabilityName]
if !ok || len(entries) == 0 {
return nil, fmt.Errorf("capability: no providers registered for %q", capabilityName)
}
best := &entries[0]
for i := 1; i < len(entries); i++ {
if entries[i].Priority > best.Priority {
best = &entries[i]
}
}
return best, nil
}
// ListCapabilities returns a sorted list of all registered capability names.
func (r *Registry) ListCapabilities() []string {
r.mu.RLock()
defer r.mu.RUnlock()
names := make([]string, 0, len(r.contracts))
for name := range r.contracts {
names = append(names, name)
}
sort.Strings(names)
return names
}
// HasProvider returns true if at least one provider is registered for the capability.
func (r *Registry) HasProvider(capabilityName string) bool {
r.mu.RLock()
defer r.mu.RUnlock()
entries, ok := r.providers[capabilityName]
return ok && len(entries) > 0
}
// ListProviders returns all providers registered for a capability.
// Returns nil if no providers are registered.
func (r *Registry) ListProviders(capabilityName string) []ProviderEntry {
r.mu.RLock()
defer r.mu.RUnlock()
entries := r.providers[capabilityName]
if len(entries) == 0 {
return nil
}
result := make([]ProviderEntry, len(entries))
copy(result, entries)
return result
}
// ContractFor returns the contract for a capability name.
// Returns false if the capability is not registered.
func (r *Registry) ContractFor(capabilityName string) (*Contract, bool) {
r.mu.RLock()
defer r.mu.RUnlock()
c, ok := r.contracts[capabilityName]
if !ok {
return nil, false
}
return &c, true
}