Custom Scaffold Template
Kitex provides the ability to customize templates, if the default templates can not meet your needs, you can use the function of this custom template provided by Kitex. Now, it supports rendering single file and circular rendering according to methodInfo.
How to Use
-
The data used by the template is
PackageInfo
and we assume that this section contains all the metadata(e.g.methodInfo
…). You only need to pass the template file, and the data in the template isPackageInfo
data. -
Template rendering can only render a single file at a time. If there is a situation of sorting the file by
methods
and rendering it, you need to control them in your code. -
Templates are passed via the YAML folder and specified with the
--template-dir
command-line parameter. To avoid large files, templates are now organized into a dedicated directory. -
extensions.yaml
in the YAML folder is a specific file. The content of this configuration file is extending the service code. If it exists, there is no need to pass the ‘template-extension’ parameter. -
There are currently four update options available: overwrite, skip, append or add files based on specified methods (
loop_method
is true). The behavior is determined by thekey
field inupdate_behavior
, which can beskip
,cover
orappend
. Whenloop_method
is enabled,append
adds new files; otherwise, it appends to the end of the file, and the specifiedkey
during the update determines the behavior. -
The code generated by Kitex is divided into two parts, ‘kitex_gen’ and ‘mainPkg’ (including ‘main.go’, ‘handler.go’ …). ‘kitex_gen’ is immutable. Choosing between ‘mainPkg’ and ‘custom layout’ is a binary decision; specifying a ‘custom layout’ will prevent the generation of ‘mainPkg’.
Usage Scenarios
The default templates can not meet your needs. (e.g. want to generate ‘MVC Layout’,uniform error handling …)
Practice
YAML File
e.g. tql
https://github.com/cloudwego/cwgo/tree/main/tpl/kitex