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 transcoding best-practice example #52

Merged
merged 3 commits into from
Jun 23, 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
6 changes: 6 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[submodule "examples/transcoding/best-practice/bp-api/third_party/protovalidate"]
path = examples/transcoding/best-practice/bp-api/third_party/protovalidate
url = https://github.com/bufbuild/protovalidate
[submodule "examples/transcoding/best-practice/bp-api/third_party/googleapis"]
path = examples/transcoding/best-practice/bp-api/third_party/googleapis
url = https://github.com/googleapis/googleapis
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ allprojects {
endWithNewline()
palantirJavaFormat()

targetExclude "build/generated/**"
targetExclude "**/generated/**"

custom("Refuse wildcard imports", {
if (it =~ /\nimport .*\*;/) {
Expand Down
32 changes: 32 additions & 0 deletions examples/transcoding/best-practice/bp-api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
## Project Configuration

Using [buf](https://buf.build/) to manage proto files.

[protoc-gen-openapi](https://github.com/google/gnostic/blob/main/cmd/protoc-gen-openapi/README.md) is not in the [Buf Schema Registry](https://buf.build/plugins),
so it can't use it directly with buf [remote plugin](https://buf.build/docs/generate/overview#generating-with-remote-plugins).

Refer to [issues#821](https://github.com/bufbuild/plugins/issues/821).

To use `protoc-gen-openapi` with buf, you can use the following steps:

1. Install `protoc-gen-openapi`

```bash
go install github.com/google/gnostic/cmd/protoc-gen-openapi@latest
```

2. Use as local plugin in buf.gen.yaml

```yaml
plugins:
- local: protoc-gen-openapi
out: gen/openapi
```

Make sure $GOPATH/bin is in your PATH: `export PATH="PATH:$(go env GOPATH)/bin"`

## Generate Code

```bash
cd examples/transcoding/best-practice/bp-api && buf generate
```
50 changes: 50 additions & 0 deletions examples/transcoding/best-practice/bp-api/api/user/v1/user.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
syntax = "proto3";

package user.v1;

import "google/api/annotations.proto";
import "buf/validate/validate.proto";

option java_multiple_files = true;
option go_package = "github.com/your/repo/user/v1;user_v1";

message User {
int64 id = 1;
string name = 2;
}

message GetUserRequest {
int64 id = 1 [(buf.validate.field).int64 = {
gte: 0
}];
}

message GetUserResponse {
User user = 1;
}

message DeleteUserRequest {
int64 id = 1 [(buf.validate.field).int64 = {
gte: 0
}];
}

message DeleteUserResponse {

}

service UserService {
// Get a user by ID.
rpc GetUser(GetUserRequest) returns (GetUserResponse) {
option (google.api.http) = {
get: "/v1/users/{id}"
};
}

// Delete a user by ID.
rpc DeleteUser(DeleteUserRequest) returns (DeleteUserResponse) {
option (google.api.http) = {
delete: "/v1/users/{id}"
};
}
}
16 changes: 16 additions & 0 deletions examples/transcoding/best-practice/bp-api/buf.gen.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
version: v2
plugins:
# Java
- remote: buf.build/grpc/java:v1.64.0
out: generated/java
- remote: buf.build/protocolbuffers/java:v25.1
out: generated/java

# OpenAPI
- local: protoc-gen-openapi
out: generated/openapi
opt:
- fq_schema_naming=true
- enum_type=string

# Other languages ...
9 changes: 9 additions & 0 deletions examples/transcoding/best-practice/bp-api/buf.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Generated by buf. DO NOT EDIT.
version: v2
deps:
- name: buf.build/bufbuild/protovalidate
commit: 46a4cf4ba1094a34bcd89a6c67163b4b
digest: b5:2076a950fdf4a8047064d55fd1d20ef21e6d745bf56e3edf557071abd4488ed48c9466d60831d8a03489dc1fcc8ceaa073d197411b59ecd873e28b1328034e0b
- name: buf.build/googleapis/googleapis
commit: f0e53af8f2fc4556b94f482688b57223
digest: b5:24e758f963ee1bb3b5218eb452e0bdfb7a5449d9a77d174b8284b6368ccc1884213689381cdcd79e4231796c281c128ac1ae50825237b1774deb542bdc704b32
11 changes: 11 additions & 0 deletions examples/transcoding/best-practice/bp-api/buf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# https://buf.build/docs/configuration/v2/buf-yaml

version: v2
modules:
- path: api
deps:
- buf.build/googleapis/googleapis
- buf.build/bufbuild/protovalidate
lint:
use:
- DEFAULT
12 changes: 12 additions & 0 deletions examples/transcoding/best-practice/bp-api/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
dependencies {
api("io.grpc:grpc-protobuf")
api(project(":grpc-starters:grpc-starter-protovalidate"))
}

sourceSets {
main {
java {
srcDirs "generated/java"
}
}
}
Loading