Skip to content

Commit

Permalink
Generator -> Generators
Browse files Browse the repository at this point in the history
Signed-off-by: Kazuyoshi Kato <katokazu@amazon.com>

fix

fix
  • Loading branch information
kzys committed Sep 23, 2021
1 parent 9ff188b commit d0ca12b
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Protobuild.toml
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
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
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{
Names: []string{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.Names = []string{override.Generator}
if len(override.Generators) > 0 {
protoc.Names = override.Generators
}

if override.Plugins != nil {
Expand Down
12 changes: 6 additions & 6 deletions protoc.go
Expand Up @@ -31,15 +31,15 @@ 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}}
{{- range $index, $name := .Names }} --{{- $name -}}_out=
{{- if $.Plugins}}plugins={{- range $index, $plugin := $.Plugins -}}
{{- if $index}}+{{end}}
{{- $plugin}}
{{- end -}},{{- end -}}
import_path={{$.ImportPath}}
{{- end -}}
,{{- end -}}
{{- end -}}import_path={{.ImportPath}}
{{- range $proto, $gopkg := .PackageMap -}},M
{{- $proto}}={{$gopkg -}}
{{- end -}}
Expand Down
39 changes: 32 additions & 7 deletions protoc_test.go
Expand Up @@ -19,14 +19,39 @@ package main
import "testing"

func TestMkcmd(t *testing.T) {
cmd := &protocCmd{Names: []string{"go"}, Plugins: []string{"grpc"}}
s, err := cmd.mkcmd()
if err != nil {
t.Fatalf("err must be nil but %+v", err)
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

expected := "protoc -I --go_out=plugins=grpc,import_path=:"
if s != expected {
t.Fatalf(`s must be %q, but %q`, expected, s)
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 d0ca12b

Please sign in to comment.