diff --git a/Makefile b/Makefile index 29f1b072..df06ac2d 100644 --- a/Makefile +++ b/Makefile @@ -11,7 +11,7 @@ export PATH := $(BIN):$(PATH) export GOBIN := $(abspath $(BIN)) COPYRIGHT_YEARS := 2021-2024 LICENSE_IGNORE := --ignore /testdata/ -BUF_VERSION := 1.34.0 +BUF_VERSION := 1.47.2 .PHONY: help help: ## Describe useful make targets @@ -81,6 +81,7 @@ generate: $(BIN)/buf $(BIN)/protoc-gen-go $(BIN)/protoc-gen-connect-go $(BIN)/li go mod tidy rm -rf internal/gen PATH="$(abspath $(BIN))" buf generate + ( cd cmd/protoc-gen-connect-go; ./generate.sh ) license-header \ --license-type apache \ --copyright-holder "The Connect Authors" \ diff --git a/buf.gen.yaml b/buf.gen.yaml index ff7dac42..ddd583eb 100644 --- a/buf.gen.yaml +++ b/buf.gen.yaml @@ -11,3 +11,4 @@ plugins: - local: protoc-gen-connect-go out: internal/gen opt: paths=source_relative +clean: true diff --git a/cmd/protoc-gen-connect-go/generate.sh b/cmd/protoc-gen-connect-go/generate.sh new file mode 100755 index 00000000..aeb9659a --- /dev/null +++ b/cmd/protoc-gen-connect-go/generate.sh @@ -0,0 +1,2 @@ +#!/bin/bash +find testdata -maxdepth 1 -type d \( ! -name testdata \) -exec bash -c "cd '{}' && buf generate" \; diff --git a/cmd/protoc-gen-connect-go/main.go b/cmd/protoc-gen-connect-go/main.go index 77421d68..a852a1c5 100644 --- a/cmd/protoc-gen-connect-go/main.go +++ b/cmd/protoc-gen-connect-go/main.go @@ -23,11 +23,11 @@ // // With [buf], your buf.gen.yaml will look like this: // -// version: v1 +// version: v2 // plugins: -// - name: go +// - local: protoc-gen-go // out: gen -// - name: connect-go +// - local: protoc-gen-connect-go // out: gen // // This generates service definitions for the Protobuf types and services @@ -35,14 +35,38 @@ // invocations above will write output to: // // gen/path/to/file.pb.go -// gen/path/to/connectfoov1/file.connect.go +// gen/path/to/foov1connect/file.connect.go +// +// The generated code is configurable with the same parameters as the protoc-gen-go +// plugin, with the following additional parameters: +// +// - package_suffix: To generate into a sub-package of the package containing the +// base .pb.go files using the given suffix. An empty suffix denotes to +// generate into the same package as the base pb.go files. Default is "connect". +// +// For example, to generate into the same package as the base .pb.go files: +// +// version: v2 +// plugins: +// - local: protoc-gen-go +// out: gen +// - local: protoc-gen-connect-go +// out: gen +// opts: package_suffix +// +// This will generate output to: +// +// gen/path/to/file.pb.go +// gen/path/to/file.connect.go // // [buf]: https://buf.build package main import ( "bytes" + "flag" "fmt" + "go/token" "os" "path" "path/filepath" @@ -64,7 +88,8 @@ const ( connectPackage = protogen.GoImportPath("connectrpc.com/connect") generatedFilenameExtension = ".connect.go" - generatedPackageSuffix = "connect" + defaultPackageSuffix = "connect" + packageSuffixFlagName = "package_suffix" usage = "See https://connectrpc.com/docs/go/getting-started to learn how to use this plugin.\n\nFlags:\n -h, --help\tPrint this help and exit.\n --version\tPrint the version and exit." @@ -89,14 +114,22 @@ func main() { fmt.Fprintln(os.Stderr, usage) os.Exit(1) } - protogen.Options{}.Run( + var flagSet flag.FlagSet + packageSuffix := flagSet.String( + packageSuffixFlagName, + defaultPackageSuffix, + "Generate files into a sub-package of the package containing the base .pb.go files using the given suffix. An empty suffix denotes to generate into the same package as the base pb.go files.", + ) + protogen.Options{ + ParamFunc: flagSet.Set, + }.Run( func(plugin *protogen.Plugin) error { plugin.SupportedFeatures = uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL) | uint64(pluginpb.CodeGeneratorResponse_FEATURE_SUPPORTS_EDITIONS) plugin.SupportedEditionsMinimum = descriptorpb.Edition_EDITION_PROTO2 plugin.SupportedEditionsMaximum = descriptorpb.Edition_EDITION_2023 for _, file := range plugin.Files { if file.Generate { - generate(plugin, file) + generate(plugin, file, *packageSuffix) } } return nil @@ -104,31 +137,40 @@ func main() { ) } -func generate(plugin *protogen.Plugin, file *protogen.File) { +func generate(plugin *protogen.Plugin, file *protogen.File, packageSuffix string) { if len(file.Services) == 0 { return } - file.GoPackageName += generatedPackageSuffix - generatedFilenamePrefixToSlash := filepath.ToSlash(file.GeneratedFilenamePrefix) - file.GeneratedFilenamePrefix = path.Join( - path.Dir(generatedFilenamePrefixToSlash), - string(file.GoPackageName), - path.Base(generatedFilenamePrefixToSlash), - ) - generatedFile := plugin.NewGeneratedFile( - file.GeneratedFilenamePrefix+generatedFilenameExtension, - protogen.GoImportPath(path.Join( + goImportPath := file.GoImportPath + if packageSuffix != "" { + if !token.IsIdentifier(packageSuffix) { + plugin.Error(fmt.Errorf("package_suffix %q is not a valid Go identifier", packageSuffix)) + return + } + file.GoPackageName += protogen.GoPackageName(packageSuffix) + generatedFilenamePrefixToSlash := filepath.ToSlash(file.GeneratedFilenamePrefix) + file.GeneratedFilenamePrefix = path.Join( + path.Dir(generatedFilenamePrefixToSlash), + string(file.GoPackageName), + path.Base(generatedFilenamePrefixToSlash), + ) + goImportPath = protogen.GoImportPath(path.Join( string(file.GoImportPath), string(file.GoPackageName), - )), + )) + } + generatedFile := plugin.NewGeneratedFile( + file.GeneratedFilenamePrefix+generatedFilenameExtension, + goImportPath, ) - generatedFile.Import(file.GoImportPath) + if packageSuffix != "" { + generatedFile.Import(file.GoImportPath) + } generatePreamble(generatedFile, file) generateServiceNameConstants(generatedFile, file.Services) - generateServiceNameVariables(generatedFile, file) for _, service := range file.Services { - generateService(generatedFile, service) + generateService(generatedFile, file, service) } } @@ -213,29 +255,22 @@ func generateServiceNameConstants(g *protogen.GeneratedFile, services []*protoge g.P() } -func generateServiceNameVariables(g *protogen.GeneratedFile, file *protogen.File) { - wrapComments(g, "These variables are the protoreflect.Descriptor objects for the RPCs defined in this package.") - g.P("var (") - for _, service := range file.Services { - serviceDescName := unexport(fmt.Sprintf("%sServiceDescriptor", service.Desc.Name())) - g.P(serviceDescName, ` = `, - g.QualifiedGoIdent(file.GoDescriptorIdent), - `.Services().ByName("`, service.Desc.Name(), `")`) - for _, method := range service.Methods { - g.P(procedureVarMethodDescriptor(method), ` = `, - serviceDescName, - `.Methods().ByName("`, method.Desc.Name(), `")`) - } +func generateServiceMethodsVar(g *protogen.GeneratedFile, file *protogen.File, service *protogen.Service) { + if len(service.Methods) == 0 { + return } - g.P(")") + serviceMethodsName := unexport(fmt.Sprintf("%sMethods", service.Desc.Name())) + g.P(serviceMethodsName, ` := `, + g.QualifiedGoIdent(file.GoDescriptorIdent), + `.Services().ByName("`, service.Desc.Name(), `").Methods()`) } -func generateService(g *protogen.GeneratedFile, service *protogen.Service) { +func generateService(g *protogen.GeneratedFile, file *protogen.File, service *protogen.Service) { names := newNames(service) generateClientInterface(g, service, names) - generateClientImplementation(g, service, names) + generateClientImplementation(g, file, service, names) generateServerInterface(g, service, names) - generateServerConstructor(g, service, names) + generateServerConstructor(g, file, service, names) generateUnimplementedServerImplementation(g, service, names) } @@ -260,7 +295,7 @@ func generateClientInterface(g *protogen.GeneratedFile, service *protogen.Servic g.P() } -func generateClientImplementation(g *protogen.GeneratedFile, service *protogen.Service, names names) { +func generateClientImplementation(g *protogen.GeneratedFile, file *protogen.File, service *protogen.Service, names names) { clientOption := connectPackage.Ident("ClientOption") // Client constructor. @@ -281,6 +316,7 @@ func generateClientImplementation(g *protogen.GeneratedFile, service *protogen.S if len(service.Methods) > 0 { g.P("baseURL = ", stringsPackage.Ident("TrimRight"), `(baseURL, "/")`) } + generateServiceMethodsVar(g, file, service) g.P("return &", names.ClientImpl, "{") for _, method := range service.Methods { g.P(unexport(method.GoName), ": ", @@ -396,7 +432,7 @@ func generateServerInterface(g *protogen.GeneratedFile, service *protogen.Servic g.P() } -func generateServerConstructor(g *protogen.GeneratedFile, service *protogen.Service, names names) { +func generateServerConstructor(g *protogen.GeneratedFile, file *protogen.File, service *protogen.Service, names names) { wrapComments(g, names.ServerConstructor, " builds an HTTP handler from the service implementation.", " It returns the path on which to mount the handler and the handler itself.") g.P("//") @@ -409,6 +445,7 @@ func generateServerConstructor(g *protogen.GeneratedFile, service *protogen.Serv handlerOption := connectPackage.Ident("HandlerOption") g.P("func ", names.ServerConstructor, "(svc ", names.Server, ", opts ...", handlerOption, ") (string, ", httpPackage.Ident("Handler"), ") {") + generateServiceMethodsVar(g, file, service) for _, method := range service.Methods { isStreamingServer := method.Desc.IsStreamingServer() isStreamingClient := method.Desc.IsStreamingClient() @@ -522,7 +559,8 @@ func procedureHandlerName(m *protogen.Method) string { } func procedureVarMethodDescriptor(m *protogen.Method) string { - return unexport(fmt.Sprintf("%s%sMethodDescriptor", m.Parent.GoName, m.GoName)) + serviceMethodsName := unexport(fmt.Sprintf("%sMethods", m.Parent.GoName)) + return serviceMethodsName + `.ByName("` + string(m.Desc.Name()) + `")` } func isDeprecatedService(service *protogen.Service) bool { diff --git a/cmd/protoc-gen-connect-go/main_test.go b/cmd/protoc-gen-connect-go/main_test.go new file mode 100644 index 00000000..03c23cfb --- /dev/null +++ b/cmd/protoc-gen-connect-go/main_test.go @@ -0,0 +1,294 @@ +// Copyright 2021-2024 The Connect Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package main + +import ( + "bytes" + "context" + "embed" + "io" + "net/http" + "net/http/httptest" + "os" + "os/exec" + "runtime" + "strings" + "testing" + + "connectrpc.com/connect" + "connectrpc.com/connect/internal/assert" + pingv1 "connectrpc.com/connect/internal/gen/connect/ping/v1" + "github.com/google/go-cmp/cmp" + "google.golang.org/protobuf/proto" + "google.golang.org/protobuf/reflect/protodesc" + "google.golang.org/protobuf/types/descriptorpb" + "google.golang.org/protobuf/types/pluginpb" + + "connectrpc.com/connect/cmd/protoc-gen-connect-go/testdata/defaultpackage" + "connectrpc.com/connect/cmd/protoc-gen-connect-go/testdata/defaultpackage/defaultpackageconnect" + "connectrpc.com/connect/cmd/protoc-gen-connect-go/testdata/diffpackage" + "connectrpc.com/connect/cmd/protoc-gen-connect-go/testdata/diffpackage/diffpackagediff" + "connectrpc.com/connect/cmd/protoc-gen-connect-go/testdata/noservice" + "connectrpc.com/connect/cmd/protoc-gen-connect-go/testdata/samepackage" +) + +//go:embed testdata +var testdata embed.FS + +func TestVersion(t *testing.T) { + t.Parallel() + stdout, stderr, exitCode := testRunProtocGenGo(t, nil, "--version") + assert.Equal(t, stdout.String(), connect.Version+"\n") + assert.Equal(t, stderr.String(), "") + assert.Equal(t, exitCode, 0) +} + +func TestGenerate(t *testing.T) { + t.Parallel() + pingFileDesc := protodesc.ToFileDescriptorProto(pingv1.File_connect_ping_v1_ping_proto) + compilerVersion := &pluginpb.Version{ + Major: ptr(int32(0)), + Minor: ptr(int32(0)), + Patch: ptr(int32(1)), + Suffix: ptr("test"), + } + t.Run("ping.proto", func(t *testing.T) { + t.Parallel() + req := &pluginpb.CodeGeneratorRequest{ + FileToGenerate: []string{"connect/ping/v1/ping.proto"}, + Parameter: nil, + ProtoFile: []*descriptorpb.FileDescriptorProto{pingFileDesc}, + SourceFileDescriptors: []*descriptorpb.FileDescriptorProto{pingFileDesc}, + CompilerVersion: compilerVersion, + } + rsp := testGenerate(t, req) + assert.Nil(t, rsp.Error) + + assert.Equal(t, rsp.GetSupportedFeatures(), 3) + assert.Equal(t, rsp.GetMinimumEdition(), int32(descriptorpb.Edition_EDITION_PROTO2)) + assert.Equal(t, rsp.GetMaximumEdition(), int32(descriptorpb.Edition_EDITION_2023)) + + assert.Equal(t, len(rsp.File), 1) + file := rsp.File[0] + assert.Equal(t, file.GetName(), "connectrpc.com/connect/internal/gen/connect/ping/v1/pingv1connect/ping.connect.go") + assert.NotZero(t, file.GetContent()) + }) + t.Run("defaultpackage.proto", func(t *testing.T) { + t.Parallel() + defaultPackageFileDesc := protodesc.ToFileDescriptorProto(defaultpackage.File_defaultpackage_proto) + req := &pluginpb.CodeGeneratorRequest{ + FileToGenerate: []string{"defaultpackage.proto"}, + Parameter: nil, + ProtoFile: []*descriptorpb.FileDescriptorProto{defaultPackageFileDesc}, + SourceFileDescriptors: []*descriptorpb.FileDescriptorProto{defaultPackageFileDesc}, + CompilerVersion: compilerVersion, + } + rsp := testGenerate(t, req) + assert.Nil(t, rsp.Error) + + assert.Equal(t, rsp.GetSupportedFeatures(), 3) + assert.Equal(t, rsp.GetMinimumEdition(), int32(descriptorpb.Edition_EDITION_PROTO2)) + assert.Equal(t, rsp.GetMaximumEdition(), int32(descriptorpb.Edition_EDITION_2023)) + + assert.Equal(t, len(rsp.File), 1) + file := rsp.File[0] + assert.Equal(t, file.GetName(), "connectrpc.com/connect/cmd/protoc-gen-connect-go/testdata/defaultpackage/defaultpackageconnect/defaultpackage.connect.go") + assert.NotZero(t, file.GetContent()) + testCmpToTestdata(t, file.GetContent(), "testdata/defaultpackage/defaultpackageconnect/defaultpackage.connect.go") + }) + // Check generated code into a the same package. + t.Run("samepackage.proto", func(t *testing.T) { + t.Parallel() + samePackageFileDesc := protodesc.ToFileDescriptorProto(samepackage.File_samepackage_proto) + req := &pluginpb.CodeGeneratorRequest{ + FileToGenerate: []string{"samepackage.proto"}, + Parameter: ptr("package_suffix"), + ProtoFile: []*descriptorpb.FileDescriptorProto{samePackageFileDesc}, + SourceFileDescriptors: []*descriptorpb.FileDescriptorProto{samePackageFileDesc}, + CompilerVersion: compilerVersion, + } + rsp := testGenerate(t, req) + assert.Nil(t, rsp.Error) + + assert.Equal(t, len(rsp.File), 1) + file := rsp.File[0] + assert.Equal(t, file.GetName(), "connectrpc.com/connect/cmd/protoc-gen-connect-go/testdata/samepackage/samepackage.connect.go") + assert.NotZero(t, file.GetContent()) + testCmpToTestdata(t, file.GetContent(), "testdata/samepackage/samepackage.connect.go") + }) + // Check generated code into a different subpackage. + t.Run("diffpackage.proto", func(t *testing.T) { + t.Parallel() + diffPackageFileDesc := protodesc.ToFileDescriptorProto(diffpackage.File_diffpackage_proto) + req := &pluginpb.CodeGeneratorRequest{ + FileToGenerate: []string{"diffpackage.proto"}, + Parameter: ptr("package_suffix=diff"), + ProtoFile: []*descriptorpb.FileDescriptorProto{diffPackageFileDesc}, + SourceFileDescriptors: []*descriptorpb.FileDescriptorProto{diffPackageFileDesc}, + CompilerVersion: compilerVersion, + } + rsp := testGenerate(t, req) + assert.Nil(t, rsp.Error) + + assert.Equal(t, len(rsp.File), 1) + file := rsp.File[0] + assert.Equal(t, file.GetName(), "connectrpc.com/connect/cmd/protoc-gen-connect-go/testdata/diffpackage/diffpackagediff/diffpackage.connect.go") + assert.NotZero(t, file.GetContent()) + testCmpToTestdata(t, file.GetContent(), "testdata/diffpackage/diffpackagediff/diffpackage.connect.go") + }) + // Validate package_suffix option. + t.Run("ping.proto:invalid_package_suffix", func(t *testing.T) { + t.Parallel() + req := &pluginpb.CodeGeneratorRequest{ + FileToGenerate: []string{"connect/ping/v1/ping.proto"}, + Parameter: ptr("package_suffix=1234"), + ProtoFile: []*descriptorpb.FileDescriptorProto{pingFileDesc}, + SourceFileDescriptors: []*descriptorpb.FileDescriptorProto{pingFileDesc}, + CompilerVersion: compilerVersion, + } + rsp := testGenerate(t, req) + assert.NotNil(t, rsp.Error) + assert.Equal(t, *rsp.Error, `package_suffix "1234" is not a valid Go identifier`) + }) + // Check no service in the file. + t.Run("noservice.proto", func(t *testing.T) { + t.Parallel() + noServiceFileDesc := protodesc.ToFileDescriptorProto(noservice.File_noservice_proto) + req := &pluginpb.CodeGeneratorRequest{ + FileToGenerate: []string{"noservice.proto"}, + ProtoFile: []*descriptorpb.FileDescriptorProto{noServiceFileDesc}, + SourceFileDescriptors: []*descriptorpb.FileDescriptorProto{noServiceFileDesc}, + CompilerVersion: compilerVersion, + } + rsp := testGenerate(t, req) + assert.Nil(t, rsp.Error) + assert.Equal(t, len(rsp.File), 0) + }) +} + +func TestClientHandler(t *testing.T) { + t.Parallel() + ctx := context.Background() + t.Run("defaultpackage.proto", func(t *testing.T) { + t.Parallel() + svc := testDefaultPackageService{} + mux := http.NewServeMux() + mux.Handle(defaultpackageconnect.NewTestServiceHandler(svc)) + server := httptest.NewServer(mux) + client := defaultpackageconnect.NewTestServiceClient(server.Client(), server.URL) + rsp, err := client.Method(ctx, connect.NewRequest(&defaultpackage.Request{})) + assert.Nil(t, err) + assert.NotNil(t, rsp) + }) + t.Run("diffpackage.proto", func(t *testing.T) { + t.Parallel() + svc := testDiffPackageService{} + mux := http.NewServeMux() + mux.Handle(diffpackagediff.NewTestServiceHandler(svc)) + server := httptest.NewServer(mux) + client := diffpackagediff.NewTestServiceClient(server.Client(), server.URL) + rsp, err := client.Method(ctx, connect.NewRequest(&diffpackage.Request{})) + assert.Nil(t, err) + assert.NotNil(t, rsp) + }) + t.Run("samepackage.proto", func(t *testing.T) { + t.Parallel() + svc := testSamePackageService{} + mux := http.NewServeMux() + mux.Handle(samepackage.NewTestServiceHandler(svc)) + server := httptest.NewServer(mux) + client := samepackage.NewTestServiceClient(server.Client(), server.URL) + rsp, err := client.Method(ctx, connect.NewRequest(&samepackage.Request{})) + assert.Nil(t, err) + assert.NotNil(t, rsp) + }) +} + +func testCmpToTestdata(t *testing.T, content, path string) { + t.Helper() + b, err := testdata.ReadFile(path) + assert.Nil(t, err) + // Strip the copyright header and generated by line. + fileContent := string(b) + if codeGenerateIndex := strings.Index(fileContent, "// Code generated by"); codeGenerateIndex != -1 { + fileContent = fileContent[codeGenerateIndex:] + fileContent = strings.Replace(fileContent, "Code generated by protoc-gen-connect-go.", "Code generated by main.", 1) + } + if runtime.GOOS == "windows" { + fileContent = strings.ReplaceAll(fileContent, "\r\n", "\n") + } + assert.Zero(t, cmp.Diff(content, fileContent)) +} + +func testGenerate(t *testing.T, req *pluginpb.CodeGeneratorRequest) *pluginpb.CodeGeneratorResponse { + t.Helper() + + inputBytes, err := proto.Marshal(req) + assert.Nil(t, err) + + stdout, stderr, exitCode := testRunProtocGenGo(t, bytes.NewReader(inputBytes)) + assert.Equal(t, exitCode, 0) + assert.Equal(t, stderr.String(), "") + assert.True(t, len(stdout.Bytes()) > 0) + + var output pluginpb.CodeGeneratorResponse + assert.Nil(t, proto.Unmarshal(stdout.Bytes(), &output)) + return &output +} + +func testRunProtocGenGo(t *testing.T, stdin io.Reader, args ...string) (stdout, stderr *bytes.Buffer, exitCode int) { + t.Helper() + + stdout = &bytes.Buffer{} + stderr = &bytes.Buffer{} + args = append([]string{"run", "main.go"}, args...) + + cmd := exec.Command("go", args...) + cmd.Env = os.Environ() + cmd.Stdin = stdin + cmd.Stdout = stdout + cmd.Stderr = stderr + assert.Nil(t, cmd.Run(), assert.Sprintf("Run go %v", args)) + exitCode = cmd.ProcessState.ExitCode() + return stdout, stderr, exitCode +} + +func ptr[T any](v T) *T { + return &v +} + +type testDefaultPackageService struct { + defaultpackageconnect.UnimplementedTestServiceHandler +} + +func (testDefaultPackageService) Method(context.Context, *connect.Request[defaultpackage.Request]) (*connect.Response[defaultpackage.Response], error) { + return connect.NewResponse(&defaultpackage.Response{}), nil +} + +type testDiffPackageService struct { + diffpackagediff.UnimplementedTestServiceHandler +} + +func (testDiffPackageService) Method(context.Context, *connect.Request[diffpackage.Request]) (*connect.Response[diffpackage.Response], error) { + return connect.NewResponse(&diffpackage.Response{}), nil +} + +type testSamePackageService struct { + samepackage.UnimplementedTestServiceHandler +} + +func (testSamePackageService) Method(context.Context, *connect.Request[samepackage.Request]) (*connect.Response[samepackage.Response], error) { + return connect.NewResponse(&samepackage.Response{}), nil +} diff --git a/cmd/protoc-gen-connect-go/testdata/defaultpackage/buf.gen.yaml b/cmd/protoc-gen-connect-go/testdata/defaultpackage/buf.gen.yaml new file mode 100644 index 00000000..8a32dda2 --- /dev/null +++ b/cmd/protoc-gen-connect-go/testdata/defaultpackage/buf.gen.yaml @@ -0,0 +1,13 @@ +version: v2 +managed: + enabled: true + override: + - file_option: go_package_prefix + value: connectrpc.com/connect/cmd/protoc-gen-connect-go/testdata/defaultpackage +plugins: + - local: protoc-gen-go + out: . + opt: paths=source_relative + - local: protoc-gen-connect-go + out: . + opt: paths=source_relative diff --git a/cmd/protoc-gen-connect-go/testdata/defaultpackage/defaultpackage.pb.go b/cmd/protoc-gen-connect-go/testdata/defaultpackage/defaultpackage.pb.go new file mode 100644 index 00000000..0620fd96 --- /dev/null +++ b/cmd/protoc-gen-connect-go/testdata/defaultpackage/defaultpackage.pb.go @@ -0,0 +1,193 @@ +// Copyright 2021-2024 The Connect Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.36.0 +// protoc (unknown) +// source: defaultpackage.proto + +package defaultpackage + +import ( + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +type Request struct { + state protoimpl.MessageState `protogen:"open.v1"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *Request) Reset() { + *x = Request{} + mi := &file_defaultpackage_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *Request) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Request) ProtoMessage() {} + +func (x *Request) ProtoReflect() protoreflect.Message { + mi := &file_defaultpackage_proto_msgTypes[0] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use Request.ProtoReflect.Descriptor instead. +func (*Request) Descriptor() ([]byte, []int) { + return file_defaultpackage_proto_rawDescGZIP(), []int{0} +} + +type Response struct { + state protoimpl.MessageState `protogen:"open.v1"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *Response) Reset() { + *x = Response{} + mi := &file_defaultpackage_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *Response) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Response) ProtoMessage() {} + +func (x *Response) ProtoReflect() protoreflect.Message { + mi := &file_defaultpackage_proto_msgTypes[1] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use Response.ProtoReflect.Descriptor instead. +func (*Response) Descriptor() ([]byte, []int) { + return file_defaultpackage_proto_rawDescGZIP(), []int{1} +} + +var File_defaultpackage_proto protoreflect.FileDescriptor + +var file_defaultpackage_proto_rawDesc = []byte{ + 0x0a, 0x14, 0x64, 0x65, 0x66, 0x61, 0x75, 0x6c, 0x74, 0x70, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, + 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x1c, 0x63, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x2e, + 0x74, 0x65, 0x73, 0x74, 0x2e, 0x64, 0x65, 0x66, 0x61, 0x75, 0x6c, 0x74, 0x5f, 0x70, 0x61, 0x63, + 0x6b, 0x61, 0x67, 0x65, 0x22, 0x09, 0x0a, 0x07, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x22, + 0x0a, 0x0a, 0x08, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x32, 0x68, 0x0a, 0x0b, 0x54, + 0x65, 0x73, 0x74, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x59, 0x0a, 0x06, 0x4d, 0x65, + 0x74, 0x68, 0x6f, 0x64, 0x12, 0x25, 0x2e, 0x63, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x2e, 0x74, + 0x65, 0x73, 0x74, 0x2e, 0x64, 0x65, 0x66, 0x61, 0x75, 0x6c, 0x74, 0x5f, 0x70, 0x61, 0x63, 0x6b, + 0x61, 0x67, 0x65, 0x2e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x26, 0x2e, 0x63, 0x6f, + 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x2e, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x64, 0x65, 0x66, 0x61, 0x75, + 0x6c, 0x74, 0x5f, 0x70, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0x2e, 0x52, 0x65, 0x73, 0x70, 0x6f, + 0x6e, 0x73, 0x65, 0x22, 0x00, 0x42, 0x8f, 0x02, 0x0a, 0x20, 0x63, 0x6f, 0x6d, 0x2e, 0x63, 0x6f, + 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x2e, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x64, 0x65, 0x66, 0x61, 0x75, + 0x6c, 0x74, 0x5f, 0x70, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0x42, 0x13, 0x44, 0x65, 0x66, 0x61, + 0x75, 0x6c, 0x74, 0x70, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x50, + 0x01, 0x5a, 0x48, 0x63, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x72, 0x70, 0x63, 0x2e, 0x63, 0x6f, + 0x6d, 0x2f, 0x63, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x2f, 0x63, 0x6d, 0x64, 0x2f, 0x70, 0x72, + 0x6f, 0x74, 0x6f, 0x63, 0x2d, 0x67, 0x65, 0x6e, 0x2d, 0x63, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, + 0x2d, 0x67, 0x6f, 0x2f, 0x74, 0x65, 0x73, 0x74, 0x64, 0x61, 0x74, 0x61, 0x2f, 0x64, 0x65, 0x66, + 0x61, 0x75, 0x6c, 0x74, 0x70, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0xa2, 0x02, 0x03, 0x43, 0x54, + 0x44, 0xaa, 0x02, 0x1b, 0x43, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x2e, 0x54, 0x65, 0x73, 0x74, + 0x2e, 0x44, 0x65, 0x66, 0x61, 0x75, 0x6c, 0x74, 0x50, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0xca, + 0x02, 0x1b, 0x43, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x5c, 0x54, 0x65, 0x73, 0x74, 0x5c, 0x44, + 0x65, 0x66, 0x61, 0x75, 0x6c, 0x74, 0x50, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0xe2, 0x02, 0x27, + 0x43, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x5c, 0x54, 0x65, 0x73, 0x74, 0x5c, 0x44, 0x65, 0x66, + 0x61, 0x75, 0x6c, 0x74, 0x50, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0x5c, 0x47, 0x50, 0x42, 0x4d, + 0x65, 0x74, 0x61, 0x64, 0x61, 0x74, 0x61, 0xea, 0x02, 0x1d, 0x43, 0x6f, 0x6e, 0x6e, 0x65, 0x63, + 0x74, 0x3a, 0x3a, 0x54, 0x65, 0x73, 0x74, 0x3a, 0x3a, 0x44, 0x65, 0x66, 0x61, 0x75, 0x6c, 0x74, + 0x50, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_defaultpackage_proto_rawDescOnce sync.Once + file_defaultpackage_proto_rawDescData = file_defaultpackage_proto_rawDesc +) + +func file_defaultpackage_proto_rawDescGZIP() []byte { + file_defaultpackage_proto_rawDescOnce.Do(func() { + file_defaultpackage_proto_rawDescData = protoimpl.X.CompressGZIP(file_defaultpackage_proto_rawDescData) + }) + return file_defaultpackage_proto_rawDescData +} + +var file_defaultpackage_proto_msgTypes = make([]protoimpl.MessageInfo, 2) +var file_defaultpackage_proto_goTypes = []any{ + (*Request)(nil), // 0: connect.test.default_package.Request + (*Response)(nil), // 1: connect.test.default_package.Response +} +var file_defaultpackage_proto_depIdxs = []int32{ + 0, // 0: connect.test.default_package.TestService.Method:input_type -> connect.test.default_package.Request + 1, // 1: connect.test.default_package.TestService.Method:output_type -> connect.test.default_package.Response + 1, // [1:2] is the sub-list for method output_type + 0, // [0:1] is the sub-list for method input_type + 0, // [0:0] is the sub-list for extension type_name + 0, // [0:0] is the sub-list for extension extendee + 0, // [0:0] is the sub-list for field type_name +} + +func init() { file_defaultpackage_proto_init() } +func file_defaultpackage_proto_init() { + if File_defaultpackage_proto != nil { + return + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_defaultpackage_proto_rawDesc, + NumEnums: 0, + NumMessages: 2, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_defaultpackage_proto_goTypes, + DependencyIndexes: file_defaultpackage_proto_depIdxs, + MessageInfos: file_defaultpackage_proto_msgTypes, + }.Build() + File_defaultpackage_proto = out.File + file_defaultpackage_proto_rawDesc = nil + file_defaultpackage_proto_goTypes = nil + file_defaultpackage_proto_depIdxs = nil +} diff --git a/cmd/protoc-gen-connect-go/testdata/defaultpackage/defaultpackage.proto b/cmd/protoc-gen-connect-go/testdata/defaultpackage/defaultpackage.proto new file mode 100644 index 00000000..82a6671d --- /dev/null +++ b/cmd/protoc-gen-connect-go/testdata/defaultpackage/defaultpackage.proto @@ -0,0 +1,25 @@ +// Copyright 2021-2024 The Connect Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +syntax = "proto3"; + +package connect.test.default_package; + +message Request {} + +message Response {} + +service TestService { + rpc Method(Request) returns (Response) {} +} diff --git a/cmd/protoc-gen-connect-go/testdata/defaultpackage/defaultpackageconnect/defaultpackage.connect.go b/cmd/protoc-gen-connect-go/testdata/defaultpackage/defaultpackageconnect/defaultpackage.connect.go new file mode 100644 index 00000000..4a41be9a --- /dev/null +++ b/cmd/protoc-gen-connect-go/testdata/defaultpackage/defaultpackageconnect/defaultpackage.connect.go @@ -0,0 +1,122 @@ +// Copyright 2021-2024 The Connect Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Code generated by protoc-gen-connect-go. DO NOT EDIT. +// +// Source: defaultpackage.proto + +package defaultpackageconnect + +import ( + connect "connectrpc.com/connect" + defaultpackage "connectrpc.com/connect/cmd/protoc-gen-connect-go/testdata/defaultpackage" + context "context" + errors "errors" + http "net/http" + strings "strings" +) + +// This is a compile-time assertion to ensure that this generated file and the connect package are +// compatible. If you get a compiler error that this constant is not defined, this code was +// generated with a version of connect newer than the one compiled into your binary. You can fix the +// problem by either regenerating this code with an older version of connect or updating the connect +// version compiled into your binary. +const _ = connect.IsAtLeastVersion1_13_0 + +const ( + // TestServiceName is the fully-qualified name of the TestService service. + TestServiceName = "connect.test.default_package.TestService" +) + +// These constants are the fully-qualified names of the RPCs defined in this package. They're +// exposed at runtime as Spec.Procedure and as the final two segments of the HTTP route. +// +// Note that these are different from the fully-qualified method names used by +// google.golang.org/protobuf/reflect/protoreflect. To convert from these constants to +// reflection-formatted method names, remove the leading slash and convert the remaining slash to a +// period. +const ( + // TestServiceMethodProcedure is the fully-qualified name of the TestService's Method RPC. + TestServiceMethodProcedure = "/connect.test.default_package.TestService/Method" +) + +// TestServiceClient is a client for the connect.test.default_package.TestService service. +type TestServiceClient interface { + Method(context.Context, *connect.Request[defaultpackage.Request]) (*connect.Response[defaultpackage.Response], error) +} + +// NewTestServiceClient constructs a client for the connect.test.default_package.TestService +// service. By default, it uses the Connect protocol with the binary Protobuf Codec, asks for +// gzipped responses, and sends uncompressed requests. To use the gRPC or gRPC-Web protocols, supply +// the connect.WithGRPC() or connect.WithGRPCWeb() options. +// +// The URL supplied here should be the base URL for the Connect or gRPC server (for example, +// http://api.acme.com or https://acme.com/grpc). +func NewTestServiceClient(httpClient connect.HTTPClient, baseURL string, opts ...connect.ClientOption) TestServiceClient { + baseURL = strings.TrimRight(baseURL, "/") + testServiceMethods := defaultpackage.File_defaultpackage_proto.Services().ByName("TestService").Methods() + return &testServiceClient{ + method: connect.NewClient[defaultpackage.Request, defaultpackage.Response]( + httpClient, + baseURL+TestServiceMethodProcedure, + connect.WithSchema(testServiceMethods.ByName("Method")), + connect.WithClientOptions(opts...), + ), + } +} + +// testServiceClient implements TestServiceClient. +type testServiceClient struct { + method *connect.Client[defaultpackage.Request, defaultpackage.Response] +} + +// Method calls connect.test.default_package.TestService.Method. +func (c *testServiceClient) Method(ctx context.Context, req *connect.Request[defaultpackage.Request]) (*connect.Response[defaultpackage.Response], error) { + return c.method.CallUnary(ctx, req) +} + +// TestServiceHandler is an implementation of the connect.test.default_package.TestService service. +type TestServiceHandler interface { + Method(context.Context, *connect.Request[defaultpackage.Request]) (*connect.Response[defaultpackage.Response], error) +} + +// NewTestServiceHandler builds an HTTP handler from the service implementation. It returns the path +// on which to mount the handler and the handler itself. +// +// By default, handlers support the Connect, gRPC, and gRPC-Web protocols with the binary Protobuf +// and JSON codecs. They also support gzip compression. +func NewTestServiceHandler(svc TestServiceHandler, opts ...connect.HandlerOption) (string, http.Handler) { + testServiceMethods := defaultpackage.File_defaultpackage_proto.Services().ByName("TestService").Methods() + testServiceMethodHandler := connect.NewUnaryHandler( + TestServiceMethodProcedure, + svc.Method, + connect.WithSchema(testServiceMethods.ByName("Method")), + connect.WithHandlerOptions(opts...), + ) + return "/connect.test.default_package.TestService/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch r.URL.Path { + case TestServiceMethodProcedure: + testServiceMethodHandler.ServeHTTP(w, r) + default: + http.NotFound(w, r) + } + }) +} + +// UnimplementedTestServiceHandler returns CodeUnimplemented from all methods. +type UnimplementedTestServiceHandler struct{} + +func (UnimplementedTestServiceHandler) Method(context.Context, *connect.Request[defaultpackage.Request]) (*connect.Response[defaultpackage.Response], error) { + return nil, connect.NewError(connect.CodeUnimplemented, errors.New("connect.test.default_package.TestService.Method is not implemented")) +} diff --git a/cmd/protoc-gen-connect-go/testdata/diffpackage/buf.gen.yaml b/cmd/protoc-gen-connect-go/testdata/diffpackage/buf.gen.yaml new file mode 100644 index 00000000..e3bfa68c --- /dev/null +++ b/cmd/protoc-gen-connect-go/testdata/diffpackage/buf.gen.yaml @@ -0,0 +1,15 @@ +version: v2 +managed: + enabled: true + override: + - file_option: go_package_prefix + value: connectrpc.com/connect/cmd/protoc-gen-connect-go/testdata/diffpackage +plugins: + - local: protoc-gen-go + out: . + opt: paths=source_relative + - local: protoc-gen-connect-go + out: . + opt: + - paths=source_relative + - package_suffix=diff diff --git a/cmd/protoc-gen-connect-go/testdata/diffpackage/diffpackage.pb.go b/cmd/protoc-gen-connect-go/testdata/diffpackage/diffpackage.pb.go new file mode 100644 index 00000000..8e7d60ea --- /dev/null +++ b/cmd/protoc-gen-connect-go/testdata/diffpackage/diffpackage.pb.go @@ -0,0 +1,194 @@ +// Copyright 2021-2024 The Connect Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.36.0 +// protoc (unknown) +// source: diffpackage.proto + +package diffpackage + +import ( + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +type Request struct { + state protoimpl.MessageState `protogen:"open.v1"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *Request) Reset() { + *x = Request{} + mi := &file_diffpackage_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *Request) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Request) ProtoMessage() {} + +func (x *Request) ProtoReflect() protoreflect.Message { + mi := &file_diffpackage_proto_msgTypes[0] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use Request.ProtoReflect.Descriptor instead. +func (*Request) Descriptor() ([]byte, []int) { + return file_diffpackage_proto_rawDescGZIP(), []int{0} +} + +type Response struct { + state protoimpl.MessageState `protogen:"open.v1"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *Response) Reset() { + *x = Response{} + mi := &file_diffpackage_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *Response) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Response) ProtoMessage() {} + +func (x *Response) ProtoReflect() protoreflect.Message { + mi := &file_diffpackage_proto_msgTypes[1] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use Response.ProtoReflect.Descriptor instead. +func (*Response) Descriptor() ([]byte, []int) { + return file_diffpackage_proto_rawDescGZIP(), []int{1} +} + +var File_diffpackage_proto protoreflect.FileDescriptor + +var file_diffpackage_proto_rawDesc = []byte{ + 0x0a, 0x11, 0x64, 0x69, 0x66, 0x66, 0x70, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0x2e, 0x70, 0x72, + 0x6f, 0x74, 0x6f, 0x12, 0x1e, 0x63, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x2e, 0x74, 0x65, 0x73, + 0x74, 0x2e, 0x64, 0x69, 0x66, 0x66, 0x65, 0x72, 0x65, 0x6e, 0x74, 0x5f, 0x70, 0x61, 0x63, 0x6b, + 0x61, 0x67, 0x65, 0x22, 0x09, 0x0a, 0x07, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x22, 0x0a, + 0x0a, 0x08, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x32, 0x6c, 0x0a, 0x0b, 0x54, 0x65, + 0x73, 0x74, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x5d, 0x0a, 0x06, 0x4d, 0x65, 0x74, + 0x68, 0x6f, 0x64, 0x12, 0x27, 0x2e, 0x63, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x2e, 0x74, 0x65, + 0x73, 0x74, 0x2e, 0x64, 0x69, 0x66, 0x66, 0x65, 0x72, 0x65, 0x6e, 0x74, 0x5f, 0x70, 0x61, 0x63, + 0x6b, 0x61, 0x67, 0x65, 0x2e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x28, 0x2e, 0x63, + 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x2e, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x64, 0x69, 0x66, 0x66, + 0x65, 0x72, 0x65, 0x6e, 0x74, 0x5f, 0x70, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0x2e, 0x52, 0x65, + 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x42, 0x93, 0x02, 0x0a, 0x22, 0x63, 0x6f, 0x6d, + 0x2e, 0x63, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x2e, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x64, 0x69, + 0x66, 0x66, 0x65, 0x72, 0x65, 0x6e, 0x74, 0x5f, 0x70, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0x42, + 0x10, 0x44, 0x69, 0x66, 0x66, 0x70, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0x50, 0x72, 0x6f, 0x74, + 0x6f, 0x50, 0x01, 0x5a, 0x45, 0x63, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x72, 0x70, 0x63, 0x2e, + 0x63, 0x6f, 0x6d, 0x2f, 0x63, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x2f, 0x63, 0x6d, 0x64, 0x2f, + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x63, 0x2d, 0x67, 0x65, 0x6e, 0x2d, 0x63, 0x6f, 0x6e, 0x6e, 0x65, + 0x63, 0x74, 0x2d, 0x67, 0x6f, 0x2f, 0x74, 0x65, 0x73, 0x74, 0x64, 0x61, 0x74, 0x61, 0x2f, 0x64, + 0x69, 0x66, 0x66, 0x70, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0xa2, 0x02, 0x03, 0x43, 0x54, 0x44, + 0xaa, 0x02, 0x1d, 0x43, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x2e, 0x54, 0x65, 0x73, 0x74, 0x2e, + 0x44, 0x69, 0x66, 0x66, 0x65, 0x72, 0x65, 0x6e, 0x74, 0x50, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, + 0xca, 0x02, 0x1d, 0x43, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x5c, 0x54, 0x65, 0x73, 0x74, 0x5c, + 0x44, 0x69, 0x66, 0x66, 0x65, 0x72, 0x65, 0x6e, 0x74, 0x50, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, + 0xe2, 0x02, 0x29, 0x43, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x5c, 0x54, 0x65, 0x73, 0x74, 0x5c, + 0x44, 0x69, 0x66, 0x66, 0x65, 0x72, 0x65, 0x6e, 0x74, 0x50, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, + 0x5c, 0x47, 0x50, 0x42, 0x4d, 0x65, 0x74, 0x61, 0x64, 0x61, 0x74, 0x61, 0xea, 0x02, 0x1f, 0x43, + 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x3a, 0x3a, 0x54, 0x65, 0x73, 0x74, 0x3a, 0x3a, 0x44, 0x69, + 0x66, 0x66, 0x65, 0x72, 0x65, 0x6e, 0x74, 0x50, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0x62, 0x06, + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_diffpackage_proto_rawDescOnce sync.Once + file_diffpackage_proto_rawDescData = file_diffpackage_proto_rawDesc +) + +func file_diffpackage_proto_rawDescGZIP() []byte { + file_diffpackage_proto_rawDescOnce.Do(func() { + file_diffpackage_proto_rawDescData = protoimpl.X.CompressGZIP(file_diffpackage_proto_rawDescData) + }) + return file_diffpackage_proto_rawDescData +} + +var file_diffpackage_proto_msgTypes = make([]protoimpl.MessageInfo, 2) +var file_diffpackage_proto_goTypes = []any{ + (*Request)(nil), // 0: connect.test.different_package.Request + (*Response)(nil), // 1: connect.test.different_package.Response +} +var file_diffpackage_proto_depIdxs = []int32{ + 0, // 0: connect.test.different_package.TestService.Method:input_type -> connect.test.different_package.Request + 1, // 1: connect.test.different_package.TestService.Method:output_type -> connect.test.different_package.Response + 1, // [1:2] is the sub-list for method output_type + 0, // [0:1] is the sub-list for method input_type + 0, // [0:0] is the sub-list for extension type_name + 0, // [0:0] is the sub-list for extension extendee + 0, // [0:0] is the sub-list for field type_name +} + +func init() { file_diffpackage_proto_init() } +func file_diffpackage_proto_init() { + if File_diffpackage_proto != nil { + return + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_diffpackage_proto_rawDesc, + NumEnums: 0, + NumMessages: 2, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_diffpackage_proto_goTypes, + DependencyIndexes: file_diffpackage_proto_depIdxs, + MessageInfos: file_diffpackage_proto_msgTypes, + }.Build() + File_diffpackage_proto = out.File + file_diffpackage_proto_rawDesc = nil + file_diffpackage_proto_goTypes = nil + file_diffpackage_proto_depIdxs = nil +} diff --git a/cmd/protoc-gen-connect-go/testdata/diffpackage/diffpackage.proto b/cmd/protoc-gen-connect-go/testdata/diffpackage/diffpackage.proto new file mode 100644 index 00000000..73bb54de --- /dev/null +++ b/cmd/protoc-gen-connect-go/testdata/diffpackage/diffpackage.proto @@ -0,0 +1,25 @@ +// Copyright 2021-2024 The Connect Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +syntax = "proto3"; + +package connect.test.different_package; + +message Request {} + +message Response {} + +service TestService { + rpc Method(Request) returns (Response) {} +} diff --git a/cmd/protoc-gen-connect-go/testdata/diffpackage/diffpackagediff/diffpackage.connect.go b/cmd/protoc-gen-connect-go/testdata/diffpackage/diffpackagediff/diffpackage.connect.go new file mode 100644 index 00000000..7d24fa01 --- /dev/null +++ b/cmd/protoc-gen-connect-go/testdata/diffpackage/diffpackagediff/diffpackage.connect.go @@ -0,0 +1,123 @@ +// Copyright 2021-2024 The Connect Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Code generated by protoc-gen-connect-go. DO NOT EDIT. +// +// Source: diffpackage.proto + +package diffpackagediff + +import ( + connect "connectrpc.com/connect" + diffpackage "connectrpc.com/connect/cmd/protoc-gen-connect-go/testdata/diffpackage" + context "context" + errors "errors" + http "net/http" + strings "strings" +) + +// This is a compile-time assertion to ensure that this generated file and the connect package are +// compatible. If you get a compiler error that this constant is not defined, this code was +// generated with a version of connect newer than the one compiled into your binary. You can fix the +// problem by either regenerating this code with an older version of connect or updating the connect +// version compiled into your binary. +const _ = connect.IsAtLeastVersion1_13_0 + +const ( + // TestServiceName is the fully-qualified name of the TestService service. + TestServiceName = "connect.test.different_package.TestService" +) + +// These constants are the fully-qualified names of the RPCs defined in this package. They're +// exposed at runtime as Spec.Procedure and as the final two segments of the HTTP route. +// +// Note that these are different from the fully-qualified method names used by +// google.golang.org/protobuf/reflect/protoreflect. To convert from these constants to +// reflection-formatted method names, remove the leading slash and convert the remaining slash to a +// period. +const ( + // TestServiceMethodProcedure is the fully-qualified name of the TestService's Method RPC. + TestServiceMethodProcedure = "/connect.test.different_package.TestService/Method" +) + +// TestServiceClient is a client for the connect.test.different_package.TestService service. +type TestServiceClient interface { + Method(context.Context, *connect.Request[diffpackage.Request]) (*connect.Response[diffpackage.Response], error) +} + +// NewTestServiceClient constructs a client for the connect.test.different_package.TestService +// service. By default, it uses the Connect protocol with the binary Protobuf Codec, asks for +// gzipped responses, and sends uncompressed requests. To use the gRPC or gRPC-Web protocols, supply +// the connect.WithGRPC() or connect.WithGRPCWeb() options. +// +// The URL supplied here should be the base URL for the Connect or gRPC server (for example, +// http://api.acme.com or https://acme.com/grpc). +func NewTestServiceClient(httpClient connect.HTTPClient, baseURL string, opts ...connect.ClientOption) TestServiceClient { + baseURL = strings.TrimRight(baseURL, "/") + testServiceMethods := diffpackage.File_diffpackage_proto.Services().ByName("TestService").Methods() + return &testServiceClient{ + method: connect.NewClient[diffpackage.Request, diffpackage.Response]( + httpClient, + baseURL+TestServiceMethodProcedure, + connect.WithSchema(testServiceMethods.ByName("Method")), + connect.WithClientOptions(opts...), + ), + } +} + +// testServiceClient implements TestServiceClient. +type testServiceClient struct { + method *connect.Client[diffpackage.Request, diffpackage.Response] +} + +// Method calls connect.test.different_package.TestService.Method. +func (c *testServiceClient) Method(ctx context.Context, req *connect.Request[diffpackage.Request]) (*connect.Response[diffpackage.Response], error) { + return c.method.CallUnary(ctx, req) +} + +// TestServiceHandler is an implementation of the connect.test.different_package.TestService +// service. +type TestServiceHandler interface { + Method(context.Context, *connect.Request[diffpackage.Request]) (*connect.Response[diffpackage.Response], error) +} + +// NewTestServiceHandler builds an HTTP handler from the service implementation. It returns the path +// on which to mount the handler and the handler itself. +// +// By default, handlers support the Connect, gRPC, and gRPC-Web protocols with the binary Protobuf +// and JSON codecs. They also support gzip compression. +func NewTestServiceHandler(svc TestServiceHandler, opts ...connect.HandlerOption) (string, http.Handler) { + testServiceMethods := diffpackage.File_diffpackage_proto.Services().ByName("TestService").Methods() + testServiceMethodHandler := connect.NewUnaryHandler( + TestServiceMethodProcedure, + svc.Method, + connect.WithSchema(testServiceMethods.ByName("Method")), + connect.WithHandlerOptions(opts...), + ) + return "/connect.test.different_package.TestService/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch r.URL.Path { + case TestServiceMethodProcedure: + testServiceMethodHandler.ServeHTTP(w, r) + default: + http.NotFound(w, r) + } + }) +} + +// UnimplementedTestServiceHandler returns CodeUnimplemented from all methods. +type UnimplementedTestServiceHandler struct{} + +func (UnimplementedTestServiceHandler) Method(context.Context, *connect.Request[diffpackage.Request]) (*connect.Response[diffpackage.Response], error) { + return nil, connect.NewError(connect.CodeUnimplemented, errors.New("connect.test.different_package.TestService.Method is not implemented")) +} diff --git a/cmd/protoc-gen-connect-go/testdata/noservice/buf.gen.yaml b/cmd/protoc-gen-connect-go/testdata/noservice/buf.gen.yaml new file mode 100644 index 00000000..3980a710 --- /dev/null +++ b/cmd/protoc-gen-connect-go/testdata/noservice/buf.gen.yaml @@ -0,0 +1,15 @@ +version: v2 +managed: + enabled: true + override: + - file_option: go_package_prefix + value: connectrpc.com/connect/cmd/protoc-gen-connect-go/testdata/noservice +plugins: + - local: protoc-gen-go + out: . + opt: paths=source_relative + - local: protoc-gen-connect-go + out: . + opt: + - paths=source_relative + - package_suffix=diff diff --git a/cmd/protoc-gen-connect-go/testdata/noservice/noservice.pb.go b/cmd/protoc-gen-connect-go/testdata/noservice/noservice.pb.go new file mode 100644 index 00000000..c6732138 --- /dev/null +++ b/cmd/protoc-gen-connect-go/testdata/noservice/noservice.pb.go @@ -0,0 +1,182 @@ +// Copyright 2021-2024 The Connect Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.36.0 +// protoc (unknown) +// source: noservice.proto + +package noservice + +import ( + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +type Request struct { + state protoimpl.MessageState `protogen:"open.v1"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *Request) Reset() { + *x = Request{} + mi := &file_noservice_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *Request) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Request) ProtoMessage() {} + +func (x *Request) ProtoReflect() protoreflect.Message { + mi := &file_noservice_proto_msgTypes[0] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use Request.ProtoReflect.Descriptor instead. +func (*Request) Descriptor() ([]byte, []int) { + return file_noservice_proto_rawDescGZIP(), []int{0} +} + +type Response struct { + state protoimpl.MessageState `protogen:"open.v1"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *Response) Reset() { + *x = Response{} + mi := &file_noservice_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *Response) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Response) ProtoMessage() {} + +func (x *Response) ProtoReflect() protoreflect.Message { + mi := &file_noservice_proto_msgTypes[1] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use Response.ProtoReflect.Descriptor instead. +func (*Response) Descriptor() ([]byte, []int) { + return file_noservice_proto_rawDescGZIP(), []int{1} +} + +var File_noservice_proto protoreflect.FileDescriptor + +var file_noservice_proto_rawDesc = []byte{ + 0x0a, 0x0f, 0x6e, 0x6f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, + 0x6f, 0x12, 0x17, 0x63, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x2e, 0x74, 0x65, 0x73, 0x74, 0x2e, + 0x6e, 0x6f, 0x5f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x22, 0x09, 0x0a, 0x07, 0x52, 0x65, + 0x71, 0x75, 0x65, 0x73, 0x74, 0x22, 0x0a, 0x0a, 0x08, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, + 0x65, 0x42, 0xec, 0x01, 0x0a, 0x1b, 0x63, 0x6f, 0x6d, 0x2e, 0x63, 0x6f, 0x6e, 0x6e, 0x65, 0x63, + 0x74, 0x2e, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x6e, 0x6f, 0x5f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, + 0x65, 0x42, 0x0e, 0x4e, 0x6f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x50, 0x72, 0x6f, 0x74, + 0x6f, 0x50, 0x01, 0x5a, 0x43, 0x63, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x72, 0x70, 0x63, 0x2e, + 0x63, 0x6f, 0x6d, 0x2f, 0x63, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x2f, 0x63, 0x6d, 0x64, 0x2f, + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x63, 0x2d, 0x67, 0x65, 0x6e, 0x2d, 0x63, 0x6f, 0x6e, 0x6e, 0x65, + 0x63, 0x74, 0x2d, 0x67, 0x6f, 0x2f, 0x74, 0x65, 0x73, 0x74, 0x64, 0x61, 0x74, 0x61, 0x2f, 0x6e, + 0x6f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0xa2, 0x02, 0x03, 0x43, 0x54, 0x4e, 0xaa, 0x02, + 0x16, 0x43, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x2e, 0x54, 0x65, 0x73, 0x74, 0x2e, 0x4e, 0x6f, + 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0xca, 0x02, 0x16, 0x43, 0x6f, 0x6e, 0x6e, 0x65, 0x63, + 0x74, 0x5c, 0x54, 0x65, 0x73, 0x74, 0x5c, 0x4e, 0x6f, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, + 0xe2, 0x02, 0x22, 0x43, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x5c, 0x54, 0x65, 0x73, 0x74, 0x5c, + 0x4e, 0x6f, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x5c, 0x47, 0x50, 0x42, 0x4d, 0x65, 0x74, + 0x61, 0x64, 0x61, 0x74, 0x61, 0xea, 0x02, 0x18, 0x43, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x3a, + 0x3a, 0x54, 0x65, 0x73, 0x74, 0x3a, 0x3a, 0x4e, 0x6f, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, + 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_noservice_proto_rawDescOnce sync.Once + file_noservice_proto_rawDescData = file_noservice_proto_rawDesc +) + +func file_noservice_proto_rawDescGZIP() []byte { + file_noservice_proto_rawDescOnce.Do(func() { + file_noservice_proto_rawDescData = protoimpl.X.CompressGZIP(file_noservice_proto_rawDescData) + }) + return file_noservice_proto_rawDescData +} + +var file_noservice_proto_msgTypes = make([]protoimpl.MessageInfo, 2) +var file_noservice_proto_goTypes = []any{ + (*Request)(nil), // 0: connect.test.no_service.Request + (*Response)(nil), // 1: connect.test.no_service.Response +} +var file_noservice_proto_depIdxs = []int32{ + 0, // [0:0] is the sub-list for method output_type + 0, // [0:0] is the sub-list for method input_type + 0, // [0:0] is the sub-list for extension type_name + 0, // [0:0] is the sub-list for extension extendee + 0, // [0:0] is the sub-list for field type_name +} + +func init() { file_noservice_proto_init() } +func file_noservice_proto_init() { + if File_noservice_proto != nil { + return + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_noservice_proto_rawDesc, + NumEnums: 0, + NumMessages: 2, + NumExtensions: 0, + NumServices: 0, + }, + GoTypes: file_noservice_proto_goTypes, + DependencyIndexes: file_noservice_proto_depIdxs, + MessageInfos: file_noservice_proto_msgTypes, + }.Build() + File_noservice_proto = out.File + file_noservice_proto_rawDesc = nil + file_noservice_proto_goTypes = nil + file_noservice_proto_depIdxs = nil +} diff --git a/cmd/protoc-gen-connect-go/testdata/noservice/noservice.proto b/cmd/protoc-gen-connect-go/testdata/noservice/noservice.proto new file mode 100644 index 00000000..2e6cbf5c --- /dev/null +++ b/cmd/protoc-gen-connect-go/testdata/noservice/noservice.proto @@ -0,0 +1,21 @@ +// Copyright 2021-2024 The Connect Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +syntax = "proto3"; + +package connect.test.no_service; + +message Request {} + +message Response {} diff --git a/cmd/protoc-gen-connect-go/testdata/samepackage/buf.gen.yaml b/cmd/protoc-gen-connect-go/testdata/samepackage/buf.gen.yaml new file mode 100644 index 00000000..5672478c --- /dev/null +++ b/cmd/protoc-gen-connect-go/testdata/samepackage/buf.gen.yaml @@ -0,0 +1,15 @@ +version: v2 +managed: + enabled: true + override: + - file_option: go_package_prefix + value: connectrpc.com/connect/cmd/protoc-gen-connect-go/testdata/samepackage +plugins: + - local: protoc-gen-go + out: . + opt: paths=source_relative + - local: protoc-gen-connect-go + out: . + opt: + - paths=source_relative + - package_suffix diff --git a/cmd/protoc-gen-connect-go/testdata/samepackage/samepackage.connect.go b/cmd/protoc-gen-connect-go/testdata/samepackage/samepackage.connect.go new file mode 100644 index 00000000..c8e3e485 --- /dev/null +++ b/cmd/protoc-gen-connect-go/testdata/samepackage/samepackage.connect.go @@ -0,0 +1,121 @@ +// Copyright 2021-2024 The Connect Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Code generated by protoc-gen-connect-go. DO NOT EDIT. +// +// Source: samepackage.proto + +package samepackage + +import ( + connect "connectrpc.com/connect" + context "context" + errors "errors" + http "net/http" + strings "strings" +) + +// This is a compile-time assertion to ensure that this generated file and the connect package are +// compatible. If you get a compiler error that this constant is not defined, this code was +// generated with a version of connect newer than the one compiled into your binary. You can fix the +// problem by either regenerating this code with an older version of connect or updating the connect +// version compiled into your binary. +const _ = connect.IsAtLeastVersion1_13_0 + +const ( + // TestServiceName is the fully-qualified name of the TestService service. + TestServiceName = "connect.test.same_package.TestService" +) + +// These constants are the fully-qualified names of the RPCs defined in this package. They're +// exposed at runtime as Spec.Procedure and as the final two segments of the HTTP route. +// +// Note that these are different from the fully-qualified method names used by +// google.golang.org/protobuf/reflect/protoreflect. To convert from these constants to +// reflection-formatted method names, remove the leading slash and convert the remaining slash to a +// period. +const ( + // TestServiceMethodProcedure is the fully-qualified name of the TestService's Method RPC. + TestServiceMethodProcedure = "/connect.test.same_package.TestService/Method" +) + +// TestServiceClient is a client for the connect.test.same_package.TestService service. +type TestServiceClient interface { + Method(context.Context, *connect.Request[Request]) (*connect.Response[Response], error) +} + +// NewTestServiceClient constructs a client for the connect.test.same_package.TestService service. +// By default, it uses the Connect protocol with the binary Protobuf Codec, asks for gzipped +// responses, and sends uncompressed requests. To use the gRPC or gRPC-Web protocols, supply the +// connect.WithGRPC() or connect.WithGRPCWeb() options. +// +// The URL supplied here should be the base URL for the Connect or gRPC server (for example, +// http://api.acme.com or https://acme.com/grpc). +func NewTestServiceClient(httpClient connect.HTTPClient, baseURL string, opts ...connect.ClientOption) TestServiceClient { + baseURL = strings.TrimRight(baseURL, "/") + testServiceMethods := File_samepackage_proto.Services().ByName("TestService").Methods() + return &testServiceClient{ + method: connect.NewClient[Request, Response]( + httpClient, + baseURL+TestServiceMethodProcedure, + connect.WithSchema(testServiceMethods.ByName("Method")), + connect.WithClientOptions(opts...), + ), + } +} + +// testServiceClient implements TestServiceClient. +type testServiceClient struct { + method *connect.Client[Request, Response] +} + +// Method calls connect.test.same_package.TestService.Method. +func (c *testServiceClient) Method(ctx context.Context, req *connect.Request[Request]) (*connect.Response[Response], error) { + return c.method.CallUnary(ctx, req) +} + +// TestServiceHandler is an implementation of the connect.test.same_package.TestService service. +type TestServiceHandler interface { + Method(context.Context, *connect.Request[Request]) (*connect.Response[Response], error) +} + +// NewTestServiceHandler builds an HTTP handler from the service implementation. It returns the path +// on which to mount the handler and the handler itself. +// +// By default, handlers support the Connect, gRPC, and gRPC-Web protocols with the binary Protobuf +// and JSON codecs. They also support gzip compression. +func NewTestServiceHandler(svc TestServiceHandler, opts ...connect.HandlerOption) (string, http.Handler) { + testServiceMethods := File_samepackage_proto.Services().ByName("TestService").Methods() + testServiceMethodHandler := connect.NewUnaryHandler( + TestServiceMethodProcedure, + svc.Method, + connect.WithSchema(testServiceMethods.ByName("Method")), + connect.WithHandlerOptions(opts...), + ) + return "/connect.test.same_package.TestService/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch r.URL.Path { + case TestServiceMethodProcedure: + testServiceMethodHandler.ServeHTTP(w, r) + default: + http.NotFound(w, r) + } + }) +} + +// UnimplementedTestServiceHandler returns CodeUnimplemented from all methods. +type UnimplementedTestServiceHandler struct{} + +func (UnimplementedTestServiceHandler) Method(context.Context, *connect.Request[Request]) (*connect.Response[Response], error) { + return nil, connect.NewError(connect.CodeUnimplemented, errors.New("connect.test.same_package.TestService.Method is not implemented")) +} diff --git a/cmd/protoc-gen-connect-go/testdata/samepackage/samepackage.pb.go b/cmd/protoc-gen-connect-go/testdata/samepackage/samepackage.pb.go new file mode 100644 index 00000000..b51e91ca --- /dev/null +++ b/cmd/protoc-gen-connect-go/testdata/samepackage/samepackage.pb.go @@ -0,0 +1,191 @@ +// Copyright 2021-2024 The Connect Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.36.0 +// protoc (unknown) +// source: samepackage.proto + +package samepackage + +import ( + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +type Request struct { + state protoimpl.MessageState `protogen:"open.v1"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *Request) Reset() { + *x = Request{} + mi := &file_samepackage_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *Request) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Request) ProtoMessage() {} + +func (x *Request) ProtoReflect() protoreflect.Message { + mi := &file_samepackage_proto_msgTypes[0] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use Request.ProtoReflect.Descriptor instead. +func (*Request) Descriptor() ([]byte, []int) { + return file_samepackage_proto_rawDescGZIP(), []int{0} +} + +type Response struct { + state protoimpl.MessageState `protogen:"open.v1"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *Response) Reset() { + *x = Response{} + mi := &file_samepackage_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *Response) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Response) ProtoMessage() {} + +func (x *Response) ProtoReflect() protoreflect.Message { + mi := &file_samepackage_proto_msgTypes[1] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use Response.ProtoReflect.Descriptor instead. +func (*Response) Descriptor() ([]byte, []int) { + return file_samepackage_proto_rawDescGZIP(), []int{1} +} + +var File_samepackage_proto protoreflect.FileDescriptor + +var file_samepackage_proto_rawDesc = []byte{ + 0x0a, 0x11, 0x73, 0x61, 0x6d, 0x65, 0x70, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0x2e, 0x70, 0x72, + 0x6f, 0x74, 0x6f, 0x12, 0x19, 0x63, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x2e, 0x74, 0x65, 0x73, + 0x74, 0x2e, 0x73, 0x61, 0x6d, 0x65, 0x5f, 0x70, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0x22, 0x09, + 0x0a, 0x07, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x22, 0x0a, 0x0a, 0x08, 0x52, 0x65, 0x73, + 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x32, 0x62, 0x0a, 0x0b, 0x54, 0x65, 0x73, 0x74, 0x53, 0x65, 0x72, + 0x76, 0x69, 0x63, 0x65, 0x12, 0x53, 0x0a, 0x06, 0x4d, 0x65, 0x74, 0x68, 0x6f, 0x64, 0x12, 0x22, + 0x2e, 0x63, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x2e, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x73, 0x61, + 0x6d, 0x65, 0x5f, 0x70, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0x2e, 0x52, 0x65, 0x71, 0x75, 0x65, + 0x73, 0x74, 0x1a, 0x23, 0x2e, 0x63, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x2e, 0x74, 0x65, 0x73, + 0x74, 0x2e, 0x73, 0x61, 0x6d, 0x65, 0x5f, 0x70, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0x2e, 0x52, + 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x42, 0xfa, 0x01, 0x0a, 0x1d, 0x63, 0x6f, + 0x6d, 0x2e, 0x63, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x2e, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x73, + 0x61, 0x6d, 0x65, 0x5f, 0x70, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0x42, 0x10, 0x53, 0x61, 0x6d, + 0x65, 0x70, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x50, 0x01, 0x5a, + 0x45, 0x63, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x72, 0x70, 0x63, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, + 0x63, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x2f, 0x63, 0x6d, 0x64, 0x2f, 0x70, 0x72, 0x6f, 0x74, + 0x6f, 0x63, 0x2d, 0x67, 0x65, 0x6e, 0x2d, 0x63, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x2d, 0x67, + 0x6f, 0x2f, 0x74, 0x65, 0x73, 0x74, 0x64, 0x61, 0x74, 0x61, 0x2f, 0x73, 0x61, 0x6d, 0x65, 0x70, + 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0xa2, 0x02, 0x03, 0x43, 0x54, 0x53, 0xaa, 0x02, 0x18, 0x43, + 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x2e, 0x54, 0x65, 0x73, 0x74, 0x2e, 0x53, 0x61, 0x6d, 0x65, + 0x50, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0xca, 0x02, 0x18, 0x43, 0x6f, 0x6e, 0x6e, 0x65, 0x63, + 0x74, 0x5c, 0x54, 0x65, 0x73, 0x74, 0x5c, 0x53, 0x61, 0x6d, 0x65, 0x50, 0x61, 0x63, 0x6b, 0x61, + 0x67, 0x65, 0xe2, 0x02, 0x24, 0x43, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x5c, 0x54, 0x65, 0x73, + 0x74, 0x5c, 0x53, 0x61, 0x6d, 0x65, 0x50, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0x5c, 0x47, 0x50, + 0x42, 0x4d, 0x65, 0x74, 0x61, 0x64, 0x61, 0x74, 0x61, 0xea, 0x02, 0x1a, 0x43, 0x6f, 0x6e, 0x6e, + 0x65, 0x63, 0x74, 0x3a, 0x3a, 0x54, 0x65, 0x73, 0x74, 0x3a, 0x3a, 0x53, 0x61, 0x6d, 0x65, 0x50, + 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_samepackage_proto_rawDescOnce sync.Once + file_samepackage_proto_rawDescData = file_samepackage_proto_rawDesc +) + +func file_samepackage_proto_rawDescGZIP() []byte { + file_samepackage_proto_rawDescOnce.Do(func() { + file_samepackage_proto_rawDescData = protoimpl.X.CompressGZIP(file_samepackage_proto_rawDescData) + }) + return file_samepackage_proto_rawDescData +} + +var file_samepackage_proto_msgTypes = make([]protoimpl.MessageInfo, 2) +var file_samepackage_proto_goTypes = []any{ + (*Request)(nil), // 0: connect.test.same_package.Request + (*Response)(nil), // 1: connect.test.same_package.Response +} +var file_samepackage_proto_depIdxs = []int32{ + 0, // 0: connect.test.same_package.TestService.Method:input_type -> connect.test.same_package.Request + 1, // 1: connect.test.same_package.TestService.Method:output_type -> connect.test.same_package.Response + 1, // [1:2] is the sub-list for method output_type + 0, // [0:1] is the sub-list for method input_type + 0, // [0:0] is the sub-list for extension type_name + 0, // [0:0] is the sub-list for extension extendee + 0, // [0:0] is the sub-list for field type_name +} + +func init() { file_samepackage_proto_init() } +func file_samepackage_proto_init() { + if File_samepackage_proto != nil { + return + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_samepackage_proto_rawDesc, + NumEnums: 0, + NumMessages: 2, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_samepackage_proto_goTypes, + DependencyIndexes: file_samepackage_proto_depIdxs, + MessageInfos: file_samepackage_proto_msgTypes, + }.Build() + File_samepackage_proto = out.File + file_samepackage_proto_rawDesc = nil + file_samepackage_proto_goTypes = nil + file_samepackage_proto_depIdxs = nil +} diff --git a/cmd/protoc-gen-connect-go/testdata/samepackage/samepackage.proto b/cmd/protoc-gen-connect-go/testdata/samepackage/samepackage.proto new file mode 100644 index 00000000..de95f281 --- /dev/null +++ b/cmd/protoc-gen-connect-go/testdata/samepackage/samepackage.proto @@ -0,0 +1,25 @@ +// Copyright 2021-2024 The Connect Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +syntax = "proto3"; + +package connect.test.same_package; + +message Request {} + +message Response {} + +service TestService { + rpc Method(Request) returns (Response) {} +} diff --git a/internal/gen/connect/collide/v1/collidev1connect/collide.connect.go b/internal/gen/connect/collide/v1/collidev1connect/collide.connect.go index 8821b506..53610e6e 100644 --- a/internal/gen/connect/collide/v1/collidev1connect/collide.connect.go +++ b/internal/gen/connect/collide/v1/collidev1connect/collide.connect.go @@ -51,12 +51,6 @@ const ( CollideServiceImportProcedure = "/connect.collide.v1.CollideService/Import" ) -// These variables are the protoreflect.Descriptor objects for the RPCs defined in this package. -var ( - collideServiceServiceDescriptor = v1.File_connect_collide_v1_collide_proto.Services().ByName("CollideService") - collideServiceImportMethodDescriptor = collideServiceServiceDescriptor.Methods().ByName("Import") -) - // CollideServiceClient is a client for the connect.collide.v1.CollideService service. type CollideServiceClient interface { Import(context.Context, *connect.Request[v1.ImportRequest]) (*connect.Response[v1.ImportResponse], error) @@ -71,11 +65,12 @@ type CollideServiceClient interface { // http://api.acme.com or https://acme.com/grpc). func NewCollideServiceClient(httpClient connect.HTTPClient, baseURL string, opts ...connect.ClientOption) CollideServiceClient { baseURL = strings.TrimRight(baseURL, "/") + collideServiceMethods := v1.File_connect_collide_v1_collide_proto.Services().ByName("CollideService").Methods() return &collideServiceClient{ _import: connect.NewClient[v1.ImportRequest, v1.ImportResponse]( httpClient, baseURL+CollideServiceImportProcedure, - connect.WithSchema(collideServiceImportMethodDescriptor), + connect.WithSchema(collideServiceMethods.ByName("Import")), connect.WithClientOptions(opts...), ), } @@ -102,10 +97,11 @@ type CollideServiceHandler interface { // By default, handlers support the Connect, gRPC, and gRPC-Web protocols with the binary Protobuf // and JSON codecs. They also support gzip compression. func NewCollideServiceHandler(svc CollideServiceHandler, opts ...connect.HandlerOption) (string, http.Handler) { + collideServiceMethods := v1.File_connect_collide_v1_collide_proto.Services().ByName("CollideService").Methods() collideServiceImportHandler := connect.NewUnaryHandler( CollideServiceImportProcedure, svc.Import, - connect.WithSchema(collideServiceImportMethodDescriptor), + connect.WithSchema(collideServiceMethods.ByName("Import")), connect.WithHandlerOptions(opts...), ) return "/connect.collide.v1.CollideService/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { diff --git a/internal/gen/connect/import/v1/importv1connect/import.connect.go b/internal/gen/connect/import/v1/importv1connect/import.connect.go index 8a9f2b8d..8ed25687 100644 --- a/internal/gen/connect/import/v1/importv1connect/import.connect.go +++ b/internal/gen/connect/import/v1/importv1connect/import.connect.go @@ -20,7 +20,7 @@ package importv1connect import ( connect "connectrpc.com/connect" - v1 "connectrpc.com/connect/internal/gen/connect/import/v1" + _ "connectrpc.com/connect/internal/gen/connect/import/v1" http "net/http" ) @@ -36,11 +36,6 @@ const ( ImportServiceName = "connect.import.v1.ImportService" ) -// These variables are the protoreflect.Descriptor objects for the RPCs defined in this package. -var ( - importServiceServiceDescriptor = v1.File_connect_import_v1_import_proto.Services().ByName("ImportService") -) - // ImportServiceClient is a client for the connect.import.v1.ImportService service. type ImportServiceClient interface { } diff --git a/internal/gen/connect/ping/v1/pingv1connect/ping.connect.go b/internal/gen/connect/ping/v1/pingv1connect/ping.connect.go index aa4b917c..f18400b3 100644 --- a/internal/gen/connect/ping/v1/pingv1connect/ping.connect.go +++ b/internal/gen/connect/ping/v1/pingv1connect/ping.connect.go @@ -64,16 +64,6 @@ const ( PingServiceCumSumProcedure = "/connect.ping.v1.PingService/CumSum" ) -// These variables are the protoreflect.Descriptor objects for the RPCs defined in this package. -var ( - pingServiceServiceDescriptor = v1.File_connect_ping_v1_ping_proto.Services().ByName("PingService") - pingServicePingMethodDescriptor = pingServiceServiceDescriptor.Methods().ByName("Ping") - pingServiceFailMethodDescriptor = pingServiceServiceDescriptor.Methods().ByName("Fail") - pingServiceSumMethodDescriptor = pingServiceServiceDescriptor.Methods().ByName("Sum") - pingServiceCountUpMethodDescriptor = pingServiceServiceDescriptor.Methods().ByName("CountUp") - pingServiceCumSumMethodDescriptor = pingServiceServiceDescriptor.Methods().ByName("CumSum") -) - // PingServiceClient is a client for the connect.ping.v1.PingService service. type PingServiceClient interface { // Ping sends a ping to the server to determine if it's reachable. @@ -97,36 +87,37 @@ type PingServiceClient interface { // http://api.acme.com or https://acme.com/grpc). func NewPingServiceClient(httpClient connect.HTTPClient, baseURL string, opts ...connect.ClientOption) PingServiceClient { baseURL = strings.TrimRight(baseURL, "/") + pingServiceMethods := v1.File_connect_ping_v1_ping_proto.Services().ByName("PingService").Methods() return &pingServiceClient{ ping: connect.NewClient[v1.PingRequest, v1.PingResponse]( httpClient, baseURL+PingServicePingProcedure, - connect.WithSchema(pingServicePingMethodDescriptor), + connect.WithSchema(pingServiceMethods.ByName("Ping")), connect.WithIdempotency(connect.IdempotencyNoSideEffects), connect.WithClientOptions(opts...), ), fail: connect.NewClient[v1.FailRequest, v1.FailResponse]( httpClient, baseURL+PingServiceFailProcedure, - connect.WithSchema(pingServiceFailMethodDescriptor), + connect.WithSchema(pingServiceMethods.ByName("Fail")), connect.WithClientOptions(opts...), ), sum: connect.NewClient[v1.SumRequest, v1.SumResponse]( httpClient, baseURL+PingServiceSumProcedure, - connect.WithSchema(pingServiceSumMethodDescriptor), + connect.WithSchema(pingServiceMethods.ByName("Sum")), connect.WithClientOptions(opts...), ), countUp: connect.NewClient[v1.CountUpRequest, v1.CountUpResponse]( httpClient, baseURL+PingServiceCountUpProcedure, - connect.WithSchema(pingServiceCountUpMethodDescriptor), + connect.WithSchema(pingServiceMethods.ByName("CountUp")), connect.WithClientOptions(opts...), ), cumSum: connect.NewClient[v1.CumSumRequest, v1.CumSumResponse]( httpClient, baseURL+PingServiceCumSumProcedure, - connect.WithSchema(pingServiceCumSumMethodDescriptor), + connect.WithSchema(pingServiceMethods.ByName("CumSum")), connect.WithClientOptions(opts...), ), } @@ -186,35 +177,36 @@ type PingServiceHandler interface { // By default, handlers support the Connect, gRPC, and gRPC-Web protocols with the binary Protobuf // and JSON codecs. They also support gzip compression. func NewPingServiceHandler(svc PingServiceHandler, opts ...connect.HandlerOption) (string, http.Handler) { + pingServiceMethods := v1.File_connect_ping_v1_ping_proto.Services().ByName("PingService").Methods() pingServicePingHandler := connect.NewUnaryHandler( PingServicePingProcedure, svc.Ping, - connect.WithSchema(pingServicePingMethodDescriptor), + connect.WithSchema(pingServiceMethods.ByName("Ping")), connect.WithIdempotency(connect.IdempotencyNoSideEffects), connect.WithHandlerOptions(opts...), ) pingServiceFailHandler := connect.NewUnaryHandler( PingServiceFailProcedure, svc.Fail, - connect.WithSchema(pingServiceFailMethodDescriptor), + connect.WithSchema(pingServiceMethods.ByName("Fail")), connect.WithHandlerOptions(opts...), ) pingServiceSumHandler := connect.NewClientStreamHandler( PingServiceSumProcedure, svc.Sum, - connect.WithSchema(pingServiceSumMethodDescriptor), + connect.WithSchema(pingServiceMethods.ByName("Sum")), connect.WithHandlerOptions(opts...), ) pingServiceCountUpHandler := connect.NewServerStreamHandler( PingServiceCountUpProcedure, svc.CountUp, - connect.WithSchema(pingServiceCountUpMethodDescriptor), + connect.WithSchema(pingServiceMethods.ByName("CountUp")), connect.WithHandlerOptions(opts...), ) pingServiceCumSumHandler := connect.NewBidiStreamHandler( PingServiceCumSumProcedure, svc.CumSum, - connect.WithSchema(pingServiceCumSumMethodDescriptor), + connect.WithSchema(pingServiceMethods.ByName("CumSum")), connect.WithHandlerOptions(opts...), ) return "/connect.ping.v1.PingService/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {