github.com/v2fly/v2ray-core/v5@v5.16.2-0.20240507031116-8191faa6e095/common/registry/implementation_set.go (about) 1 package registry 2 3 import ( 4 "github.com/golang/protobuf/proto" 5 6 "github.com/v2fly/v2ray-core/v5/common/protoext" 7 ) 8 9 //go:generate go run github.com/v2fly/v2ray-core/v5/common/errors/errorgen 10 11 type implementationSet struct { 12 AliasLookup map[string]*implementation 13 } 14 15 type CustomLoader func(data []byte, loader LoadByAlias) (proto.Message, error) 16 17 type implementation struct { 18 FullName string 19 Alias []string 20 Loader CustomLoader 21 } 22 23 func (i *implementationSet) RegisterImplementation(name string, opt *protoext.MessageOpt, loader CustomLoader) { 24 alias := opt.GetShortName() 25 26 impl := &implementation{ 27 FullName: name, 28 Alias: alias, 29 } 30 31 for _, aliasName := range alias { 32 i.AliasLookup[aliasName] = impl 33 } 34 } 35 36 func (i *implementationSet) findImplementationByAlias(alias string) (string, CustomLoader, error) { 37 impl, found := i.AliasLookup[alias] 38 if found { 39 return impl.FullName, impl.Loader, nil 40 } 41 return "", nil, newError("cannot find implementation by alias: ", alias) 42 } 43 44 func newImplementationSet() *implementationSet { 45 return &implementationSet{AliasLookup: map[string]*implementation{}} 46 }