Skip to content

Commit c3787b4

Browse files
Jeremytjuhjohanbrandhorst
authored andcommitted
Use Go templates in protofile comments (#1056)
* use GO templates in protofile comments * Added support for the other swagger documentation fields * Added feature for Go templates in imported files * Fixed typo in main.go Co-Authored-By: Johan Brandhorst <[email protected]> * Added Go template example * Added Go template documentation * Generated examples using build image * Generated bazel files using build image * Added use_go_templates.pb.gw.go in build file * Enhanced documentation * Fixed indents * Edited Go template bazel test * Excluded use_go_template.pb.gw.go in buildfile
1 parent 8115cdb commit c3787b4

File tree

14 files changed

+1247
-23
lines changed

14 files changed

+1247
-23
lines changed

Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ SWAGGER_EXAMPLES=examples/proto/examplepb/echo_service.proto \
6464
examples/proto/examplepb/wrappers.proto \
6565
examples/proto/examplepb/stream.proto \
6666
examples/proto/examplepb/unannotated_echo_service.proto \
67+
examples/proto/examplepb/use_go_template.proto \
6768
examples/proto/examplepb/response_body_service.proto
6869

6970
EXAMPLES=examples/proto/examplepb/echo_service.proto \
@@ -73,6 +74,7 @@ EXAMPLES=examples/proto/examplepb/echo_service.proto \
7374
examples/proto/examplepb/non_standard_names.proto \
7475
examples/proto/examplepb/wrappers.proto \
7576
examples/proto/examplepb/unannotated_echo_service.proto \
77+
examples/proto/examplepb/use_go_template.proto \
7678
examples/proto/examplepb/response_body_service.proto
7779

7880
EXAMPLE_SVCSRCS=$(EXAMPLES:.proto=.pb.go)
@@ -161,7 +163,7 @@ $(EXAMPLE_GWSRCS): $(GATEWAY_PLUGIN) $(EXAMPLES)
161163

162164
$(EXAMPLE_SWAGGERSRCS): ADDITIONAL_SWG_FLAGS:=$(ADDITIONAL_SWG_FLAGS),grpc_api_configuration=examples/proto/examplepb/unannotated_echo_service.yaml
163165
$(EXAMPLE_SWAGGERSRCS): $(SWAGGER_PLUGIN) $(SWAGGER_EXAMPLES)
164-
protoc -I $(PROTOC_INC_PATH) -I. -I$(GOOGLEAPIS_DIR) --plugin=$(SWAGGER_PLUGIN) --swagger_out=logtostderr=true,allow_repeated_fields_in_body=true,$(PKGMAP)$(ADDITIONAL_SWG_FLAGS):. $(SWAGGER_EXAMPLES)
166+
protoc -I $(PROTOC_INC_PATH) -I. -I$(GOOGLEAPIS_DIR) --plugin=$(SWAGGER_PLUGIN) --swagger_out=logtostderr=true,allow_repeated_fields_in_body=true,use_go_templates=true,$(PKGMAP)$(ADDITIONAL_SWG_FLAGS):. $(SWAGGER_EXAMPLES)
165167

166168
$(ECHO_EXAMPLE_SRCS): $(ECHO_EXAMPLE_SPEC)
167169
$(SWAGGER_CODEGEN) generate -i $(ECHO_EXAMPLE_SPEC) \

docs/_docs/usegotemplates.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
| Title | Category |
2+
| -------------------------------------- | ------------- |
3+
| Use go templates in protofile comments | Documentation |
4+
5+
# Use go templates in protofile comments
6+
7+
Use [Go templates](https://golang.org/pkg/text/template/ "Package template") in your protofile comments to allow more advanced documentation such as:
8+
* Documentation about fields in the proto objects.
9+
* Import the content of external files (such as [Markdown](https://en.wikipedia.org/wiki/Markdown "Markdown Github")).
10+
11+
## How to use it
12+
13+
By default this function is turned off, so if you want to use it you have to set the `use_go_templates` flag to true inside of the `swagger_out` flag.
14+
```bash
15+
--swagger_out=use_go_templates=true:.
16+
```
17+
18+
### Example script
19+
20+
Example of a bash script with the `use_go_templates` flag set to true:
21+
22+
```bash
23+
protoc -I/usr/local/include -I. \
24+
-I$GOPATH/src \
25+
-I$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \
26+
-I$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway \
27+
--go_out=plugins=grpc:. \
28+
--grpc-gateway_out=logtostderr=true:. \
29+
--swagger_out=logtostderr=true,use_go_templates=true:. \
30+
*.proto
31+
```
32+
33+
### Example proto file
34+
35+
Example of a protofile with Go templates. This proto file imports documentation from another file, `tables.md`:
36+
```protobuf
37+
service LoginService {
38+
// Login
39+
//
40+
// {{.MethodDescriptorProto.Name}} is a call with the method(s) {{$first := true}}{{range .Bindings}}{{if $first}}{{$first = false}}{{else}}, {{end}}{{.HTTPMethod}}{{end}} within the "{{.Service.Name}}" service.
41+
// It takes in "{{.RequestType.Name}}" and returns a "{{.ResponseType.Name}}".
42+
//
43+
// {{import "tables.md"}}
44+
rpc Login (LoginRequest) returns (LoginReply) {
45+
option (google.api.http) = {
46+
post: "/v1/example/login"
47+
body: "*"
48+
};
49+
}
50+
}
51+
52+
message LoginRequest {
53+
// The entered username
54+
string username = 1;
55+
// The entered password
56+
string password = 2;
57+
}
58+
59+
message LoginReply {
60+
// Whether you have access or not
61+
bool access = 1;
62+
}
63+
```
64+
65+
The content of `tables.md`:
66+
67+
```markdown
68+
## {{.RequestType.Name}}
69+
| Field ID | Name | Type | Description |
70+
| ----------- | --------- | --------------------------------------------------------- | ---------------------------- | {{range .RequestType.Fields}}
71+
| {{.Number}} | {{.Name}} | {{if eq .Label.String "LABEL_REPEATED"}}[]{{end}}{{.Type}} | {{fieldcomments .Message .}} | {{end}}
72+
73+
## {{.ResponseType.Name}}
74+
| Field ID | Name | Type | Description |
75+
| ----------- | --------- | ---------------------------------------------------------- | ---------------------------- | {{range .ResponseType.Fields}}
76+
| {{.Number}} | {{.Name}} | {{if eq .Label.String "LABEL_REPEATED"}}[]{{end}}{{.Type}} | {{fieldcomments .Message .}} | {{end}}
77+
```
78+
79+
## Swagger output
80+
81+
### SwaggerUI
82+
83+
This is how the swagger file would be rendered in [SwaggerUI](https://swagger.io/tools/swagger-ui/ "SwaggerUI site")
84+
85+
![Screenshot swaggerfile in SwaggerUI](../_imgs/gotemplates/swaggerui.png "SwaggerUI")
86+
87+
### Postman
88+
89+
This is how the swagger file would be rendered in [Postman](https://www.getpostman.com/ "Postman site")
90+
91+
![Screenshot swaggerfile in Postman](../_imgs/gotemplates/postman.png "Postman")
92+
93+
For a more detailed example of a protofile that has Go templates enabled, [click here](https://github.com/grpc-ecosystem/grpc-gateway/blob/master/examples/proto/examplepb/use_go_template.proto "Example protofile with Go template").

docs/_imgs/gotemplates/postman.png

50.5 KB
Loading

docs/_imgs/gotemplates/swaggerui.png

30.2 KB
Loading

examples/proto/examplepb/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package(default_visibility = ["//visibility:public"])
99
# gazelle:exclude flow_combination.pb.gw.go
1010
# gazelle:exclude non_standard_names.pb.gw.go
1111
# gazelle:exclude stream.pb.gw.go
12+
# gazelle:exclude use_go_template.pb.gw.go
1213
# gazelle:exclude wrappers.pb.gw.go
1314
# gazelle:exclude response_body_service.pb.gw.go
1415

@@ -22,6 +23,7 @@ proto_library(
2223
"response_body_service.proto",
2324
"stream.proto",
2425
"unannotated_echo_service.proto",
26+
"use_go_template.proto",
2527
"wrappers.proto",
2628
],
2729
deps = [

0 commit comments

Comments
 (0)