Skip to content

Commit 3fbcdf7

Browse files
seefloodwenxuwan
andauthored
feat: code generator (#779)
Co-authored-by: wenxuwan <[email protected]>
1 parent da5b2a1 commit 3fbcdf7

File tree

12 files changed

+295
-47
lines changed

12 files changed

+295
-47
lines changed

docs/_sidebar.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,9 @@
8989
- [Automate testing of Quickstart documentation with tools](en/development/test-quickstart.md)
9090
- [Component Development Guide](en/development/developing-component.md)
9191
- Want to modify proto files or API definition?
92-
- [Development specification when adding API](en/development/developing-api.md)
93-
- [Comment specification of proto file](en/api_reference/comment_spec_of_proto.md)
9492
- [How to generate pb code and API reference](en/api_reference/how_to_generate_api_doc.md)
93+
- [Comment specification of proto file](en/api_reference/comment_spec_of_proto.md)
94+
- [Development specification when adding API](en/development/developing-api.md)
9595
- [Layotto Github Workflows Guide](en/development/github-workflows.md)
9696
- [Layotto Commands Guide](en/development/commands.md)
9797
- [Layotto contributor guide](en/development/CONTRIBUTING.md)

docs/en/api_reference/how_to_generate_api_doc.md

+97-15
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,113 @@
1-
# How to generate `.pb.go` code and corresponding documentation
2-
Note: the commands below should be executed under layotto directory
1+
# How to generate code and documentation from the `.proto` files
2+
3+
Suppose you wrote a new proto file `spec/proto/extension/v1/email/email.proto` and you want to implement this API in Layotto:
4+
5+
```protobuf
6+
// EmailService is used to send emails.
7+
service EmailService {
8+
9+
// Send an email with template
10+
rpc SendEmailWithTemplate(SendEmailWithTemplateRequest) returns (SendEmailWithTemplateResponse) {}
11+
12+
// Send an email with raw content instead of using templates.
13+
rpc SendEmail(SendEmailRequest) returns (SendEmailResponse) {}
14+
15+
}
16+
17+
// different message types......
18+
```
19+
20+
It's a tedious job because you have to write a lot of code and docs.
21+
22+
Fortunately, Layotto has tools to generate the code/docs/CI configuration automatically. You don't have to do the job yourself!
23+
24+
## step 1. Make sure your proto file meets the following requirements
25+
- The file path should be `spec/proto/extension/v1/{api short name}/{api short name}.proto`
26+
- There should be only one `service` in the proto file. For example, the following file is **WRONG** :
27+
28+
```protobuf
29+
// EmailService is used to send emails.
30+
service EmailService {
31+
// ...
32+
}
33+
34+
// Wrong: there should be only one `service` in a `.proto` file
35+
service EmailService2 {
36+
// ...
37+
}
38+
39+
// different message types......
40+
```
41+
42+
- If you don't want to generate the quickstart docs for the proto, add a comment `/* @exclude quickstart generator */` .
43+
- If you don't want to generate the sdk & sidecar code for the proto, add a comment `/* @exclude code generator */` .
44+
45+
You can take the `spec/proto/extension/v1/s3/oss.proto` as an example:
46+
47+
```protobuf
48+
/* @exclude quickstart generator */
49+
/* @exclude code generator */
50+
// ObjectStorageService is an abstraction for blob storage or so called "object storage", such as alibaba cloud OSS, such as AWS S3.
51+
// You invoke ObjectStorageService API to do some CRUD operations on your binary file, e.g. query my file, delete my file, etc.
52+
service ObjectStorageService{
53+
//......
54+
}
55+
```
56+
57+
## step 2. Check the environment
58+
To run the generator, you need:
59+
- Go version >=1.16
60+
- Start Docker
61+
62+
## step 3. Generate everything
63+
Note: the command below should be executed under layotto directory
364

465
```shell
566
make proto
667
```
768

869
Then you get:
9-
- `.pb.go` code
10-
- API reference docs
11-
- updated API reference list
12-
- quickstart document (both chinese and english)
13-
- updated sidebar (The tool will add the generated quickstart doc into the sidebar of https://mosn.io/layotto )
14-
- updated CI (The tool will add the generated quickstart doc into the CI script `etc/script/test-quickstart.sh`)
70+
- Generated code
71+
- `.pb.go` code
72+
- `_grpc.pb.go` code
73+
- layotto go-sdk code
74+
- layotto sidecar code
75+
- Generated documentation
76+
- API reference docs
77+
- updated API reference list
78+
- quickstart document (both chinese and english)
79+
- updated sidebar (The tool will add the generated quickstart doc into the sidebar of https://mosn.io/layotto )
80+
- Updated CI (The tool will add the generated quickstart doc into the CI script `etc/script/test-quickstart.sh`)
81+
82+
## step 4. Write the rest of the code
83+
Now it's your job to implement:
84+
85+
- Layotto component
86+
- go examples
87+
88+
![image](https://user-images.githubusercontent.com/26001097/188782762-bc1404a8-b891-45d3-a1ac-f86cafdbc0ab.png)
89+
90+
- java examples
91+
92+
![image](https://user-images.githubusercontent.com/26001097/188782989-9aec893f-9d12-4ee6-9a64-940b0ba1ba1b.png)
93+
94+
## Behind the scenes
95+
We have a protoc plugin called [protoc-gen-p6](https://github.com/seeflood/protoc-gen-p6) to generate code for Layotto.
1596

16-
That's all :)
97+
## What if I want to generate pb/documentation only?
98+
The steps above generate everything, but what if I only want to generate `.pb.go` code ? What if I only want to generate the docs?
1799

18-
## How to compile the proto files into `.pb.go` code
100+
### How to compile the proto files into `.pb.go` code
19101
<!-- tabs:start -->
20-
### **Make cmmand(recommended)**
102+
#### **Make cmmand(recommended)**
21103

22104
```bash
23105
make proto.code
24106
```
25107

26108
This command uses docker to run protoc and generate `.pb.go` code files.
27109

28-
### **Install protoc**
110+
#### **Install protoc**
29111
1. Install protoc version: [v3.17.3](https://github.com/protocolbuffers/protobuf/releases/tag/v3.17.3)
30112

31113
2. Install protoc-gen-go v1.28 and protoc-gen-go-grpc v1.2
@@ -38,19 +120,19 @@ protoc -I. --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_o
38120
```
39121

40122
<!-- tabs:end -->
41-
## How to generate API reference doc according to the proto files
123+
### How to generate API reference doc according to the proto files
42124
We can use [protoc-gen-doc](https://github.com/pseudomuto/protoc-gen-doc) and docker to generate api documents,the command is as follows:
43125

44126
<!-- tabs:start -->
45-
### **Make command(recommended)**
127+
#### **Make command(recommended)**
46128

47129
```bash
48130
make proto.doc
49131
```
50132

51133
This command uses docker to run protoc-gen-doc and generate docs.
52134

53-
### **Use docker to run protoc-gen-doc**
135+
#### **Use docker to run protoc-gen-doc**
54136
`make proto.doc` invokes the script `etc/script/generate-doc.sh`, which uses docker to run protoc-gen-doc.
55137

56138
You can check `etc/script/generate-doc.sh` for more details.

docs/zh/_sidebar.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,9 @@
107107
- [使用工具自动测试 Quickstart 文档](zh/development/test-quickstart.md)
108108
- [想要开发新的组件?](zh/development/developing-component.md)
109109
- 想要修改proto文件或API定义?
110-
- [新增API时的开发规范](zh/development/developing-api.md)
111-
- [proto文件注释规范](zh/api_reference/comment_spec_of_proto.md)
112110
- [如何基于proto文件生成代码、接口文档](zh/api_reference/how_to_generate_api_doc.md)
111+
- [proto文件注释规范](zh/api_reference/comment_spec_of_proto.md)
112+
- [新增API时的开发规范](zh/development/developing-api.md)
113113
- [Layotto 四大 Github Workflows 说明](zh/development/github-workflows.md)
114114
- [Layotto 命令行工具指南](zh/development/commands.md)
115115
- 如何给 issue 打 label

docs/zh/api_reference/how_to_generate_api_doc.md

+95-13
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,104 @@
11
# 如何基于proto文件生成代码、文档
22

3+
Suppose you wrote a new proto file `spec/proto/extension/v1/email/email.proto` and you want to implement this API in Layotto:
4+
5+
```protobuf
6+
// EmailService is used to send emails.
7+
service EmailService {
8+
9+
// Send an email with template
10+
rpc SendEmailWithTemplate(SendEmailWithTemplateRequest) returns (SendEmailWithTemplateResponse) {}
11+
12+
// Send an email with raw content instead of using templates.
13+
rpc SendEmail(SendEmailRequest) returns (SendEmailResponse) {}
14+
15+
}
16+
17+
// different message types......
18+
```
19+
20+
It's a tedious job because you have to write a lot of code and docs.
21+
22+
Fortunately, Layotto has tools to generate the code/docs/CI configuration automatically. You don't have to do the job yourself!
23+
24+
## step 1. Make sure your proto file meets the following requirements
25+
- The file path should be `spec/proto/extension/v1/{api short name}/{api short name}.proto`
26+
- There should be only one `service` in the proto file. For example, the following file is **WRONG** :
27+
28+
```protobuf
29+
// EmailService is used to send emails.
30+
service EmailService {
31+
// ...
32+
}
33+
34+
// Wrong: there should be only one `service` in a `.proto` file
35+
service EmailService2 {
36+
// ...
37+
}
38+
39+
// different message types......
40+
```
41+
42+
- If you don't want to generate the quickstart docs for the proto, add a comment `/* @exclude quickstart generator */` .
43+
- If you don't want to generate the sdk & sidecar code for the proto, add a comment `/* @exclude code generator */` .
44+
45+
You can take the `spec/proto/extension/v1/s3/oss.proto` as an example:
46+
47+
```protobuf
48+
/* @exclude quickstart generator */
49+
/* @exclude code generator */
50+
// ObjectStorageService is an abstraction for blob storage or so called "object storage", such as alibaba cloud OSS, such as AWS S3.
51+
// You invoke ObjectStorageService API to do some CRUD operations on your binary file, e.g. query my file, delete my file, etc.
52+
service ObjectStorageService{
53+
//......
54+
}
55+
```
56+
57+
## step 2. Check the environment
58+
To run the generator, you need:
59+
- Go version >=1.16
60+
- Start Docker
61+
62+
## step 3. Generate everything
63+
364
```shell
465
make proto
566
```
667

768
Then you get:
8-
- `.pb.go` code
9-
- API reference docs
10-
- updated API reference list
11-
- quickstart document (both chinese and english)
12-
- updated sidebar (The tool will add the generated quickstart doc into the sidebar of https://mosn.io/layotto )
13-
- updated CI (The tool will add the generated quickstart doc into the CI script `etc/script/test-quickstart.sh`)
69+
- Generated code
70+
- `.pb.go` code
71+
- `_grpc.pb.go` code
72+
- layotto go-sdk code
73+
- layotto sidecar code
74+
- Generated documentation
75+
- API reference docs
76+
- updated API reference list
77+
- quickstart document (both chinese and english)
78+
- updated sidebar (The tool will add the generated quickstart doc into the sidebar of https://mosn.io/layotto )
79+
- Updated CI (The tool will add the generated quickstart doc into the CI script `etc/script/test-quickstart.sh`)
80+
81+
## step 4. Write the rest of the code
82+
Now it's your job to implement:
83+
84+
- Layotto component
85+
- go examples
86+
87+
![image](https://user-images.githubusercontent.com/26001097/188782762-bc1404a8-b891-45d3-a1ac-f86cafdbc0ab.png)
88+
89+
- java examples
90+
91+
![image](https://user-images.githubusercontent.com/26001097/188782989-9aec893f-9d12-4ee6-9a64-940b0ba1ba1b.png)
92+
93+
## Behind the scenes
94+
We have a protoc plugin called [protoc-gen-p6](https://github.com/seeflood/protoc-gen-p6) to generate code for Layotto.
1495

15-
That's all :)
96+
## What if I want to generate pb/documentation only?
97+
The steps above generate everything, but what if I only want to generate `.pb.go` code ? What if I only want to generate the docs?
1698

17-
## 如何把 proto 文件编译成`.pb.go`代码
99+
### 如何把 proto 文件编译成`.pb.go`代码
18100
<!-- tabs:start -->
19-
### **Make 命令生成(推荐)**
101+
#### **Make 命令生成(推荐)**
20102
本地启动 docker 后,在项目根目录下运行:
21103

22104
```bash
@@ -27,7 +109,7 @@ make proto.code
27109

28110
这种方式更方便,开发者不需要修改本地 protoc 版本,省去了很多烦恼。
29111

30-
### **手动安装工具**
112+
#### **手动安装工具**
31113
1. Install protoc version: [v3.17.3](https://github.com/protocolbuffers/protobuf/releases/tag/v3.17.3)
32114

33115
2. Install protoc-gen-go v1.28 and protoc-gen-go-grpc v1.2
@@ -39,12 +121,12 @@ cd spec/proto/runtime/v1
39121
protoc -I. --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=require_unimplemented_servers=false,paths=source_relative *.proto
40122
```
41123
<!-- tabs:end -->
42-
## 如何基于proto文件生成接口文档
124+
### 如何基于proto文件生成接口文档
43125

44126
我们可以用[protoc-gen-doc](https://github.com/pseudomuto/protoc-gen-doc) 和docker来生成接口文档,相关命令如下:
45127

46128
<!-- tabs:start -->
47-
### **Make 命令生成(推荐)**
129+
#### **Make 命令生成(推荐)**
48130
本地启动 docker 后,在项目根目录下运行:
49131

50132
```bash
@@ -53,7 +135,7 @@ make proto.doc
53135

54136
该命令会用 docker 启动 protoc-gen-doc,生成文档
55137

56-
### **用 docker 启动 protoc-gen-doc**
138+
#### **用 docker 启动 protoc-gen-doc**
57139
`make proto.doc` invokes the script `etc/script/generate-doc.sh`, which uses docker to run protoc-gen-doc.
58140

59141
You can check `etc/script/generate-doc.sh` for more details.

0 commit comments

Comments
 (0)