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 ability to include a build tag #122

Merged
merged 1 commit into from
Jan 29, 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
10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ gen-testproto: get-grpc-testproto gen-wkt-testproto install
testproto/proto2/scalars.proto \
testproto/unsafe/unsafe.proto \
|| exit 1;
$(PROTOBUF_ROOT)/src/protoc \
--proto_path=testproto \
--proto_path=include \
--go_out=. --plugin protoc-gen-go="${GOBIN}/protoc-gen-go" \
--go-vtproto_opt=paths=source_relative \
--go-vtproto_opt=buildTag=vtprotobuf \
--go-vtproto_out=allow-empty=true:./testproto/buildtag --plugin protoc-gen-go-vtproto="${GOBIN}/protoc-gen-go-vtproto" \
-I$(PROTOBUF_ROOT)/src \
testproto/empty/empty.proto \
|| exit 1;

get-grpc-testproto: install
$(PROTOBUF_ROOT)/src/protoc \
Expand Down
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,20 @@ The following features can be generated:
--go-vtproto_opt=pool=vitess.io/vitess/go/vt/proto/query.Row \
--go-vtproto_opt=pool=vitess.io/vitess/go/vt/proto/binlogdata.VStreamRowsResponse \
```
6. (Optional) if you want to selectively compile the generate `vtprotobuf` files, the `--vtproto_opt=buildTag=<tag>` can be used.

6. Compile the `.proto` files in your project. You should see `_vtproto.pb.go` files next to the `.pb.go` and `_grpc.pb.go` files that were already being generated.
When using this option, the generated code will only be compiled in if a build tag is provided.

7. (Optional) Switch your RPC framework to use the optimized helpers (see following sections)
It is recommended, but not required, to use `vtprotobuf` as the build tag if this is desired, especially if your project is imported by others.
This will reduce the number of build tags a user will need to configure if they are importing multiple libraries following this pattern.

When using this option, it is strongly recommended to make your code compile with and without the build tag.
This can be done with type assertions before using `vtprotobuf` generated methods.
The `grpc.Codec{}` object (discussed below) shows an example.

7. Compile the `.proto` files in your project. You should see `_vtproto.pb.go` files next to the `.pb.go` and `_grpc.pb.go` files that were already being generated.

8. (Optional) Switch your RPC framework to use the optimized helpers (see following sections)

## `vtprotobuf` package and well-known types

Expand Down
1 change: 1 addition & 0 deletions cmd/protoc-gen-go-vtproto/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func main() {
f.Var(&cfg.Poolable, "pool", "use memory pooling for this object")
f.BoolVar(&cfg.Wrap, "wrap", false, "generate wrapper types")
f.StringVar(&features, "features", "all", "list of features to generate (separated by '+')")
f.StringVar(&cfg.BuildTag, "buildTag", "", "the go:build tag to set on generated files")

protogen.Options{ParamFunc: f.Set}.Run(func(plugin *protogen.Plugin) error {
gen, err := generator.NewGenerator(plugin, strings.Split(features, "+"), &cfg)
Expand Down
12 changes: 9 additions & 3 deletions generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ func (o ObjectSet) Set(s string) error {
}

type Config struct {
Poolable ObjectSet
Wrap bool
AllowEmpty bool
Poolable ObjectSet
Wrap bool
AllowEmpty bool
BuildTag string
}

type Generator struct {
Expand Down Expand Up @@ -97,6 +98,11 @@ func (gen *Generator) generateFile(gf *protogen.GeneratedFile, file *protogen.Fi
LocalPackages: gen.local,
}

if p.Config.BuildTag != "" {
// Support both forms of tags for maximum compatibility
p.P("//go:build ", p.Config.BuildTag)
p.P("// +build ", p.Config.BuildTag)
}
p.P("// Code generated by protoc-gen-go-vtproto. DO NOT EDIT.")
if bi, ok := debug.ReadBuildInfo(); ok {
p.P("// protoc-gen-go-vtproto version: ", bi.Main.Version)
Expand Down
19 changes: 19 additions & 0 deletions testproto/buildtag/empty/empty_vtproto.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading