diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2021-08-30 11:47:47 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2021-08-30 12:04:25 +0300 |
commit | d52975efc2b4978aef138fb606b81347b82d366c (patch) | |
tree | 70279aa8b53362fccdfc1dbaba02aea77e4448bf | |
parent | 40fae4205d3ad62ca9341620146486bee8d31b28 (diff) |
protobuf: Migrate to use new descriptorpb package
The golang/protobuf dependency is deprecated in favor of
google.golang.org/protobuf. Migrate our usage of the descriptor package
to use the new descriptorpb package instead.
-rw-r--r-- | internal/praefect/protoregistry/protoregistry.go | 29 | ||||
-rw-r--r-- | internal/protoutil/extension.go | 14 | ||||
-rw-r--r-- | proto/go/internal/linter/lint.go | 8 | ||||
-rw-r--r-- | proto/go/internal/linter/lint_test.go | 4 | ||||
-rw-r--r-- | proto/go/internal/linter/method.go | 28 |
5 files changed, 41 insertions, 42 deletions
diff --git a/internal/praefect/protoregistry/protoregistry.go b/internal/praefect/protoregistry/protoregistry.go index 6968adbba..84ed9edfe 100644 --- a/internal/praefect/protoregistry/protoregistry.go +++ b/internal/praefect/protoregistry/protoregistry.go @@ -4,7 +4,6 @@ import ( "fmt" "strings" - "github.com/golang/protobuf/protoc-gen-go/descriptor" "gitlab.com/gitlab-org/gitaly/v14/internal/protoutil" "gitlab.com/gitlab-org/gitaly/v14/proto/go/gitalypb" "google.golang.org/protobuf/proto" @@ -164,7 +163,7 @@ type Registry struct { } // New creates a new ProtoRegistry with info from one or more descriptor.FileDescriptorProto -func New(protos ...*descriptor.FileDescriptorProto) (*Registry, error) { +func New(protos ...*descriptorpb.FileDescriptorProto) (*Registry, error) { methods := make(map[string]MethodInfo) interceptedMethods := make(map[string]struct{}) @@ -214,7 +213,7 @@ func NewFromPaths(paths ...string) (*Registry, error) { type protoFactory func([]byte) (proto.Message, error) -func methodReqFactory(method *descriptor.MethodDescriptorProto) (protoFactory, error) { +func methodReqFactory(method *descriptorpb.MethodDescriptorProto) (protoFactory, error) { // for some reason, the descriptor prepends a dot not expected in Go inputTypeName := strings.TrimPrefix(method.GetInputType(), ".") @@ -236,8 +235,8 @@ func methodReqFactory(method *descriptor.MethodDescriptorProto) (protoFactory, e } func parseMethodInfo( - p *descriptor.FileDescriptorProto, - methodDesc *descriptor.MethodDescriptorProto, + p *descriptorpb.FileDescriptorProto, + methodDesc *descriptorpb.MethodDescriptorProto, fullMethodName string, ) (MethodInfo, error) { opMsg, err := protoutil.GetOpExtension(methodDesc) @@ -330,7 +329,7 @@ func parseMethodInfo( return mi, nil } -func getFileTypes(filename string) ([]*descriptor.DescriptorProto, error) { +func getFileTypes(filename string) ([]*descriptorpb.DescriptorProto, error) { fd, err := protoreg.GlobalFiles.FindFileByPath(filename) if err != nil { return nil, err @@ -350,8 +349,8 @@ func getFileTypes(filename string) ([]*descriptor.DescriptorProto, error) { return types, nil } -func getTopLevelMsgs(p *descriptor.FileDescriptorProto) (map[string]*descriptor.DescriptorProto, error) { - topLevelMsgs := map[string]*descriptor.DescriptorProto{} +func getTopLevelMsgs(p *descriptorpb.FileDescriptorProto) (map[string]*descriptorpb.DescriptorProto, error) { + topLevelMsgs := map[string]*descriptorpb.DescriptorProto{} types, err := getFileTypes(p.GetName()) if err != nil { return nil, err @@ -366,13 +365,13 @@ func getTopLevelMsgs(p *descriptor.FileDescriptorProto) (map[string]*descriptor. // recursively. Then if field matches but type don't match expectedType subMatch method is used // from this point. This matcher assumes that only one field in the message matches the credentials. type matcher struct { - match func(*descriptor.FieldDescriptorProto) (bool, error) - subMatch func(*descriptor.FieldDescriptorProto) (bool, error) - expectedType string // fully qualified name of expected type e.g. ".gitaly.Repository" - topLevelMsgs map[string]*descriptor.DescriptorProto // Map of all top level messages in given file and it dependencies. Result of getTopLevelMsgs should be used. + match func(*descriptorpb.FieldDescriptorProto) (bool, error) + subMatch func(*descriptorpb.FieldDescriptorProto) (bool, error) + expectedType string // fully qualified name of expected type e.g. ".gitaly.Repository" + topLevelMsgs map[string]*descriptorpb.DescriptorProto // Map of all top level messages in given file and it dependencies. Result of getTopLevelMsgs should be used. } -func (m matcher) findField(t *descriptor.DescriptorProto) ([]int, error) { +func (m matcher) findField(t *descriptorpb.DescriptorProto) ([]int, error) { for _, f := range t.GetField() { match, err := m.match(f) if err != nil { @@ -407,8 +406,8 @@ func (m matcher) findField(t *descriptor.DescriptorProto) ([]int, error) { return nil, nil } -func findChildMsg(topLevelMsgs map[string]*descriptor.DescriptorProto, t *descriptor.DescriptorProto, f *descriptor.FieldDescriptorProto) (*descriptor.DescriptorProto, error) { - var childType *descriptor.DescriptorProto +func findChildMsg(topLevelMsgs map[string]*descriptorpb.DescriptorProto, t *descriptorpb.DescriptorProto, f *descriptorpb.FieldDescriptorProto) (*descriptorpb.DescriptorProto, error) { + var childType *descriptorpb.DescriptorProto const msgPrimitive = "TYPE_MESSAGE" if primitive := f.GetType().String(); primitive != msgPrimitive { return nil, nil diff --git a/internal/protoutil/extension.go b/internal/protoutil/extension.go index 5a2b9cd2d..33a294716 100644 --- a/internal/protoutil/extension.go +++ b/internal/protoutil/extension.go @@ -4,15 +4,15 @@ import ( "errors" "fmt" - "github.com/golang/protobuf/protoc-gen-go/descriptor" "gitlab.com/gitlab-org/gitaly/v14/proto/go/gitalypb" "google.golang.org/protobuf/proto" "google.golang.org/protobuf/reflect/protoregistry" "google.golang.org/protobuf/runtime/protoimpl" + "google.golang.org/protobuf/types/descriptorpb" ) // GetOpExtension gets the OperationMsg from a method descriptor -func GetOpExtension(m *descriptor.MethodDescriptorProto) (*gitalypb.OperationMsg, error) { +func GetOpExtension(m *descriptorpb.MethodDescriptorProto) (*gitalypb.OperationMsg, error) { ext, err := getExtension(m.GetOptions(), gitalypb.E_OpType) if err != nil { return nil, err @@ -22,27 +22,27 @@ func GetOpExtension(m *descriptor.MethodDescriptorProto) (*gitalypb.OperationMsg } // IsInterceptedService returns whether the serivce is intercepted by Praefect. -func IsInterceptedService(s *descriptor.ServiceDescriptorProto) (bool, error) { +func IsInterceptedService(s *descriptorpb.ServiceDescriptorProto) (bool, error) { return getBoolExtension(s.GetOptions(), gitalypb.E_Intercepted) } // GetRepositoryExtension gets the repository extension from a field descriptor -func GetRepositoryExtension(m *descriptor.FieldDescriptorProto) (bool, error) { +func GetRepositoryExtension(m *descriptorpb.FieldDescriptorProto) (bool, error) { return getBoolExtension(m.GetOptions(), gitalypb.E_Repository) } // GetStorageExtension gets the storage extension from a field descriptor -func GetStorageExtension(m *descriptor.FieldDescriptorProto) (bool, error) { +func GetStorageExtension(m *descriptorpb.FieldDescriptorProto) (bool, error) { return getBoolExtension(m.GetOptions(), gitalypb.E_Storage) } // GetTargetRepositoryExtension gets the target_repository extension from a field descriptor -func GetTargetRepositoryExtension(m *descriptor.FieldDescriptorProto) (bool, error) { +func GetTargetRepositoryExtension(m *descriptorpb.FieldDescriptorProto) (bool, error) { return getBoolExtension(m.GetOptions(), gitalypb.E_TargetRepository) } // GetAdditionalRepositoryExtension gets the target_repository extension from a field descriptor -func GetAdditionalRepositoryExtension(m *descriptor.FieldDescriptorProto) (bool, error) { +func GetAdditionalRepositoryExtension(m *descriptorpb.FieldDescriptorProto) (bool, error) { return getBoolExtension(m.GetOptions(), gitalypb.E_AdditionalRepository) } diff --git a/proto/go/internal/linter/lint.go b/proto/go/internal/linter/lint.go index f7d0fd43c..95a97e2d8 100644 --- a/proto/go/internal/linter/lint.go +++ b/proto/go/internal/linter/lint.go @@ -4,11 +4,11 @@ import ( "errors" "fmt" - "github.com/golang/protobuf/protoc-gen-go/descriptor" plugin "github.com/golang/protobuf/protoc-gen-go/plugin" "gitlab.com/gitlab-org/gitaly/v14/internal/protoutil" "gitlab.com/gitlab-org/gitaly/v14/proto/go/gitalypb" "google.golang.org/protobuf/reflect/protoregistry" + "google.golang.org/protobuf/types/descriptorpb" ) // ensureMethodOpType will ensure that method includes the op_type option. @@ -17,7 +17,7 @@ import ( // rpc ExampleMethod(ExampleMethodRequest) returns (ExampleMethodResponse) { // option (op_type).op = ACCESSOR; // } -func ensureMethodOpType(fileDesc *descriptor.FileDescriptorProto, m *descriptor.MethodDescriptorProto, req *plugin.CodeGeneratorRequest) error { +func ensureMethodOpType(fileDesc *descriptorpb.FileDescriptorProto, m *descriptorpb.MethodDescriptorProto, req *plugin.CodeGeneratorRequest) error { opMsg, err := protoutil.GetOpExtension(m) if err != nil { if errors.Is(err, protoregistry.NotFound) { @@ -51,7 +51,7 @@ func ensureMethodOpType(fileDesc *descriptor.FileDescriptorProto, m *descriptor. } } -func validateMethod(file *descriptor.FileDescriptorProto, service *descriptor.ServiceDescriptorProto, method *descriptor.MethodDescriptorProto, req *plugin.CodeGeneratorRequest) error { +func validateMethod(file *descriptorpb.FileDescriptorProto, service *descriptorpb.ServiceDescriptorProto, method *descriptorpb.MethodDescriptorProto, req *plugin.CodeGeneratorRequest) error { if intercepted, err := protoutil.IsInterceptedService(service); err != nil { return fmt.Errorf("is intercepted service: %w", err) } else if intercepted { @@ -72,7 +72,7 @@ func validateMethod(file *descriptor.FileDescriptorProto, service *descriptor.Se // LintFile ensures the file described meets Gitaly required processes. // Currently, this is limited to validating if request messages contain // a mandatory operation code. -func LintFile(file *descriptor.FileDescriptorProto, req *plugin.CodeGeneratorRequest) []error { +func LintFile(file *descriptorpb.FileDescriptorProto, req *plugin.CodeGeneratorRequest) []error { var errs []error for _, service := range file.GetService() { diff --git a/proto/go/internal/linter/lint_test.go b/proto/go/internal/linter/lint_test.go index 4bf2653a2..307a1f906 100644 --- a/proto/go/internal/linter/lint_test.go +++ b/proto/go/internal/linter/lint_test.go @@ -4,12 +4,12 @@ import ( "errors" "testing" - "github.com/golang/protobuf/protoc-gen-go/descriptor" plugin "github.com/golang/protobuf/protoc-gen-go/plugin" "github.com/stretchr/testify/require" _ "gitlab.com/gitlab-org/gitaly/v14/proto/go/internal/linter/testdata" "google.golang.org/protobuf/reflect/protodesc" protoreg "google.golang.org/protobuf/reflect/protoregistry" + "google.golang.org/protobuf/types/descriptorpb" ) func TestLintFile(t *testing.T) { @@ -47,7 +47,7 @@ func TestLintFile(t *testing.T) { fdToCheck := protodesc.ToFileDescriptorProto(fd) req := &plugin.CodeGeneratorRequest{ - ProtoFile: []*descriptor.FileDescriptorProto{fdToCheck}, + ProtoFile: []*descriptorpb.FileDescriptorProto{fdToCheck}, } for _, protoPath := range []string{ diff --git a/proto/go/internal/linter/method.go b/proto/go/internal/linter/method.go index bf4d56746..828769020 100644 --- a/proto/go/internal/linter/method.go +++ b/proto/go/internal/linter/method.go @@ -5,16 +5,16 @@ import ( "fmt" "strings" - "github.com/golang/protobuf/protoc-gen-go/descriptor" plugin "github.com/golang/protobuf/protoc-gen-go/plugin" "gitlab.com/gitlab-org/gitaly/v14/internal/protoutil" "gitlab.com/gitlab-org/gitaly/v14/proto/go/gitalypb" + "google.golang.org/protobuf/types/descriptorpb" ) type methodLinter struct { req *plugin.CodeGeneratorRequest - fileDesc *descriptor.FileDescriptorProto - methodDesc *descriptor.MethodDescriptorProto + fileDesc *descriptorpb.FileDescriptorProto + methodDesc *descriptorpb.MethodDescriptorProto opMsg *gitalypb.OperationMsg } @@ -128,8 +128,8 @@ func (ml methodLinter) ensureValidTargetRepository(expected int) error { return nil } -func (ml methodLinter) getTopLevelMsgs() (map[string]*descriptor.DescriptorProto, error) { - topLevelMsgs := map[string]*descriptor.DescriptorProto{} +func (ml methodLinter) getTopLevelMsgs() (map[string]*descriptorpb.DescriptorProto, error) { + topLevelMsgs := map[string]*descriptorpb.DescriptorProto{} types, err := getFileTypes(ml.fileDesc.GetName(), ml.req) if err != nil { @@ -142,13 +142,13 @@ func (ml methodLinter) getTopLevelMsgs() (map[string]*descriptor.DescriptorProto } type matcher struct { - match func(*descriptor.FieldDescriptorProto) (bool, error) - subMatch func(*descriptor.FieldDescriptorProto) (bool, error) + match func(*descriptorpb.FieldDescriptorProto) (bool, error) + subMatch func(*descriptorpb.FieldDescriptorProto) (bool, error) expectedType string - topLevelMsgs map[string]*descriptor.DescriptorProto + topLevelMsgs map[string]*descriptorpb.DescriptorProto } -func (m matcher) findMatchingFields(prefix string, t *descriptor.DescriptorProto) ([]string, error) { +func (m matcher) findMatchingFields(prefix string, t *descriptorpb.DescriptorProto) ([]string, error) { var storageFields []string for _, f := range t.GetField() { subMatcher := m @@ -188,8 +188,8 @@ func (m matcher) findMatchingFields(prefix string, t *descriptor.DescriptorProto return storageFields, nil } -func findChildMsg(topLevelMsgs map[string]*descriptor.DescriptorProto, t *descriptor.DescriptorProto, f *descriptor.FieldDescriptorProto) (*descriptor.DescriptorProto, error) { - var childType *descriptor.DescriptorProto +func findChildMsg(topLevelMsgs map[string]*descriptorpb.DescriptorProto, t *descriptorpb.DescriptorProto, f *descriptorpb.FieldDescriptorProto) (*descriptorpb.DescriptorProto, error) { + var childType *descriptorpb.DescriptorProto const msgPrimitive = "TYPE_MESSAGE" if primitive := f.GetType().String(); primitive != msgPrimitive { return nil, nil @@ -213,9 +213,9 @@ func findChildMsg(topLevelMsgs map[string]*descriptor.DescriptorProto, t *descri return nil, fmt.Errorf("could not find message type %q", msgName) } -func getFileTypes(filename string, req *plugin.CodeGeneratorRequest) ([]*descriptor.DescriptorProto, error) { - var types []*descriptor.DescriptorProto - var protoFile *descriptor.FileDescriptorProto +func getFileTypes(filename string, req *plugin.CodeGeneratorRequest) ([]*descriptorpb.DescriptorProto, error) { + var types []*descriptorpb.DescriptorProto + var protoFile *descriptorpb.FileDescriptorProto for _, pf := range req.ProtoFile { if pf.Name != nil && *pf.Name == filename { types = pf.GetMessageType() |