Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.25
toolchain go1.25.5

require (
github.com/jhump/protoreflect v1.17.1-0.20240913204751-8f5fd1dcb3c5
github.com/bufbuild/protocompile v0.14.1
github.com/jhump/protoreflect/v2 v2.0.0-beta.2
github.com/roadrunner-server/endure/v2 v2.6.2
github.com/roadrunner-server/errors v1.4.1
Expand All @@ -15,17 +15,11 @@ require (
)

require (
github.com/bufbuild/protocompile v0.14.1 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/rogpeppe/go-internal v1.14.1 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/sync v0.19.0 // indirect
golang.org/x/sys v0.39.0 // indirect
golang.org/x/text v0.32.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20251103181224-f26f9409b101 // indirect
google.golang.org/grpc v1.78.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
14 changes: 0 additions & 14 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@ github.com/bufbuild/protocompile v0.14.1/go.mod h1:ppVdAIhbr2H8asPk6k4pY7t9zB1OU
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
github.com/jhump/protoreflect v1.17.1-0.20240913204751-8f5fd1dcb3c5 h1:OUsOWe/nhWohrzIjKP7Wk3Bt1lhDHn0w39uiT/zTWPM=
github.com/jhump/protoreflect v1.17.1-0.20240913204751-8f5fd1dcb3c5/go.mod h1:uUKhM0KLkqvoYeM5BSlLxkJ3Dja3r0N08ru0cacT99E=
github.com/jhump/protoreflect/v2 v2.0.0-beta.2 h1:qZU+rEZUOYTz1Bnhi3xbwn+VxdXkLVeEpAeZzVXLY88=
github.com/jhump/protoreflect/v2 v2.0.0-beta.2/go.mod h1:4tnOYkB/mq7QTyS3YKtVtNrJv4Psqout8HA1U+hZtgM=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
Expand All @@ -33,18 +29,8 @@ go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.27.1 h1:08RqriUEv8+ArZRYSTXy1LeBScaMpVSTBhCeaZYfMYc=
go.uber.org/zap v1.27.1/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
golang.org/x/net v0.47.0 h1:Mx+4dIFzqraBXUugkia1OOvlD6LemFo1ALMHjrXDOhY=
golang.org/x/net v0.47.0/go.mod h1:/jNxtkgq5yWUGYkaZGqo27cfGZ1c5Nen03aYrrKpVRU=
golang.org/x/sync v0.19.0 h1:vV+1eWNmZ5geRlYjzm2adRgW2/mcpevXNg50YZtPCE4=
golang.org/x/sync v0.19.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
golang.org/x/sys v0.39.0 h1:CvCKL8MeisomCi6qNZ+wbb0DN9E5AATixKsvNtMoMFk=
golang.org/x/sys v0.39.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
golang.org/x/text v0.32.0 h1:ZD01bjUt1FQ9WJ0ClOL5vxgxOI/sVCNgX1YtKwcY0mU=
golang.org/x/text v0.32.0/go.mod h1:o/rUWzghvpD5TXrTIBuJU77MTaN0ljMWE47kxGJQ7jY=
google.golang.org/genproto/googleapis/rpc v0.0.0-20251103181224-f26f9409b101 h1:tRPGkdGHuewF4UisLzzHHr1spKw92qLM98nIzxbC0wY=
google.golang.org/genproto/googleapis/rpc v0.0.0-20251103181224-f26f9409b101/go.mod h1:7i2o+ce6H/6BluujYR+kqX3GKH+dChPTQU19wjRPiGk=
google.golang.org/grpc v1.78.0 h1:K1XZG/yGDJnzMdd/uZHAkVqJE+xIDOcmdSFZkBUicNc=
google.golang.org/grpc v1.78.0/go.mod h1:I47qjTo4OKbMkjA/aOOwxDIiPSBofUtQUI5EfpWvW7U=
google.golang.org/protobuf v1.36.11 h1:fV6ZwhNocDyBLK0dj+fg8ektcVegBBuEolpbTQyBNVE=
google.golang.org/protobuf v1.36.11/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
2 changes: 2 additions & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -163,5 +163,7 @@ google.golang.org/genproto/googleapis/api v0.0.0-20251022142026-3a174f9686a8 h1:
google.golang.org/genproto/googleapis/api v0.0.0-20251022142026-3a174f9686a8/go.mod h1:fDMmzKV90WSg1NbozdqrE64fkuTv6mlq2zxo9ad+3yo=
google.golang.org/genproto/googleapis/api v0.0.0-20251029180050-ab9386a59fda h1:+2XxjfsAu6vqFxwGBRcHiMaDCuZiqXGDUDVWVtrFAnE=
google.golang.org/genproto/googleapis/api v0.0.0-20251029180050-ab9386a59fda/go.mod h1:fDMmzKV90WSg1NbozdqrE64fkuTv6mlq2zxo9ad+3yo=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240604185151-ef581f913117/go.mod h1:EfXuqaE1J41VCDicxHzUDm+8rk+7ZdXzHV0IhO/I6s0=
google.golang.org/grpc v1.66.2/go.mod h1:s3/l6xSSCURdVfAnL+TqCNMyTDAGN6+lZeVxnZR128Y=
google.golang.org/grpc v1.77.0 h1:wVVY6/8cGA6vvffn+wWK5ToddbgdU3d8MNENr4evgXM=
google.golang.org/protobuf v1.36.10 h1:AYd7cD/uASjIL6Q9LiTjz8JLcrh/88q5UObnmY3aOOE=
89 changes: 23 additions & 66 deletions registry.go
Original file line number Diff line number Diff line change
@@ -1,56 +1,55 @@
package protoreg

import (
er "errors"
"context"
"strings"

"github.com/jhump/protoreflect/desc"
"github.com/jhump/protoreflect/desc/protoparse"
"github.com/bufbuild/protocompile"
"github.com/jhump/protoreflect/v2/protoresolve"
"github.com/roadrunner-server/errors"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"
)

type Registry interface {
Registry() *protoresolve.Registry
Services() map[string]*desc.ServiceDescriptor
FindMethodByFullPath(method string) (*desc.MethodDescriptor, error)
Services() map[string]protoreflect.ServiceDescriptor
FindMethodByFullPath(method string) (protoreflect.MethodDescriptor, error)
}

type ProtoRegistry struct {
registry *protoresolve.Registry
services map[string]*desc.ServiceDescriptor
services map[string]protoreflect.ServiceDescriptor
}

func (p *Plugin) InitRegistry() (*ProtoRegistry, error) {
reg := &ProtoRegistry{
services: make(map[string]*desc.ServiceDescriptor),
services: make(map[string]protoreflect.ServiceDescriptor),
}

parser := &protoparse.Parser{
ImportPaths: p.config.ProtoPath,
compiler := protocompile.Compiler{
Resolver: protocompile.WithStandardImports(&protocompile.SourceResolver{
ImportPaths: p.config.ProtoPath,
}),
}

files := &protoregistry.Files{}

fds, err := parser.ParseFiles(p.config.Files...)
ctx := context.Background()
fds, err := compiler.Compile(ctx, p.config.Files...)
if err != nil {
return nil, err
}

for _, file := range fds {
err = files.RegisterFile(file.UnwrapFile())
if err != nil {
return nil, err
}

err = registerDependencies(files, parser, file.GetDependencies())
files := &protoregistry.Files{}
for _, fd := range fds {
err = files.RegisterFile(fd)
if err != nil {
return nil, err
}

for _, service := range file.GetServices() {
reg.services[service.GetFullyQualifiedName()] = service
// Collect services
for i := 0; i < fd.Services().Len(); i++ {
svc := fd.Services().Get(i)
reg.services[string(svc.FullName())] = svc
}
}

Expand All @@ -68,12 +67,12 @@ func (reg *ProtoRegistry) Registry() *protoresolve.Registry {
}

// Services returns the service descriptors map
func (reg *ProtoRegistry) Services() map[string]*desc.ServiceDescriptor {
func (reg *ProtoRegistry) Services() map[string]protoreflect.ServiceDescriptor {
return reg.services
}

// FindMethodByFullPath finds a method descriptor by full method path
func (reg *ProtoRegistry) FindMethodByFullPath(method string) (*desc.MethodDescriptor, error) {
func (reg *ProtoRegistry) FindMethodByFullPath(method string) (protoreflect.MethodDescriptor, error) {
parts := strings.Split(strings.TrimPrefix(method, "/"), "/")
if len(parts) != 2 {
return nil, errors.Errorf("Unexpected method")
Expand All @@ -84,47 +83,5 @@ func (reg *ProtoRegistry) FindMethodByFullPath(method string) (*desc.MethodDescr
return nil, errors.Errorf("Service not found: %s", parts[0])
}

return service.FindMethodByName(parts[1]), nil
}

func registerDependencies(files *protoregistry.Files, parser *protoparse.Parser, deps []*desc.FileDescriptor) error {
const op = errors.Op("protoreg_registry_parse_proto")

if len(deps) == 0 {
return nil
}

filenames := filenamesFromDesc(deps)

fds, err := parser.ParseFiles(filenames...)
if err != nil {
return errors.E(op, err)
}

for _, fd := range fds {
_, err = files.FindFileByPath(fd.GetName())
if er.Is(err, protoregistry.NotFound) {
err = files.RegisterFile(fd.UnwrapFile())
if err != nil {
return errors.E(op, err)
}
}

err := registerDependencies(files, parser, fd.GetDependencies())
if err != nil {
return errors.E(op, err)
}
}

return nil
}

func filenamesFromDesc(files []*desc.FileDescriptor) []string {
names := make([]string, len(files))

for i, file := range files {
names[i] = file.GetName()
}

return names
return service.Methods().ByName(protoreflect.Name(parts[1])), nil
}
1 change: 1 addition & 0 deletions tests/protoreg_plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func TestProtoregInit(t *testing.T) {
assert.NoError(t, err)

unknown, err := registry.FindMethodByFullPath("service.v1.Test/Unknown")
assert.NoError(t, err)
assert.Nil(t, unknown)
}

Expand Down
Loading