Skip to content

Commit ab88888

Browse files
authored
Merge pull request #122 from howardjohn/gen/build-tag
Add ability to include a build tag
2 parents 2cc4577 + c64fedf commit ab88888

File tree

5 files changed

+51
-5
lines changed

5 files changed

+51
-5
lines changed

Makefile

+10
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,16 @@ gen-testproto: get-grpc-testproto gen-wkt-testproto install
6464
testproto/proto2/scalars.proto \
6565
testproto/unsafe/unsafe.proto \
6666
|| exit 1;
67+
$(PROTOBUF_ROOT)/src/protoc \
68+
--proto_path=testproto \
69+
--proto_path=include \
70+
--go_out=. --plugin protoc-gen-go="${GOBIN}/protoc-gen-go" \
71+
--go-vtproto_opt=paths=source_relative \
72+
--go-vtproto_opt=buildTag=vtprotobuf \
73+
--go-vtproto_out=allow-empty=true:./testproto/buildtag --plugin protoc-gen-go-vtproto="${GOBIN}/protoc-gen-go-vtproto" \
74+
-I$(PROTOBUF_ROOT)/src \
75+
testproto/empty/empty.proto \
76+
|| exit 1;
6777

6878
get-grpc-testproto: install
6979
$(PROTOBUF_ROOT)/src/protoc \

README.md

+12-2
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,20 @@ The following features can be generated:
108108
--go-vtproto_opt=pool=vitess.io/vitess/go/vt/proto/query.Row \
109109
--go-vtproto_opt=pool=vitess.io/vitess/go/vt/proto/binlogdata.VStreamRowsResponse \
110110
```
111+
6. (Optional) if you want to selectively compile the generate `vtprotobuf` files, the `--vtproto_opt=buildTag=<tag>` can be used.
111112
112-
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.
113+
When using this option, the generated code will only be compiled in if a build tag is provided.
113114
114-
7. (Optional) Switch your RPC framework to use the optimized helpers (see following sections)
115+
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.
116+
This will reduce the number of build tags a user will need to configure if they are importing multiple libraries following this pattern.
117+
118+
When using this option, it is strongly recommended to make your code compile with and without the build tag.
119+
This can be done with type assertions before using `vtprotobuf` generated methods.
120+
The `grpc.Codec{}` object (discussed below) shows an example.
121+
122+
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.
123+
124+
8. (Optional) Switch your RPC framework to use the optimized helpers (see following sections)
115125
116126
## `vtprotobuf` package and well-known types
117127

cmd/protoc-gen-go-vtproto/main.go

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ func main() {
2626
f.Var(&cfg.Poolable, "pool", "use memory pooling for this object")
2727
f.BoolVar(&cfg.Wrap, "wrap", false, "generate wrapper types")
2828
f.StringVar(&features, "features", "all", "list of features to generate (separated by '+')")
29+
f.StringVar(&cfg.BuildTag, "buildTag", "", "the go:build tag to set on generated files")
2930

3031
protogen.Options{ParamFunc: f.Set}.Run(func(plugin *protogen.Plugin) error {
3132
gen, err := generator.NewGenerator(plugin, strings.Split(features, "+"), &cfg)

generator/generator.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,10 @@ func (o ObjectSet) Set(s string) error {
3737
}
3838

3939
type Config struct {
40-
Poolable ObjectSet
41-
Wrap bool
42-
AllowEmpty bool
40+
Poolable ObjectSet
41+
Wrap bool
42+
AllowEmpty bool
43+
BuildTag string
4344
}
4445

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

101+
if p.Config.BuildTag != "" {
102+
// Support both forms of tags for maximum compatibility
103+
p.P("//go:build ", p.Config.BuildTag)
104+
p.P("// +build ", p.Config.BuildTag)
105+
}
100106
p.P("// Code generated by protoc-gen-go-vtproto. DO NOT EDIT.")
101107
if bi, ok := debug.ReadBuildInfo(); ok {
102108
p.P("// protoc-gen-go-vtproto version: ", bi.Main.Version)

testproto/buildtag/empty/empty_vtproto.pb.go

+19
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)