Skip to content

Commit

Permalink
Support multiple generators
Browse files Browse the repository at this point in the history
The latest version of protoc-gen-go doesn't support gRPC. Supporting
multiple generators is the way to support gRPC going forward.

Signed-off-by: Kazuyoshi Kato <katokazu@amazon.com>
  • Loading branch information
kzys committed Sep 23, 2021
1 parent 6b023c6 commit 764614f
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Protobuild.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version = "unstable"
# Generator defines which generator to go. The default is "go". This will be
# formatted into the --xxx_out flag provided to protoc. Below, we have an
# example that selects the ctrd vanity binary.
# generator = "go"
# generators = ["go"]

# Plugins allows one to specify one or more plugins for use in generation.
#
Expand Down
20 changes: 12 additions & 8 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,14 @@ import (
const configVersion = "unstable"

type config struct {
Version string
Generator string
Plugins []string
Includes struct {
Version string
Generators []string

// Plugins will be deprecated. It has to be per-Generator setting, but neither protoc-gen-go nor protoc-gen-go-grpc
// don't support Plugins. So the refactoring is not worth to do.
Plugins []string

Includes struct {
Before []string
Vendored []string
Packages []string
Expand All @@ -40,9 +44,9 @@ type config struct {
Packages map[string]string

Overrides []struct {
Prefixes []string
Generator string
Plugins *[]string
Prefixes []string
Generators []string
Plugins *[]string

// TODO(stevvooe): We could probably support overriding of includes and
// package maps, but they don't seem to be as useful. Likely,
Expand All @@ -59,7 +63,7 @@ type config struct {

func newDefaultConfig() config {
return config{
Generator: "go",
Generators: []string{"go"},
Includes: struct {
Before []string
Vendored []string
Expand Down
12 changes: 6 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ func main() {

// Index overrides by target import path
overrides := map[string]struct {
Prefixes []string
Generator string
Plugins *[]string
Prefixes []string
Generators []string
Plugins *[]string
}{}
for _, override := range c.Overrides {
for _, prefix := range override.Prefixes {
Expand Down Expand Up @@ -166,7 +166,7 @@ func main() {
includes = append(includes, c.Includes.After...)

protoc := protocCmd{
Name: c.Generator,
Names: c.Generators,
ImportPath: pkg.GoImportPath,
PackageMap: c.Packages,
Plugins: c.Plugins,
Expand All @@ -182,8 +182,8 @@ func main() {

if override, ok := overrides[importDirPath]; ok {
// selectively apply the overrides to the protoc structure.
if override.Generator != "" {
protoc.Name = override.Generator
if len(override.Generators) > 0 {
protoc.Names = override.Generators
}

if override.Plugins != nil {
Expand Down
16 changes: 10 additions & 6 deletions protoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,16 @@ var (
{{if $index}}` + string(filepath.ListSeparator) + `{{end -}}
{{.}}
{{- end -}}
{{- if .Descriptors}} --include_imports --descriptor_set_out={{.Descriptors}}{{- end }} --
{{- .Name -}}_out={{if .Plugins}}plugins={{- range $index, $plugin := .Plugins -}}
{{- if $index}}+{{end}}
{{- $plugin}}
{{- 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 -}}
,{{- end -}}import_path={{.ImportPath}}
{{- range $proto, $gopkg := .PackageMap -}},M
{{- $proto}}={{$gopkg -}}
{{- end -}}
Expand All @@ -46,7 +50,7 @@ var (

// protocParams defines inputs to a protoc command string.
type protocCmd struct {
Name string // backend name
Names []string
Includes []string
Plugins []string
Descriptors string
Expand Down
57 changes: 57 additions & 0 deletions protoc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
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 main

import "testing"

func TestMkcmd(t *testing.T) {
testcases := []struct {
name string
cmd protocCmd
expected string
}{
{
name: "basic",
cmd: protocCmd{Names: []string{"go"}},
expected: "protoc -I --go_out=import_path=:",
},
{
name: "plugin",
cmd: protocCmd{Names: []string{"go"}, Plugins: []string{"grpc"}},
expected: "protoc -I --go_out=plugins=grpc,import_path=:",
},
{
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=:",
},
}
for _, tc := range testcases {
t.Run(tc.name, func(subtest *testing.T) {
cmd := &tc.cmd

s, err := cmd.mkcmd()
if err != nil {
subtest.Fatalf("err must be nil but %+v", err)
}

if s != tc.expected {
subtest.Fatalf(`s must be %q, but %q`, tc.expected, s)
}
})
}
}

0 comments on commit 764614f

Please sign in to comment.