1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-07-08 14:24:36 -04:00
v2fly/common/registry/implementation_set.go

47 lines
1.1 KiB
Go
Raw Normal View History

2021-09-04 10:21:58 -04:00
package registry
2021-09-04 14:06:49 -04:00
import (
"github.com/golang/protobuf/proto"
2021-10-28 06:34:19 -04:00
"github.com/v2fly/v2ray-core/v5/common/protoext"
2021-09-04 14:06:49 -04:00
)
2021-09-04 10:21:58 -04:00
//go:generate go run github.com/v2fly/v2ray-core/v5/common/errors/errorgen
2021-09-04 10:21:58 -04:00
type implementationSet struct {
AliasLookup map[string]*implementation
}
type CustomLoader func(data []byte, loader LoadByAlias) (proto.Message, error)
2021-09-04 14:06:49 -04:00
2021-09-04 10:21:58 -04:00
type implementation struct {
FullName string
Alias []string
2021-09-04 14:06:49 -04:00
Loader CustomLoader
2021-09-04 10:21:58 -04:00
}
2021-09-04 14:06:49 -04:00
func (i *implementationSet) RegisterImplementation(name string, opt *protoext.MessageOpt, loader CustomLoader) {
2021-09-04 10:21:58 -04:00
alias := opt.GetShortName()
impl := &implementation{
FullName: name,
Alias: alias,
}
for _, aliasName := range alias {
i.AliasLookup[aliasName] = impl
}
}
func (i *implementationSet) findImplementationByAlias(alias string) (string, CustomLoader, error) {
2021-09-04 10:21:58 -04:00
impl, found := i.AliasLookup[alias]
if found {
2021-09-04 14:06:49 -04:00
return impl.FullName, impl.Loader, nil
2021-09-04 10:21:58 -04:00
}
return "", nil, newError("cannot find implementation by alias: ", alias)
2021-09-04 10:21:58 -04:00
}
func newImplementationSet() *implementationSet {
return &implementationSet{AliasLookup: map[string]*implementation{}}
}