Skip to content

Commit

Permalink
Merge pull request #122 from howardjohn/gen/build-tag
Browse files Browse the repository at this point in the history
Add ability to include a build tag
  • Loading branch information
vmg committed Jan 29, 2024
2 parents 2cc4577 + c64fedf commit ab88888
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 5 deletions.
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.

0 comments on commit ab88888

Please sign in to comment.