Skip to content

Commit

Permalink
Merge branch 'main' into feature/pool-wildcard
Browse files Browse the repository at this point in the history
Signed-off-by: Vicent Marti <vmg@strn.cat>
  • Loading branch information
vmg committed Jan 29, 2024
2 parents b4f3668 + 63d143b commit ec98e72
Show file tree
Hide file tree
Showing 55 changed files with 9,270 additions and 6,968 deletions.
13 changes: 12 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ gen-wkt: bin/protoc-gen-go-vtproto
$(PROTOBUF_ROOT)/src/google/protobuf/empty.proto \
$(PROTOBUF_ROOT)/src/google/protobuf/field_mask.proto \
$(PROTOBUF_ROOT)/src/google/protobuf/timestamp.proto \
$(PROTOBUF_ROOT)/src/google/protobuf/wrappers.proto
$(PROTOBUF_ROOT)/src/google/protobuf/wrappers.proto \
$(PROTOBUF_ROOT)/src/google/protobuf/struct.proto

gen-testproto: get-grpc-testproto gen-wkt-testproto install
$(PROTOBUF_ROOT)/src/protoc \
Expand All @@ -63,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
18 changes: 13 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,24 @@ 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.

## Well-known types
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.

By default, `vtprotobuf` will detect ProtoBuf [well-known types](https://protobuf.dev/reference/protobuf/google.protobuf/) embedded in your own Messages and generate optimized code to marshal and unmarshal them.
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.

In order to access the optimized code for these types, your `_vtproto.pb.go` files will have a dependency on this Go package. If this is not acceptable, you can disable well-known types with `--go-vtproto_opt=wkt=false`.
8. (Optional) Switch your RPC framework to use the optimized helpers (see following sections)

## `vtprotobuf` package and well-known types

Your generated `_vtproto.pb.go` files will have a dependency on this Go package to access some helper functions as well as the optimized code for ProtoBuf [well-known types](https://protobuf.dev/reference/protobuf/google.protobuf/). `vtprotobuf` will detect these types embedded in your own Messages and generate optimized code to marshal and unmarshal them.

## Using the optimized code with RPC frameworks

Expand Down
2 changes: 1 addition & 1 deletion cmd/protoc-gen-go-vtproto/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ func main() {
f.Var(&cfg.Poolable, "pool", "use memory pooling for this object")
f.Var(&cfg.PoolableExclude, "pool-exclude", "do not use memory pooling for this object")
f.BoolVar(&cfg.Wrap, "wrap", false, "generate wrapper types")
f.BoolVar(&cfg.WellKnownTypes, "wkt", true, "generate optimized code for well-known 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
2 changes: 1 addition & 1 deletion conformance/internal/conformance/conformance.pb.go

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

Loading

0 comments on commit ec98e72

Please sign in to comment.