Skip to content

Commit a79e9fb

Browse files
committed
feat(cmd/gen): add flag use_separate_service_package
#8233
1 parent 080f956 commit a79e9fb

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,13 @@ func generateFile(gen *protogen.Plugin, file *protogen.File) *protogen.Generated
136136
if len(file.Services) == 0 {
137137
return nil
138138
}
139+
140+
importPath := file.GoImportPath
141+
if *useSeparateServicePackage {
142+
importPath = file.GoImportPath + "_" + protogen.GoImportPath("grpc")
143+
}
139144
filename := file.GeneratedFilenamePrefix + "_grpc.pb.go"
140-
g := gen.NewGeneratedFile(filename, file.GoImportPath)
145+
g := gen.NewGeneratedFile(filename, importPath)
141146
// Attach all comments associated with the syntax field.
142147
genLeadingComments(g, file.Desc.SourceLocations().ByPath(protoreflect.SourcePath{fileDescriptorProtoSyntaxFieldNumber}))
143148
g.P("// Code generated by protoc-gen-go-grpc. DO NOT EDIT.")

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,11 @@ import (
4444

4545
const version = "1.5.1"
4646

47-
var requireUnimplemented *bool
48-
var useGenericStreams *bool
47+
var (
48+
requireUnimplemented *bool
49+
useGenericStreams *bool
50+
useSeparateServicePackage *bool
51+
)
4952

5053
func main() {
5154
showVersion := flag.Bool("version", false, "print the version and exit")
@@ -58,6 +61,7 @@ func main() {
5861
var flags flag.FlagSet
5962
requireUnimplemented = flags.Bool("require_unimplemented_servers", true, "set to false to match legacy behavior")
6063
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")
64+
useSeparateServicePackage = flags.Bool("use_separate_service_package", false, "set to true to use separate service package, service package will import protobuf package")
6165

6266
protogen.Options{
6367
ParamFunc: flags.Set,

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,20 @@ if [[ -n "${DIFF}" ]]; then
4747
exit 1
4848
fi
4949

50+
# test use separate service package
51+
protoc \
52+
--go-grpc_out="${TEMPDIR}" \
53+
--go-grpc_opt=paths=source_relative,use_separate_service_package=true \
54+
"examples/route_guide/routeguide/route_guide.proto"
55+
56+
# grep is piped to [[ $? == 1 ]] to avoid exiting on grep but exit on error
57+
# (like if the file was not found). See man grep for more info.
58+
GREP=$(grep 'routeguide "google.golang.org/grpc/examples/route_guide/routeguide"' \
59+
"${TEMPDIR}/examples/route_guide/routeguide/route_guide_grpc.pb.go" || [[ $? == 1 ]])
60+
61+
if [[ -z "${GREP}" ]]; then
62+
echo -e "ERROR: Generated file not import routeguide package\n"
63+
exit 1
64+
fi
65+
5066
echo SUCCESS

0 commit comments

Comments
 (0)