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

Added well-known types generated by vtproto plugin #93

Merged
1 commit merged into from
Oct 2, 2023
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
17 changes: 17 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Contributing to vtProtobuf

## Workflow

For all contributors, we recommend the standard [GitHub flow](https://guides.github.com/introduction/flow/)
based on [forking and pull requests](https://guides.github.com/activities/forking/).

For significant changes, please [create an issue](https://github.com/planetscale/vtprotobuf/issues)
to let everyone know what you're planning to work on, and to track progress and design decisions.

## Development

### Protobuf version upgrade

1. Bump protobuf version in [./protobuf.sh](./protobuf.sh)) (PROTOBUF_VERSION variable).
1. Run `./protobuf.sh` to download and build protobuf.
1. Run `make genall` to regenerate proto files with a new compiler version, including well-known types.
59 changes: 56 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
export GOBIN=$(PWD)/bin
export PROTOBUF_ROOT=$(PWD)/_vendor/protobuf-21.12

.PHONY: install test gen-conformance gen-include genall
.PHONY: install test gen-conformance gen-include gen-wkt genall

install:
go install -tags protolegacy google.golang.org/protobuf/cmd/protoc-gen-go
go install -tags protolegacy ./cmd/protoc-gen-go-vtproto
# go install -tags protolegacy github.com/gogo/protobuf/protoc-gen-gofast

bin/protoc-gen-go-vtproto:
go build -o bin/protoc-gen-go-vtproto cmd/protoc-gen-go-vtproto/main.go

gen-conformance:
$(PROTOBUF_ROOT)/src/protoc \
--proto_path=$(PROTOBUF_ROOT) \
Expand All @@ -32,7 +35,35 @@ gen-include:
github.com/planetscale/vtprotobuf/vtproto/ext.proto
mv include/github.com/planetscale/vtprotobuf/vtproto/*.go ./vtproto

gen-testproto:
gen-wkt: bin/protoc-gen-go-vtproto
$(PROTOBUF_ROOT)/src/protoc \
-I$(PROTOBUF_ROOT)/src \
--plugin protoc-gen-go="${GOBIN}/protoc-gen-go" \
--plugin protoc-gen-go-vtproto="${GOBIN}/protoc-gen-go-vtproto" \
--go_out=. \
--go-vtproto_out=. \
--go_opt=module=github.com/planetscale/vtprotobuf \
--go_opt="Mgoogle/protobuf/any.proto=github.com/planetscale/vtprotobuf/types/known/any;anypb" \
--go_opt="Mgoogle/protobuf/duration.proto=github.com/planetscale/vtprotobuf/types/known/duration;durationpb" \
--go_opt="Mgoogle/protobuf/empty.proto=github.com/planetscale/vtprotobuf/types/known/empty;emptypb" \
--go_opt="Mgoogle/protobuf/field_mask.proto=github.com/planetscale/vtprotobuf/types/known/field_mask;fieldmaskpb" \
--go_opt="Mgoogle/protobuf/timestamp.proto=github.com/planetscale/vtprotobuf/types/known/timestamp;timestamppb" \
--go_opt="Mgoogle/protobuf/wrappers.proto=github.com/planetscale/vtprotobuf/types/known/wrappers;wrapperspb" \
--go-vtproto_opt=module=github.com/planetscale/vtprotobuf \
--go-vtproto_opt="Mgoogle/protobuf/any.proto=github.com/planetscale/vtprotobuf/types/known/any;anypb" \
--go-vtproto_opt="Mgoogle/protobuf/duration.proto=github.com/planetscale/vtprotobuf/types/known/duration;durationpb" \
--go-vtproto_opt="Mgoogle/protobuf/empty.proto=github.com/planetscale/vtprotobuf/types/known/empty;emptypb" \
--go-vtproto_opt="Mgoogle/protobuf/field_mask.proto=github.com/planetscale/vtprotobuf/types/known/field_mask;fieldmaskpb" \
--go-vtproto_opt="Mgoogle/protobuf/timestamp.proto=github.com/planetscale/vtprotobuf/types/known/timestamp;timestamppb" \
--go-vtproto_opt="Mgoogle/protobuf/wrappers.proto=github.com/planetscale/vtprotobuf/types/known/wrappers;wrapperspb" \
$(PROTOBUF_ROOT)/src/google/protobuf/any.proto \
$(PROTOBUF_ROOT)/src/google/protobuf/duration.proto \
$(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

gen-testproto: gen-wkt-testproto
$(PROTOBUF_ROOT)/src/protoc \
--proto_path=testproto \
--proto_path=include \
Expand All @@ -47,7 +78,29 @@ gen-testproto:
testproto/proto2/scalars.proto \
|| exit 1;

genall: install gen-include gen-conformance gen-testproto
gen-wkt-testproto:
$(PROTOBUF_ROOT)/src/protoc \
--proto_path=testproto \
--proto_path=include \
--go_out=. --plugin protoc-gen-go="${GOBIN}/protoc-gen-go" \
--go-vtproto_out=allow-empty=true:. --plugin protoc-gen-go-vtproto="${GOBIN}/protoc-gen-go-vtproto" \
-I$(PROTOBUF_ROOT)/src \
--go_opt="Mgoogle/protobuf/any.proto=github.com/planetscale/vtprotobuf/types/known/any;anypb" \
--go_opt="Mgoogle/protobuf/duration.proto=github.com/planetscale/vtprotobuf/types/known/duration;durationpb" \
--go_opt="Mgoogle/protobuf/empty.proto=github.com/planetscale/vtprotobuf/types/known/empty;emptypb" \
--go_opt="Mgoogle/protobuf/field_mask.proto=github.com/planetscale/vtprotobuf/types/known/field_mask;fieldmaskpb" \
--go_opt="Mgoogle/protobuf/timestamp.proto=github.com/planetscale/vtprotobuf/types/known/timestamp;timestamppb" \
--go_opt="Mgoogle/protobuf/wrappers.proto=github.com/planetscale/vtprotobuf/types/known/wrappers;wrapperspb" \
--go-vtproto_opt="Mgoogle/protobuf/any.proto=github.com/planetscale/vtprotobuf/types/known/any;anypb" \
--go-vtproto_opt="Mgoogle/protobuf/duration.proto=github.com/planetscale/vtprotobuf/types/known/duration;durationpb" \
--go-vtproto_opt="Mgoogle/protobuf/empty.proto=github.com/planetscale/vtprotobuf/types/known/empty;emptypb" \
--go-vtproto_opt="Mgoogle/protobuf/field_mask.proto=github.com/planetscale/vtprotobuf/types/known/field_mask;fieldmaskpb" \
--go-vtproto_opt="Mgoogle/protobuf/timestamp.proto=github.com/planetscale/vtprotobuf/types/known/timestamp;timestamppb" \
--go-vtproto_opt="Mgoogle/protobuf/wrappers.proto=github.com/planetscale/vtprotobuf/types/known/wrappers;wrapperspb" \
testproto/wkt/wkt.proto \
|| exit 1;

genall: install gen-include gen-conformance gen-testproto gen-wkt

test: install gen-conformance
go test -short ./...
Expand Down
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,30 @@ The following features can be generated:

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

### Well-known types

Several changes are needed in order to use pre-generated optimized code for well-known types.

1. `go get github.com/planetscale/vtprotobuf`
1. Imports `google.golang.org/protobuf/types/known` should be replaced with `github.com/planetscale/vtprotobuf`.
1. `protoc` should be invoked with the following options:

```
protoc \
--go_opt="Mgoogle/protobuf/any.proto=github.com/planetscale/vtprotobuf/types/known/any;anypb" \
--go_opt="Mgoogle/protobuf/duration.proto=github.com/planetscale/vtprotobuf/types/known/duration;durationpb" \
--go_opt="Mgoogle/protobuf/empty.proto=github.com/planetscale/vtprotobuf/types/known/empty;emptypb" \
--go_opt="Mgoogle/protobuf/field_mask.proto=github.com/planetscale/vtprotobuf/types/known/field_mask;fieldmaskpb" \
--go_opt="Mgoogle/protobuf/timestamp.proto=github.com/planetscale/vtprotobuf/types/known/timestamp;timestamppb" \
--go_opt="Mgoogle/protobuf/wrappers.proto=github.com/planetscale/vtprotobuf/types/known/wrappers;wrapperspb" \
--go-vtproto_opt="Mgoogle/protobuf/any.proto=github.com/planetscale/vtprotobuf/types/known/any;anypb" \
--go-vtproto_opt="Mgoogle/protobuf/duration.proto=github.com/planetscale/vtprotobuf/types/known/duration;durationpb" \
--go-vtproto_opt="Mgoogle/protobuf/empty.proto=github.com/planetscale/vtprotobuf/types/known/empty;emptypb" \
--go-vtproto_opt="Mgoogle/protobuf/field_mask.proto=github.com/planetscale/vtprotobuf/types/known/field_mask;fieldmaskpb" \
--go-vtproto_opt="Mgoogle/protobuf/timestamp.proto=github.com/planetscale/vtprotobuf/types/known/timestamp;timestamppb" \
--go-vtproto_opt="Mgoogle/protobuf/wrappers.proto=github.com/planetscale/vtprotobuf/types/known/wrappers;wrapperspb"
```

## Using the optimized code with RPC frameworks

The `protoc-gen-go-vtproto` compiler does not overwrite any of the default marshalling or unmarshalling code for your ProtoBuf objects. Instead, it generates helper methods that can be called explicitly to opt-in to faster (de)serialization.
Expand Down
2 changes: 1 addition & 1 deletion protobuf.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ PROTOBUF_VERSION=21.12
ROOT=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &> /dev/null && pwd)
PROTOBUF_PATH="${ROOT}/_vendor/protobuf-${PROTOBUF_VERSION}"

if [ -f "$PROTOBUF_PATH/protoc" ]; then
if [ -f "$PROTOBUF_PATH/src/protoc" ]; then
echo "protoc found in $PROTOBUF_PATH"
exit 0
fi
Expand Down
Loading
Loading