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
|
|
|
|
2022-01-02 10:16:23 -05: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
|
|
|
|
2022-01-02 10:16:23 -05: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
|
|
|
|
}
|
|
|
|
|
2021-09-04 14:22:41 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-04 14:22:41 -04:00
|
|
|
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
|
|
|
}
|
2021-09-06 08:45:01 -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{}}
|
|
|
|
}
|