Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to disable protobuf generation #69

Merged
merged 1 commit into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,20 @@ $ 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 like
[`protoc-gen-go`](https://pkg.go.dev/github.com/golang/protobuf/protoc-gen-go),
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 +534,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)
saschagrunert marked this conversation as resolved.
Show resolved Hide resolved
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
Loading