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
3
64
4
65
``` shell
5
66
make proto
6
67
```
7
68
8
69
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.
15
96
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?
17
99
18
- ## How to compile the proto files into ` .pb.go ` code
100
+ ### How to compile the proto files into ` .pb.go ` code
19
101
<!-- tabs:start -->
20
- ### ** Make cmmand(recommended)**
102
+ #### ** Make cmmand(recommended)**
21
103
22
104
``` bash
23
105
make proto.code
24
106
```
25
107
26
108
This command uses docker to run protoc and generate ` .pb.go ` code files.
27
109
28
- ### ** Install protoc**
110
+ #### ** Install protoc**
29
111
1 . Install protoc version: [ v3.17.3] ( https://github.com/protocolbuffers/protobuf/releases/tag/v3.17.3 )
30
112
31
113
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
38
120
```
39
121
40
122
<!-- 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
42
124
We can use [ protoc-gen-doc] ( https://github.com/pseudomuto/protoc-gen-doc ) and docker to generate api documents,the command is as follows:
43
125
44
126
<!-- tabs:start -->
45
- ### ** Make command(recommended)**
127
+ #### ** Make command(recommended)**
46
128
47
129
``` bash
48
130
make proto.doc
49
131
```
50
132
51
133
This command uses docker to run protoc-gen-doc and generate docs.
52
134
53
- ### ** Use docker to run protoc-gen-doc**
135
+ #### ** Use docker to run protoc-gen-doc**
54
136
` make proto.doc ` invokes the script ` etc/script/generate-doc.sh ` , which uses docker to run protoc-gen-doc.
55
137
56
138
You can check ` etc/script/generate-doc.sh ` for more details.
0 commit comments