Skip to content

Commit c0bb495

Browse files
committed
feat(cmd/gen): support mapping _grpc.pb.go files to different Go packages from .pb.go
grpc#8233
1 parent 732f3f3 commit c0bb495

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

cmd/protoc-gen-go-grpc/grpc.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,16 @@ const fileDescriptorProtoPackageFieldNumber = 2
132132
const fileDescriptorProtoSyntaxFieldNumber = 12
133133

134134
// generateFile generates a _grpc.pb.go file containing gRPC service definitions.
135-
func generateFile(gen *protogen.Plugin, file *protogen.File) *protogen.GeneratedFile {
135+
func generateFile(gen *protogen.Plugin, mapping map[string]string, file *protogen.File) *protogen.GeneratedFile {
136136
if len(file.Services) == 0 {
137137
return nil
138138
}
139139
filename := file.GeneratedFilenamePrefix + "_grpc.pb.go"
140-
g := gen.NewGeneratedFile(filename, file.GoImportPath)
140+
path := file.GoImportPath
141+
if n, ok := mapping[file.Desc.Path()]; ok {
142+
path = protogen.GoImportPath(n)
143+
}
144+
g := gen.NewGeneratedFile(filename, path)
141145
// Attach all comments associated with the syntax field.
142146
genLeadingComments(g, file.Desc.SourceLocations().ByPath(protoreflect.SourcePath{fileDescriptorProtoSyntaxFieldNumber}))
143147
g.P("// Code generated by protoc-gen-go-grpc. DO NOT EDIT.")

cmd/protoc-gen-go-grpc/main.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ package main
3636
import (
3737
"flag"
3838
"fmt"
39+
"strings"
3940

4041
"google.golang.org/protobuf/compiler/protogen"
4142
"google.golang.org/protobuf/types/descriptorpb"
@@ -59,6 +60,9 @@ func main() {
5960
requireUnimplemented = flags.Bool("require_unimplemented_servers", true, "set to false to match legacy behavior")
6061
useGenericStreams = flags.Bool("use_generic_streams_experimental", true, "set to true to use generic types for streaming client and server objects; this flag is EXPERIMENTAL and may be changed or removed in a future release")
6162

63+
var grpcMapping mappingFlag
64+
flags.Var(&grpcMapping, "grpc_mapping", "change proto file go_package option, should be like xxx.proto=go_package. Multiple OK")
65+
6266
protogen.Options{
6367
ParamFunc: flags.Set,
6468
}.Run(func(gen *protogen.Plugin) error {
@@ -69,8 +73,29 @@ func main() {
6973
if !f.Generate {
7074
continue
7175
}
72-
generateFile(gen, f)
76+
generateFile(gen, grpcMapping, f)
7377
}
7478
return nil
7579
})
7680
}
81+
82+
type mappingFlag map[string]string
83+
84+
func (a *mappingFlag) String() string {
85+
return fmt.Sprintf("%v", *a)
86+
}
87+
88+
func (a *mappingFlag) Set(value string) error {
89+
k, v, ok := strings.Cut(value, "=")
90+
if !ok {
91+
return fmt.Errorf("invalid grpc_mapping: %v", value)
92+
}
93+
if k == "" || v == "" {
94+
return fmt.Errorf("invalid grpc_mapping: %v", value)
95+
}
96+
if *a == nil {
97+
*a = make(mappingFlag)
98+
}
99+
(*a)[k] = v
100+
return nil
101+
}

0 commit comments

Comments
 (0)