经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Spring Boot » 查看文章
protoc-gen-doc 自定义模板规则详解
来源:cnblogs  作者:一流菜鸟  时间:2023/8/4 9:02:26  对本文有异议

protoc-gen-doc 自定义模板规则详解

配套演示工程

此项目中所用 proto 文件位于 ./proto 目录下,来源于 官方proto示例

此项目中所列所有模板case文件位于 ./tmpl 目录下

此教程均基于 markdown 文本演示

前言

最近有通过 proto 文件生成其接口文档的需求,而 protoc-gen-doc 所生成的格式不能很好地满足需要。看到它支持自定义模板,但几乎没有一篇文章能详细解释该如何自定义,该用什么字段自定义,里面各种遍历、条件语句怎么写,如何取用 ServiceMethod Options 等。

经过近一天的梳理和踩坑,产出本文,记载了自定义模板所需的常用语法。

protoc-gen-doc 介绍

protoc-gen-doc 是一个用于生成 proto 的 protoc 插件,通过解析 proto 文件定义,快速生成多种类型的接口文档。

生成文档的原理,就是基于一个文件模板,通过解析 proto 文件的属性,填充到模板中指定位置。可以基于内置模板生成,也可以自定义模板使用。

内置模板支持:

  • json
  • html
  • markdown
  • docbook

使用方式:

  1. 首先通过 go get 下载 protoc-gen-doc 二进制文件,使用时将传入此文件的路径。。为了演示方便,我在配套代码工程的根目录下放了一个 protoc-gen-doc 文件。
  1. go get -u github.com/pseudomuto/protoc-gen-doc/cmd/protoc-gen-doc
  1. 使用 protoc 命令
  1. protoc --plugin=protoc-gen-doc=./protoc-gen-doc --proto_path=./proto --proto_path=./third_party --doc_out=./out --doc_opt=markdown,index.md ./proto/*.proto

参数解析:

  • --plugin: 指定 protoc-gen-doc,并指定 protoc-gen-doc 可执行文件的路径
  • --proto_path: 包含目标 proto 文件的目录路径,如果依赖了其他目录的 proto 文件,也要使用此参数指定其目录。例如 third_party 目录。
  • --doc_out: 接口文档的输出目录
  • --doc_opt: 指定待输出的文档模板和文件名
  • 最后一行: 指定目标 proto 文件

指定自定义模板

如果要指定自定义模型,则可修改 --doc_opt 的第一个参数。

例如,我在 ./tmpl 目录下自定义了模板文件 case1.tmpl,则 --doc_opt 应设置为:

  1. --doc_opt=./tmpl/case1.tmpl,index.md

官方自定义模板示例

官方在这里有两个简单的自定义模板,里面可见常用用法,但没有太多解释,想编写自己的自定义模板还是难度颇大。可供参考。

官方自定义模板示例

数据结构与函数定义

想要理解模板,就要将其中每个占位的参数所代表的含义。

模板中所有的占位参数,均可以在源码中找到:templates.go

这里不需要理解源码做了什么,不需要知道它是如何运行的,只需要看一个结构体:

  1. type Template struct {
  2. // The files that were parsed
  3. Files []*File `json:"files"`
  4. // Details about the scalar values and their respective types in supported languages.
  5. Scalars []*ScalarValue `json:"scalarValueTypes"`
  6. }

这就是从 proto 解析出的结构 Files 为一个文件,从 File 进入,可以见更多细节,亦可更深入 Services、Messages 查看。

  1. type File struct {
  2. Name string `json:"name"`
  3. Description string `json:"description"`
  4. Package string `json:"package"`
  5. HasEnums bool `json:"hasEnums"`
  6. HasExtensions bool `json:"hasExtensions"`
  7. HasMessages bool `json:"hasMessages"`
  8. HasServices bool `json:"hasServices"`
  9. Enums orderedEnums `json:"enums"`
  10. Extensions orderedExtensions `json:"extensions"`
  11. Messages orderedMessages `json:"messages"`
  12. Services orderedServices `json:"services"`
  13. Options map[string]interface{} `json:"options,omitempty"`
  14. }

与官方示例进行对比便可发现,那些占位的字段,皆为这里所定义的字段。这里字段名名如其意,应该无需详解。

其中Description字段,对应的是 proto 文件中的注释。当注释写在 service 上方时,便可从 Service 结构的 Desctiption字段读取。

另外,每个结构体还定义了诸多方法,也可以用于在自定义模板中调用。详情见后文。

基本语法

注解

主要以要生成的文档类型决定。

比如在 markdown 与 html 中,<!-- -->无法被渲染出来,则可以用 <!-- --> 写注解

输出字段值

想获取到某个字段值,可以使用 {{}} 符号。在一个自定义模型文件中,

{{.}} 初始表示 Template 结构

{{.Files}} 表示输出 Template 的 Files 字段。

如果想要拿到单独一个 File 进行操作,就要使用遍历语句进行遍历。

遍历语句

从 Template 中遍历 Files 列表,如下所示:

  1. <!-- case1.tmpl -->
  2. {{range .Files}}
  3. # {{.Name}}
  4. {{.Description}}
  5. {{.}}
  6. {{end}}

上面例子,使用 {{Range .Files}} 表示遍历 Files 列表,到 {{end}} 处结束。在 range 和 end 之间,{{.}} 表示 File 的结构,{{.Name}} 即为 File.Name。

修改 --doc_opt=./tmpl/case1.tmpl,case1.md 参数,查看运行结果:

image

遍历语句非常常用,它是可以嵌套的,不光 File,连 Service、Message、ServiceMethod 等结构,都需要使用 Range 遍历进入,获取其内部结构。

例如,进入File,并进入 File.Services,打印 Service 的信息:

  1. <!-- case2.tmpl -->
  2. {{range .Files}}
  3. # {{.Name}}
  4. {{.Description}}
  5. {{.}}
  6. {{range .Services}}
  7. ## ServiceName: {{.Name}}
  8. {{.Description}}
  9. {{.}}
  10. {{end}} <!-- end Services -->
  11. {{end}} <!-- end Files -->

这里我们遍历打印了 Service.Name、Service.Description 和 Service 结构体

修改 --doc_opt=./tmpl/case2.tmpl,case2.md 参数,查看运行结果:

image

参数赋值

可以看到,每次 range 内部,{{.}} 都变成了 range 的目标列表的一个 Item。当 range 嵌套较多时,容易混淆。可以将 Item 赋值给一个参数,在之后使用参数进行调用。

基于 case2 进行改造:

  1. <!-- case3.tmpl -->
  2. {{range .Files}}
  3. {{$file := .}}
  4. # {{$file.Name}}
  5. {{$file.Description}}
  6. {{$file}}
  7. {{range $file.Services}}
  8. {{$service := .}}
  9. ## ServiceName: {{$service.Name}}
  10. {{$service.Description}}
  11. {{$service}}
  12. {{end}} <!-- end Services -->
  13. {{end}} <!-- end Files -->

查看运行结果,可以看到用了参数的 case3 结果与 case2 完全一致:

image

条件语句

使用 {{if}} {{end}},也可以在其中加入 {{else}}

{{if}} 目前发现有两种用法:

  1. {{if eq param1 param2}}
  2. {{end}}
  3. {{if boolParam}}
  4. {{end}}

前者对比两个参数是否相等。后者判断 boolParam 是否为 true。

下面示例基于 case3,将判断 file.Name 是否等于 Booking.proto,若是,则输出相关信息,否则输出忽略 xxx

Booking.proto 中继续判断 HasServices 是否存在 service,如果存在则输出相关信息

  1. <!-- case4.tmpl -->
  2. {{range .Files}}
  3. {{$file := .}}
  4. {{if eq $file.Name "Booking.proto"}}
  5. # {{$file.Name}}
  6. {{$file.Description}}
  7. {{$file}}
  8. {{if $file.HasServices}}
  9. {{range $file.Services}}
  10. {{$service := .}}
  11. ## ServiceName: {{$service.Name}}
  12. {{$service.Description}}
  13. {{$service}}
  14. {{end}} <!-- end Services -->
  15. {{end}} <!-- end if -->
  16. {{else}}
  17. # 忽略 {{$file.Name}}
  18. {{end}} <!-- end if else -->
  19. {{end}} <!-- end Files -->

image

调用函数

想知道某个函数的功能,还要亲自阅读一下源码关于各结构体函数的实现:templates.go

使用方式:

  1. {{functionName params... }}

函数大多是用来读取 Options 内容的。几乎每个结构都有 Options,为 map[string]interface{} 结构。

比较常用的 Options,如下图对 Service 进行 http 的定义:

image

为了取出这里的 Options,可以使用 ServiceMethod 的 Option 方法。

基于 case3 进行改造:

  1. <!-- case5.tmpl -->
  2. {{range .Files}}
  3. {{$file := .}}
  4. # {{$file.Name}}
  5. {{$file.Description}}
  6. {{$file}}
  7. {{range $file.Services}}
  8. {{$service := .}}
  9. ## ServiceName: {{$service.Name}}
  10. {{$service.Description}}
  11. {{range $service.Methods}}
  12. {{$method := .}}
  13. ### ServiceMethod: {{$method.Name}}
  14. {{range ($method.Option "google.api.http").Rules}}
  15. - {{.Method}}
  16. - {{.Pattern}}
  17. - \{{.Body}} <!-- body 为 * 时会被认为是 markdown 语法 -->
  18. {{end}} <!-- end Rules -->
  19. {{end}} <!-- end Methods -->
  20. {{end}} <!-- end Services -->
  21. {{end}} <!-- end Files -->

运行后结果:

image

($method.Option "google.api.http").Rules 对取出的 interface{} 类型做了类型转换。为什么用 Rules 来转换,可以在源码中找到:

image

其他

  1. <!-- 设置默认值 -->
  2. {{ .XXX | default ""}}
  3. <!-- 取值小写 -->
  4. {{ .XXX | lower}}
  5. <!-- 替换 a 字符为空 -->
  6. {{ .XXX | replace "a" ""}}
  7. <!-- 取值小写并替换 a 字符为空 -->
  8. {{ .XXX | lower | replace "a" ""}}

除此之外,还有不少其他用法,暂时没有更深入探索,但掌握以上已经能够满足自定义模板之需求。

如看者有其他补充,不胜感激。

参考文档汇总

  1. 配套演示工程 https://github.com/csuqiyuan/custom-protoc-doc-example
  2. proto 文件官方示例 https://github.com/pseudomuto/protoc-gen-doc/tree/master/examples/proto
  3. 自定义模板官方示例 https://github.com/pseudomuto/protoc-gen-doc/tree/master/examples/templates
  4. templates.go https://github.com/pseudomuto/protoc-gen-doc/blob/master/template.go
  5. google.api.http Option 类型转换 https://github.com/pseudomuto/protoc-gen-doc/blob/master/extensions/google_api_http/google_api_http.go

原文链接:https://www.cnblogs.com/ccwd/p/17603865.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号