portal/protocols/registry.go

44 lines
987 B
Go
Raw Normal View History

2024-01-12 00:11:53 +00:00
package protocols
import (
"errors"
"git.lumeweb.com/LumeWeb/portal/interfaces"
)
var (
2024-01-16 01:10:15 +00:00
_ interfaces.ProtocolRegistry = (*ProtocolRegistryImpl)(nil)
2024-01-12 00:11:53 +00:00
)
type ProtocolRegistryImpl struct {
protocols map[string]interfaces.Protocol
}
2024-01-16 01:10:15 +00:00
func NewProtocolRegistry() interfaces.ProtocolRegistry {
2024-01-12 00:11:53 +00:00
return &ProtocolRegistryImpl{
protocols: make(map[string]interfaces.Protocol),
}
}
func (r *ProtocolRegistryImpl) Register(name string, protocol interfaces.Protocol) {
2024-01-12 00:11:53 +00:00
if _, exists := r.protocols[name]; exists {
panic("protocol already registered")
2024-01-12 00:11:53 +00:00
}
r.protocols[name] = protocol
}
func (r *ProtocolRegistryImpl) Get(name string) (interfaces.Protocol, error) {
protocol, exists := r.protocols[name]
if !exists {
return nil, errors.New("protocol not found")
}
return protocol, nil
}
func (r *ProtocolRegistryImpl) All() map[string]interfaces.Protocol {
pMap := make(map[string]interfaces.Protocol)
for key, value := range r.protocols {
pMap[key] = value
}
return pMap
}