Template Extension
The cwgo tool also supports passing its own template, and the template syntax is the syntax of go template. cwgo also welcomes users to contribute their own templates. Due to the different concepts of RPC and HTTP, the corresponding template variables also have some differences, please refer to the following for details.
To pass a custom template, add the -template
parameter to the command, such as:
Template Reading
cwgo supports reading templates from local or git. git supports https or ssh formats, with -template
specifying the template path and -branch
specifying the template branch(default to the main branch).
RPC Server、Client, HTTP Server、Client all support template extension, specific usage can be found in the following text.
Local
git https
git ssh
RPC
-
The template file is delivered through the yaml folder, specified by the
--template-dir
command line parameter of kitex, all yaml files in this folder will be rendered, and if the template parsing fails, it will exit directly. Watch out for unknown hidden files.The yaml file is defined as follows:
-
extensions.yaml
in the folder is a specific file, the content of which is Extended Service Code configuration file. Please be aware of file naming conflicts. -
The data used by the template is PackageInfo. It is considered that this part contains all metadata, such as methodInfo, etc. The user only needs to pass the template file, and the data in the template is PackageInfo data. Commonly used content in PackageInfo can be found in the Appendix.
-
cwgo supports circularly rendering files according to methodInfo. There is only one element in the methodInfo list during cyclic rendering, which is the method currently being rendered.
-
When updating, currently supports overwriting, skipping and adding files according to methods, and supports appending in one file. If the loop render file is enabled, only skip and cover are supported.
Best practice rpc example tpl can refer to here.
HTTP
- The template file is delivered through the yaml folder, but unlike the RPC layout, the HTTP layout is implemented based on hertz’s custom template. Here we need to specify the yaml file name to be fixed as
layout.yaml
andpackage .yaml
, for the use of custom templates, please refer to Hz custom template usage documentation.
Best practice http example tpl can refer to here.