From a4051839cc2b0d3b8bf50ea719eb3c3aa5ffedd6 Mon Sep 17 00:00:00 2001 From: Kazuyoshi Kato Date: Wed, 20 Oct 2021 14:36:06 -0700 Subject: [PATCH 1/2] Remove plugins and import_path if version=2 This change introduces version=2 that supports latest protoc, protoc-gen-go and protoc-gen-go-grpc. Signed-off-by: Kazuyoshi Kato --- README.md | 13 ++++++++++++- config.go | 6 ------ main.go | 23 +++++++++++++++++++++++ protoc.go | 45 +++++++++++++++++++++++++++++++++++---------- protoc_test.go | 48 +++++++++++++++++++++++++++++++++--------------- 5 files changed, 103 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index 4b9b499..4604e55 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,18 @@ protobufs. To get started with a project, you must do the following: go list ./... | grep -v vendor | xargs protobuild ``` -TODO(stevvooe): Make this better. +## Version Compatibility + +Originally protoc-gen-go was supporting gRPC through its plugins mechanism. +[However gRPC support is later extracted as protoc-gen-go-gprc binary](https://github.com/protocolbuffers/protobuf-go/releases/tag/v1.20.0#user-content-v1.20-grpc-support). + +To use protoc-gen-go and protoc-gen-go-grpc. Please specify `version = "2"` and +both code generators instead of `plugins` like below. + +``` +version = "2" +generators = ["go", "go-grpc"] +``` ## Project details diff --git a/config.go b/config.go index 28d4e49..c949a43 100644 --- a/config.go +++ b/config.go @@ -24,8 +24,6 @@ import ( "github.com/pelletier/go-toml" ) -const configVersion = "unstable" - type config struct { Version string Generators []string @@ -97,10 +95,6 @@ func readConfigFrom(p []byte) (config, error) { log.Fatalln(err) } - if c.Version != configVersion { - return config{}, fmt.Errorf("unknown file version %v; please upgrade to %v", c.Version, configVersion) - } - if c.Generator != "" { if len(c.Generators) > 0 { return config{}, fmt.Errorf( diff --git a/main.go b/main.go index 91fc918..77066bd 100644 --- a/main.go +++ b/main.go @@ -27,6 +27,7 @@ import ( "os/exec" "path" "path/filepath" + "strconv" "strings" "syscall" @@ -47,6 +48,22 @@ func init() { flag.BoolVar(&quiet, "quiet", false, "suppress verbose output") } +func parseVersion(s string) (int, error) { + if s == "unstable" { + return 0, nil + } + + v, err := strconv.Atoi(s) + if err != nil { + return 0, fmt.Errorf("unknown file version %q: %w", s, err) + } + + if v < 1 || v > 2 { + return 0, fmt.Errorf(`unknown file version %q; valid versions are "unstable", "1" and "2"`, s) + } + return v, nil +} + func main() { flag.Parse() @@ -55,6 +72,11 @@ func main() { log.Fatalln(err) } + version, err := parseVersion(c.Version) + if err != nil { + log.Fatalln(err) + } + pkgInfos, err := goPkgInfo(flag.Args()...) if err != nil { log.Fatalln(err) @@ -174,6 +196,7 @@ func main() { Files: pkg.ProtoFiles, OutputDir: outputDir, Includes: includes, + Version: version, } importDirPath, err := filepath.Rel(outputDir, pkg.Dir) diff --git a/protoc.go b/protoc.go index 842b0e2..a42950e 100644 --- a/protoc.go +++ b/protoc.go @@ -18,9 +18,11 @@ package main import ( "bytes" + "fmt" "os" "os/exec" "path/filepath" + "strings" "text/template" ) @@ -32,18 +34,16 @@ var ( {{- end -}} {{- if .Descriptors}} --include_imports --descriptor_set_out={{.Descriptors}}{{- end -}} - {{- range $index, $name := .Names }} --{{- $name -}}_out= - {{- if $.Plugins}}plugins={{- range $index, $plugin := $.Plugins -}} - {{- if $index}}+{{end}} - {{- $plugin}} - {{- end -}},{{- end -}} - import_path={{$.ImportPath}} - {{- end -}} + {{ if lt .Version 2 }} + {{- range $index, $name := .Names }} --{{- $name -}}_out={{- $.GoOutV1 }}{{- end -}} + {{- else -}} + {{- range $index, $name := .Names }} --{{- $name -}}_out={{- $.GoOutV2 }}{{- end -}} - {{- range $proto, $gopkg := .PackageMap -}},M - {{- $proto}}={{$gopkg -}} + {{- range $proto, $gopkg := .PackageMap }} --go_opt=M + {{- $proto}}={{$gopkg -}} + {{- end -}} {{- end -}} - :{{- .OutputDir }} + {{- range .Files}} {{.}}{{end -}} `)) ) @@ -58,6 +58,8 @@ type protocCmd struct { PackageMap map[string]string Files []string OutputDir string + // Version is Protobuild's version. + Version int } func (p *protocCmd) mkcmd() (string, error) { @@ -69,6 +71,29 @@ func (p *protocCmd) mkcmd() (string, error) { return buf.String(), nil } +// GoOutV1 returns the parameter for --go_out= for protoc-gen-go < 1.4.0. +// Note that plugins and import_path are no longer supported by +// newer protoc-gen-go versions. +func (p *protocCmd) GoOutV1() string { + var result string + if len(p.Plugins) > 0 { + result += "plugins=" + strings.Join(p.Plugins, "+") + "," + } + result += "import_path=" + p.ImportPath + + for proto, pkg := range p.PackageMap { + result += fmt.Sprintf(",M%s=%s", proto, pkg) + } + result += ":" + p.OutputDir + + return result +} + +// GoOutV2 returns the parameter for --go_out= for protoc-gen-go >= 1.4.0. +func (p *protocCmd) GoOutV2() string { + return p.OutputDir +} + func (p *protocCmd) run() error { arg, err := p.mkcmd() if err != nil { diff --git a/protoc_test.go b/protoc_test.go index c8e86eb..d703592 100644 --- a/protoc_test.go +++ b/protoc_test.go @@ -20,37 +20,55 @@ import "testing" func TestMkcmd(t *testing.T) { testcases := []struct { - name string - cmd protocCmd - expected string + name string + cmd protocCmd + expectedV1 string + expectedV2 string }{ { - name: "basic", - cmd: protocCmd{Names: []string{"go"}}, - expected: "protoc -I --go_out=import_path=:", + name: "basic", + cmd: protocCmd{Names: []string{"go"}}, + expectedV1: "protoc -I --go_out=import_path=:", + expectedV2: "protoc -I --go_out=", }, { - name: "plugin", - cmd: protocCmd{Names: []string{"go"}, Plugins: []string{"grpc"}}, - expected: "protoc -I --go_out=plugins=grpc,import_path=:", + name: "plugin", + cmd: protocCmd{Names: []string{"go"}, Plugins: []string{"grpc"}}, + expectedV1: "protoc -I --go_out=plugins=grpc,import_path=:", + expectedV2: "protoc -I --go_out=", }, { - name: "use protoc-gen-go-grpc instead of plugins", - cmd: protocCmd{Names: []string{"go", "go-grpc"}}, - expected: "protoc -I --go_out=import_path= --go-grpc_out=import_path=:", + name: "use protoc-gen-go-grpc instead of plugins", + cmd: protocCmd{Names: []string{"go", "go-grpc"}}, + expectedV1: "protoc -I --go_out=import_path=: --go-grpc_out=import_path=:", + expectedV2: "protoc -I --go_out= --go-grpc_out=", }, } for _, tc := range testcases { - t.Run(tc.name, func(t *testing.T) { + t.Run(tc.name+"V1", func(t *testing.T) { cmd := &tc.cmd + cmd.Version = 1 s, err := cmd.mkcmd() if err != nil { t.Fatalf("err must be nil but %+v", err) } - if s != tc.expected { - t.Fatalf(`s must be %q, but %q`, tc.expected, s) + if s != tc.expectedV1 { + t.Fatalf(`s must be %q, but %q`, tc.expectedV1, s) + } + }) + t.Run(tc.name+"V2", func(t *testing.T) { + cmd := &tc.cmd + cmd.Version = 2 + + s, err := cmd.mkcmd() + if err != nil { + t.Fatalf("err must be nil but %+v", err) + } + + if s != tc.expectedV2 { + t.Fatalf(`s must be %q, but %q`, tc.expectedV2, s) } }) } From 3b9c4c7191eec9af518cfcaeec9ed1e044179ca6 Mon Sep 17 00:00:00 2001 From: Kazuyoshi Kato Date: Wed, 20 Oct 2021 14:49:14 -0700 Subject: [PATCH 2/2] Test Protobuild with multiple protoc-gen-go versions This change extends GitHub Actions to test Protobuild against multiple protoc-gen-go versions. Signed-off-by: Kazuyoshi Kato --- .github/workflows/ci.yml | 93 ++++++++++++++++++--- examples/v2/doc.go | 21 +++++ examples/v2/foo.pb.go | 165 +++++++++++++++++++++++++++++++++++++ examples/v2/foo.proto | 16 ++++ examples/v2/foo_grpc.pb.go | 102 +++++++++++++++++++++++ examples/v2/next.pb.txt | 77 +++++++++++++++++ go.mod | 2 +- v2.toml | 10 +++ 8 files changed, 475 insertions(+), 11 deletions(-) create mode 100644 examples/v2/doc.go create mode 100644 examples/v2/foo.pb.go create mode 100644 examples/v2/foo.proto create mode 100644 examples/v2/foo_grpc.pb.go create mode 100755 examples/v2/next.pb.txt create mode 100644 v2.toml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index de0695d..b9dd7d4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -7,9 +7,40 @@ on: branches: [ main ] jobs: + test: + name: Test + runs-on: ubuntu-20.04 + timeout-minutes: 5 + steps: + - name: Check out code + uses: actions/checkout@v2 + with: + path: src/github.com/containerd/protobuild + fetch-depth: 25 + + - name: Project checks + uses: containerd/project-checks@v1 + with: + working-directory: src/github.com/containerd/protobuild + + - name: Setup environment + shell: bash + run: | + echo "GOPATH=${{ github.workspace }}" >> $GITHUB_ENV + echo "${{ github.workspace }}/bin" >> $GITHUB_PATH + + - name: Check out code + uses: actions/checkout@v2 + with: + path: src/github.com/containerd/protobuild + + - name: Test + working-directory: src/github.com/containerd/protobuild + run: | + go test - build: - name: Protobuild CI + v1: + name: Run with protoc-gen-go v1.3.5 runs-on: ubuntu-20.04 timeout-minutes: 5 steps: @@ -30,12 +61,6 @@ jobs: uses: actions/checkout@v2 with: path: src/github.com/containerd/protobuild - fetch-depth: 25 - - - name: Project checks - uses: containerd/project-checks@v1 - with: - working-directory: src/github.com/containerd/protobuild - name: Build working-directory: src/github.com/containerd/protobuild @@ -58,7 +83,55 @@ jobs: - name: Run protobuild to see all committed auto-generated files can be generated as is working-directory: src/github.com/containerd/protobuild run: | - find examples/ -name '*.pb.*' | xargs rm + rm examples/foo/*.pb.* examples/bar/*.pb.* examples/nogrpc/*.pb.* + export PATH=$(go env GOBIN):$PATH + ./protobuild github.com/containerd/protobuild/examples/bar github.com/containerd/protobuild/examples/foo github.com/containerd/protobuild/examples/nogrpc + git diff --exit-code + + v2: + name: Run with protoc-gen-go v1.26 + runs-on: ubuntu-20.04 + timeout-minutes: 5 + steps: + + - name: Set up Go + uses: actions/setup-go@v2 + with: + go-version: 1.16.x + id: go + + - name: Setup environment + shell: bash + run: | + echo "GOPATH=${{ github.workspace }}" >> $GITHUB_ENV + echo "${{ github.workspace }}/bin" >> $GITHUB_PATH + + - name: Check out code + uses: actions/checkout@v2 + with: + path: src/github.com/containerd/protobuild + + - name: Build + working-directory: src/github.com/containerd/protobuild + run: | + go build . + + - name: Install protoc + run: | + curl -LO https://github.com/protocolbuffers/protobuf/releases/download/v3.18.1/protoc-3.18.1-linux-x86_64.zip + sudo unzip -x protoc-3.18.1-linux-x86_64.zip -d /usr/local + sudo chmod -R go+rX /usr/local/include + sudo chmod go+x /usr/local/bin/protoc + + - name: Install protoc-gen-go and protoc-gen-go-grpc + run: | + go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.26 + go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.1 + + - name: Run protobuild to see all committed auto-generated files can be generated as is + working-directory: src/github.com/containerd/protobuild + run: | + rm examples/v2/*.pb.* export PATH=$(go env GOBIN):$PATH - go list ./... | grep -v vendor | xargs ./protobuild + ./protobuild -f v2.toml github.com/containerd/protobuild/examples/v2 git diff --exit-code diff --git a/examples/v2/doc.go b/examples/v2/doc.go new file mode 100644 index 0000000..f4902a7 --- /dev/null +++ b/examples/v2/doc.go @@ -0,0 +1,21 @@ +/* + Copyright The containerd 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 foo demonstrates a protobuf package. +// +// We include a doc.go file in here to let protobuild know to place a +// Go/Protobuf package. +package v2 diff --git a/examples/v2/foo.pb.go b/examples/v2/foo.pb.go new file mode 100644 index 0000000..bd27971 --- /dev/null +++ b/examples/v2/foo.pb.go @@ -0,0 +1,165 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.26.0 +// protoc v3.18.1 +// source: github.com/containerd/protobuild/examples/v2/foo.proto + +package v2 + +import ( + internal "github.com/containerd/protobuild/internal" + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + timestamppb "google.golang.org/protobuf/types/known/timestamppb" + 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 DoRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Timestamp *timestamppb.Timestamp `protobuf:"bytes,3,opt,name=timestamp,proto3" json:"timestamp,omitempty"` +} + +func (x *DoRequest) Reset() { + *x = DoRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_github_com_containerd_protobuild_examples_v2_foo_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *DoRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*DoRequest) ProtoMessage() {} + +func (x *DoRequest) ProtoReflect() protoreflect.Message { + mi := &file_github_com_containerd_protobuild_examples_v2_foo_proto_msgTypes[0] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use DoRequest.ProtoReflect.Descriptor instead. +func (*DoRequest) Descriptor() ([]byte, []int) { + return file_github_com_containerd_protobuild_examples_v2_foo_proto_rawDescGZIP(), []int{0} +} + +func (x *DoRequest) GetTimestamp() *timestamppb.Timestamp { + if x != nil { + return x.Timestamp + } + return nil +} + +var File_github_com_containerd_protobuild_examples_v2_foo_proto protoreflect.FileDescriptor + +var file_github_com_containerd_protobuild_examples_v2_foo_proto_rawDesc = []byte{ + 0x0a, 0x36, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x63, 0x6f, 0x6e, + 0x74, 0x61, 0x69, 0x6e, 0x65, 0x72, 0x64, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x69, + 0x6c, 0x64, 0x2f, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x73, 0x2f, 0x76, 0x32, 0x2f, 0x66, + 0x6f, 0x6f, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x15, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, + 0x75, 0x69, 0x6c, 0x64, 0x2e, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x76, 0x32, 0x1a, + 0x1b, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, + 0x2f, 0x65, 0x6d, 0x70, 0x74, 0x79, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x1a, 0x1f, 0x67, 0x6f, + 0x6f, 0x67, 0x6c, 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2f, 0x74, 0x69, + 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x45, 0x0a, + 0x09, 0x44, 0x6f, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x38, 0x0a, 0x09, 0x74, 0x69, + 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x1a, 0x2e, + 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, + 0x54, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x52, 0x09, 0x74, 0x69, 0x6d, 0x65, 0x73, + 0x74, 0x61, 0x6d, 0x70, 0x32, 0x45, 0x0a, 0x03, 0x46, 0x6f, 0x6f, 0x12, 0x3e, 0x0a, 0x02, 0x44, + 0x6f, 0x12, 0x20, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x69, 0x6c, 0x64, 0x2e, 0x65, + 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x76, 0x32, 0x2e, 0x44, 0x6f, 0x52, 0x65, 0x71, 0x75, + 0x65, 0x73, 0x74, 0x1a, 0x16, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, + 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x42, 0x32, 0x5a, 0x30, 0x67, + 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x63, 0x6f, 0x6e, 0x74, 0x61, 0x69, + 0x6e, 0x65, 0x72, 0x64, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x69, 0x6c, 0x64, 0x2f, + 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x73, 0x2f, 0x76, 0x32, 0x2f, 0x3b, 0x76, 0x32, 0x62, + 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_github_com_containerd_protobuild_examples_v2_foo_proto_rawDescOnce sync.Once + file_github_com_containerd_protobuild_examples_v2_foo_proto_rawDescData = file_github_com_containerd_protobuild_examples_v2_foo_proto_rawDesc +) + +func file_github_com_containerd_protobuild_examples_v2_foo_proto_rawDescGZIP() []byte { + file_github_com_containerd_protobuild_examples_v2_foo_proto_rawDescOnce.Do(func() { + file_github_com_containerd_protobuild_examples_v2_foo_proto_rawDescData = protoimpl.X.CompressGZIP(file_github_com_containerd_protobuild_examples_v2_foo_proto_rawDescData) + }) + return file_github_com_containerd_protobuild_examples_v2_foo_proto_rawDescData +} + +var file_github_com_containerd_protobuild_examples_v2_foo_proto_msgTypes = make([]protoimpl.MessageInfo, 1) +var file_github_com_containerd_protobuild_examples_v2_foo_proto_goTypes = []interface{}{ + (*DoRequest)(nil), // 0: protobuild.example.v2.DoRequest + (*timestamppb.Timestamp)(nil), // 1: google.protobuf.Timestamp + (*internal.Empty)(nil), // 2: google.protobuf.Empty +} +var file_github_com_containerd_protobuild_examples_v2_foo_proto_depIdxs = []int32{ + 1, // 0: protobuild.example.v2.DoRequest.timestamp:type_name -> google.protobuf.Timestamp + 0, // 1: protobuild.example.v2.Foo.Do:input_type -> protobuild.example.v2.DoRequest + 2, // 2: protobuild.example.v2.Foo.Do:output_type -> google.protobuf.Empty + 2, // [2:3] is the sub-list for method output_type + 1, // [1:2] is the sub-list for method input_type + 1, // [1:1] is the sub-list for extension type_name + 1, // [1:1] is the sub-list for extension extendee + 0, // [0:1] is the sub-list for field type_name +} + +func init() { file_github_com_containerd_protobuild_examples_v2_foo_proto_init() } +func file_github_com_containerd_protobuild_examples_v2_foo_proto_init() { + if File_github_com_containerd_protobuild_examples_v2_foo_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_github_com_containerd_protobuild_examples_v2_foo_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*DoRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_github_com_containerd_protobuild_examples_v2_foo_proto_rawDesc, + NumEnums: 0, + NumMessages: 1, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_github_com_containerd_protobuild_examples_v2_foo_proto_goTypes, + DependencyIndexes: file_github_com_containerd_protobuild_examples_v2_foo_proto_depIdxs, + MessageInfos: file_github_com_containerd_protobuild_examples_v2_foo_proto_msgTypes, + }.Build() + File_github_com_containerd_protobuild_examples_v2_foo_proto = out.File + file_github_com_containerd_protobuild_examples_v2_foo_proto_rawDesc = nil + file_github_com_containerd_protobuild_examples_v2_foo_proto_goTypes = nil + file_github_com_containerd_protobuild_examples_v2_foo_proto_depIdxs = nil +} diff --git a/examples/v2/foo.proto b/examples/v2/foo.proto new file mode 100644 index 0000000..8dc1a7c --- /dev/null +++ b/examples/v2/foo.proto @@ -0,0 +1,16 @@ +syntax = "proto3"; + +package protobuild.example.v2; + +import "google/protobuf/empty.proto"; +import "google/protobuf/timestamp.proto"; + +option go_package = "github.com/containerd/protobuild/examples/v2/;v2"; + +service Foo { + rpc Do(DoRequest) returns (google.protobuf.Empty); +} + +message DoRequest { + google.protobuf.Timestamp timestamp = 3; +} diff --git a/examples/v2/foo_grpc.pb.go b/examples/v2/foo_grpc.pb.go new file mode 100644 index 0000000..6c04fcf --- /dev/null +++ b/examples/v2/foo_grpc.pb.go @@ -0,0 +1,102 @@ +// Code generated by protoc-gen-go-grpc. DO NOT EDIT. + +package v2 + +import ( + context "context" + grpc "google.golang.org/grpc" + codes "google.golang.org/grpc/codes" + status "google.golang.org/grpc/status" + emptypb "google.golang.org/protobuf/types/known/emptypb" +) + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the grpc package it is being compiled against. +// Requires gRPC-Go v1.32.0 or later. +const _ = grpc.SupportPackageIsVersion7 + +// FooClient is the client API for Foo service. +// +// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream. +type FooClient interface { + Do(ctx context.Context, in *DoRequest, opts ...grpc.CallOption) (*emptypb.Empty, error) +} + +type fooClient struct { + cc grpc.ClientConnInterface +} + +func NewFooClient(cc grpc.ClientConnInterface) FooClient { + return &fooClient{cc} +} + +func (c *fooClient) Do(ctx context.Context, in *DoRequest, opts ...grpc.CallOption) (*emptypb.Empty, error) { + out := new(emptypb.Empty) + err := c.cc.Invoke(ctx, "/protobuild.example.v2.Foo/Do", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +// FooServer is the server API for Foo service. +// All implementations must embed UnimplementedFooServer +// for forward compatibility +type FooServer interface { + Do(context.Context, *DoRequest) (*emptypb.Empty, error) + mustEmbedUnimplementedFooServer() +} + +// UnimplementedFooServer must be embedded to have forward compatible implementations. +type UnimplementedFooServer struct { +} + +func (UnimplementedFooServer) Do(context.Context, *DoRequest) (*emptypb.Empty, error) { + return nil, status.Errorf(codes.Unimplemented, "method Do not implemented") +} +func (UnimplementedFooServer) mustEmbedUnimplementedFooServer() {} + +// UnsafeFooServer may be embedded to opt out of forward compatibility for this service. +// Use of this interface is not recommended, as added methods to FooServer will +// result in compilation errors. +type UnsafeFooServer interface { + mustEmbedUnimplementedFooServer() +} + +func RegisterFooServer(s grpc.ServiceRegistrar, srv FooServer) { + s.RegisterService(&Foo_ServiceDesc, srv) +} + +func _Foo_Do_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(DoRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(FooServer).Do(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/protobuild.example.v2.Foo/Do", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(FooServer).Do(ctx, req.(*DoRequest)) + } + return interceptor(ctx, in, info, handler) +} + +// Foo_ServiceDesc is the grpc.ServiceDesc for Foo service. +// It's only intended for direct use with grpc.RegisterService, +// and not to be introspected or modified (even as a copy) +var Foo_ServiceDesc = grpc.ServiceDesc{ + ServiceName: "protobuild.example.v2.Foo", + HandlerType: (*FooServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "Do", + Handler: _Foo_Do_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, + Metadata: "github.com/containerd/protobuild/examples/v2/foo.proto", +} diff --git a/examples/v2/next.pb.txt b/examples/v2/next.pb.txt new file mode 100755 index 0000000..c5e7633 --- /dev/null +++ b/examples/v2/next.pb.txt @@ -0,0 +1,77 @@ +file { + name: "google/protobuf/empty.proto" + package: "google.protobuf" + message_type { + name: "Empty" + } + options { + java_package: "com.google.protobuf" + java_outer_classname: "EmptyProto" + java_multiple_files: true + go_package: "google.golang.org/protobuf/types/known/emptypb" + cc_enable_arenas: true + objc_class_prefix: "GPB" + csharp_namespace: "Google.Protobuf.WellKnownTypes" + } + syntax: "proto3" +} +file { + name: "google/protobuf/timestamp.proto" + package: "google.protobuf" + message_type { + name: "Timestamp" + field { + name: "seconds" + number: 1 + label: LABEL_OPTIONAL + type: TYPE_INT64 + json_name: "seconds" + } + field { + name: "nanos" + number: 2 + label: LABEL_OPTIONAL + type: TYPE_INT32 + json_name: "nanos" + } + } + options { + java_package: "com.google.protobuf" + java_outer_classname: "TimestampProto" + java_multiple_files: true + go_package: "google.golang.org/protobuf/types/known/timestamppb" + cc_enable_arenas: true + objc_class_prefix: "GPB" + csharp_namespace: "Google.Protobuf.WellKnownTypes" + } + syntax: "proto3" +} +file { + name: "github.com/containerd/protobuild/examples/v2/foo.proto" + package: "protobuild.example.v2" + dependency: "google/protobuf/empty.proto" + dependency: "google/protobuf/timestamp.proto" + message_type { + name: "DoRequest" + field { + name: "timestamp" + number: 3 + label: LABEL_OPTIONAL + type: TYPE_MESSAGE + type_name: ".google.protobuf.Timestamp" + json_name: "timestamp" + } + } + service { + name: "Foo" + method { + name: "Do" + input_type: ".protobuild.example.v2.DoRequest" + output_type: ".google.protobuf.Empty" + } + } + options { + go_package: "github.com/containerd/protobuild/examples/v2/;v2" + } + syntax: "proto3" +} diff --git a/go.mod b/go.mod index e1c3c7e..c7389af 100644 --- a/go.mod +++ b/go.mod @@ -8,5 +8,5 @@ require ( golang.org/x/net v0.0.0-20201021035429-f5854403a974 // indirect golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect google.golang.org/grpc v1.28.1 - google.golang.org/protobuf v1.27.1 // indirect + google.golang.org/protobuf v1.27.1 ) diff --git a/v2.toml b/v2.toml new file mode 100644 index 0000000..dfe0f9a --- /dev/null +++ b/v2.toml @@ -0,0 +1,10 @@ +version = "2" + +generators = ["go", "go-grpc"] + +[packages] +"google/protobuf/empty.proto" = "github.com/containerd/protobuild/internal" + +[[descriptors]] +prefix = "github.com/containerd/protobuild/examples/v2" +target = "examples/v2/next.pb.txt"