Skip to content

Commit

Permalink
Add option to disable pb gen
Browse files Browse the repository at this point in the history
Signed-off-by: Sascha Grunert <[email protected]>
  • Loading branch information
saschagrunert committed Nov 22, 2024
1 parent 4626258 commit 3e0bd28
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 15 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,19 @@ $ protoc --go-plugin_out=. --go-plugin_opt=paths=source_relative greeting.proto

Then, you will find 4 files generated in the same directory, `greet.pb.go`, `greet_host.pb.go`, `greet_plugin.pb.go` and `greet_vtproto.pb.go`.

#### Optional: Skip the go protobuf generation

If the plugin should be used together with other generators, then it may be
helpful to skip the standard protobuf generation. This can be done by setting
the option `disable_pb_gen=true`, for example:

```shell
$ protoc \
--go_opt=paths=source_relative --go_out=. \
--go-plugin_opt=paths=source_relative,disable_pb_gen=true --go-plugin_out=. \
greeting.proto
```

### Implement a plugin
The `Greeter` interface is generated as below in the previous step.

Expand Down Expand Up @@ -520,4 +533,4 @@ Welcome your contribution :)

[releases]: https://github.com/knqyf263/go-plugin/releases

[semver]: https://semver.org/
[semver]: https://semver.org/
4 changes: 3 additions & 1 deletion cmd/protoc-gen-go-plugin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"flag"

"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/types/pluginpb"

Expand All @@ -10,6 +11,7 @@ import (

func main() {
var flags flag.FlagSet
disablePbGen := flags.Bool("disable_pb_gen", false, "disable .pb.go generation")
protogen.Options{ParamFunc: flags.Set}.Run(func(plugin *protogen.Plugin) error {
g, err := gen.NewGenerator(plugin)
if err != nil {
Expand All @@ -20,7 +22,7 @@ func main() {
if !f.Generate {
continue
}
g.GenerateFiles(f)
g.GenerateFiles(f, gen.Options{DisablePBGen: *disablePbGen})
}

plugin.SupportedFeatures = uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL)
Expand Down
36 changes: 23 additions & 13 deletions gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ type Generator struct {
vtgen *vtgenerator.Generator
}

type Options struct {
DisablePBGen bool
}

func NewGenerator(plugin *protogen.Plugin) (*Generator, error) {
ext := &vtgenerator.Extensions{}
featureNames := []string{"marshal", "unmarshal", "size"}
Expand Down Expand Up @@ -138,18 +142,22 @@ func replaceImport(m *protogen.Message) {
}

// GenerateFiles generates the contents of a .pb.go file.
func (gg *Generator) GenerateFiles(file *protogen.File) *protogen.GeneratedFile {
func (gg *Generator) GenerateFiles(file *protogen.File, opts Options) *protogen.GeneratedFile {
f := gg.newFileInfo(file)
gg.generatePBFile(f)
gg.generatePBFile(f, opts.DisablePBGen)
gg.generateHostFile(f)
gg.generatePluginFile(f)
gg.generateVTFile(f)
gg.generateOptionsFile(f)
return nil
}

func (gg *Generator) generatePBFile(f *fileInfo) {
filename := f.GeneratedFilenamePrefix + ".pb.go"
func (gg *Generator) generatePBFile(f *fileInfo, disabled bool) {
filename := f.GeneratedFilenamePrefix
if disabled {
filename += "_service"
}
filename += ".pb.go"
g := gg.plugin.NewGeneratedFile(filename, f.GoImportPath)

gg.generateHeader(g, f)
Expand All @@ -165,14 +173,16 @@ func (gg *Generator) generatePBFile(f *fileInfo) {
g.P()
}

for i, imps := 0, f.Desc.Imports(); i < imps.Len(); i++ {
gg.genImport(g, f, imps.Get(i))
}
for _, enum := range f.allEnums {
genEnum(g, f, enum)
}
for _, message := range f.allMessages {
genMessage(g, f, message)
if !disabled {
for i, imps := 0, f.Desc.Imports(); i < imps.Len(); i++ {
gg.genImport(g, f, imps.Get(i))
}
for _, enum := range f.allEnums {
genEnum(g, f, enum)
}
for _, message := range f.allMessages {
genMessage(g, f, message)
}
}
for _, service := range f.allServices {
genServiceInterface(g, f, service)
Expand Down Expand Up @@ -252,7 +262,7 @@ func (gg *Generator) genImport(g *protogen.GeneratedFile, f *fileInfo, imp proto

// Generate public imports by generating the imported file, parsing it,
// and extracting every symbol that should receive a forwarding declaration.
impGen := gg.GenerateFiles(impFile)
impGen := gg.GenerateFiles(impFile, Options{})
impGen.Skip()
b, err := impGen.Content()
if err != nil {
Expand Down

0 comments on commit 3e0bd28

Please sign in to comment.