Skip to content

Commit

Permalink
chore(internal/gapicgen): support stub generation in google-cloud-go (#…
Browse files Browse the repository at this point in the history
…6150)

Enables generation of protobuf/gRPC stubs in google-cloud-go GAPIC subdirectory. The constructed importpath is mapped using the protobuf/gapic option `-M` to remap the `go_package` in all proto files designated as input to be generated.

Important: Setting `StubsDir` in the microgen configuration **does not** stop generation in genproto.
  • Loading branch information
noahdietz committed Jun 16, 2022
1 parent c963be3 commit e2d14a0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
6 changes: 6 additions & 0 deletions internal/gapicgen/generator/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ type MicrogenConfig struct {
// Transports is a list of Transports to generate a client for. Acceptable
// values are 'grpc' and 'rest'
Transports []string

// StubsDir indicates that the protobuf/gRPC stubs should be generated
// in the GAPIC module by replacing the go_package option with the value of
// ImportPath plus the specified suffix separated by a "/", and using the
// same Pkg value.
StubsDir string
}

var MicrogenGapicConfigs = []*MicrogenConfig{
Expand Down
16 changes: 16 additions & 0 deletions internal/gapicgen/generator/gapics.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,22 @@ func (g *GapicGenerator) microgen(conf *MicrogenConfig) error {
if conf.Pkg == "compute" {
args = append(args, "--go_gapic_opt", "diregapic")
}
if conf.StubsDir != "" {
// Enable protobuf/gRPC generation in the google-cloud-go directory.
args = append(args, "--go_out=plugins=grpc:"+g.googleCloudDir)

// For each file to be generated i.e. each file in the proto package,
// override the go_package option. Applied to both the protobuf/gRPC
// generated code, and to notify the GAPIC generator of the new
// import path used to reference those stubs.
stubPkg := filepath.Join(conf.ImportPath, conf.StubsDir)
for _, f := range protoFiles {
f = strings.TrimPrefix(f, g.googleapisDir+"/")
rerouteGoPkg := fmt.Sprintf("M%s=%s;%s", f, stubPkg, conf.Pkg)
args = append(args, "--go_opt="+rerouteGoPkg, "--go_gapic_opt="+rerouteGoPkg)
}
}

args = append(args, protoFiles...)
c := execv.Command("protoc", args...)
c.Dir = g.googleapisDir
Expand Down

0 comments on commit e2d14a0

Please sign in to comment.