1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-14 08:15:55 -05:00
v2fly/common/registry/registry.go

50 lines
1.5 KiB
Go
Raw Normal View History

2021-09-04 10:21:58 -04:00
package registry
import (
"github.com/v2fly/v2ray-core/v4/common/protoext"
"google.golang.org/protobuf/proto"
)
type implementationRegistry struct {
implSet map[string]*implementationSet
}
func (i *implementationRegistry) RegisterImplementation(name string, opt *protoext.MessageOpt) {
interfaceType := opt.GetType()[0]
implSet, found := i.implSet[interfaceType]
if !found {
implSet = newImplementationSet()
i.implSet[interfaceType] = implSet
}
implSet.RegisterImplementation(name, opt)
}
func (i *implementationRegistry) FindImplementationByAlias(interfaceType, alias string) (string, error) {
implSet, found := i.implSet[interfaceType]
if !found {
return "", newError("cannot find implemention unknown interface type")
}
return implSet.FindImplementationByAlias(alias)
}
func newImplementationRegistry() *implementationRegistry {
return &implementationRegistry{implSet: map[string]*implementationSet{}}
}
var globalImplementationRegistry = newImplementationRegistry()
func RegisterImplementation(proto proto.Message) error {
msgDesc := proto.ProtoReflect().Type().Descriptor()
fullName := string(msgDesc.FullName())
msgOpts, err := protoext.GetMessageOptions(msgDesc)
if err != nil {
return newError("unable to find message options").Base(err)
}
globalImplementationRegistry.RegisterImplementation(fullName, msgOpts)
return nil
}
func FindImplementationByAlias(interfaceType, alias string) (string, error) {
return globalImplementationRegistry.FindImplementationByAlias(interfaceType, alias)
}