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
cwgo server -type RPC -service {service name} -idl {idl path} -template {tpl path}
--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.extensions.yaml
in the folder is a specific file, the content of which is Extended Service Code configuration file. If the file exists, there is no need to pass the template-extension
parameterThe yaml file is defined as follows:
# The path and file name of the generated file, which will create a folder in the root directory of the project, and generate a main.go file in the folder, which supports template rendering syntax
path: /handler/{{ .Name }}.go
update_behavior:
type: skip / cover / append # Specify update behavior, if loop_method is true, append is not supported. The default is skip
key: Test{{.Name}} # The searched function name, if the rendered function name exists, it is considered that the method does not need to be appended
append_tpl: # updated content template
import_tpl: # The newly added import content is a list, which can be rendered by template
loop_method: true # Whether to enable loop rendering
body: template content # template content
Best practice rpc example tpl can refer to here
layout.yaml
and package .yaml
, for the use of custom templates, please refer to Hz custom template usage documentation.Best practice http example tpl can refer to here
type PackageInfo struct {
Namespace string // idl namespace, it is recommended not to use under pb
Dependencies map[string]string // package name => import path, used for searching imports
*ServiceInfo // the target service
Codec string
NoFastAPI bool
Version string
RealServiceName string
Imports map[string]map[string]bool
ExternalKitexGen string
Features []feature
FrugalPretouch bool
Module string // go module name
}
type ServiceInfo struct {
PkgInfo
ServiceName string
RawServiceName string
ServiceTypeName func() string
Base *ServiceInfo
Methods []*MethodInfo
CombineServices []*ServiceInfo
Has Streaming bool
}
type PkgInfo struct {
PkgName string // last paragraph of namespace
PkgRefName string
ImportPath string // import path of req and resp of this method
}
type MethodInfo struct {
PkgInfo
ServiceName string // the name of this service
Name string // the name of this method
RawName string // Same as above
Oneway bool
void bool
Args []*Parameter // Input parameter information, including input parameter name, import path, type
Resp *Parameter // output parameter, including input parameter name, import path, type
Exceptions []*Parameter
ArgStructName string
ResStructName string
IsResponseNeedRedirect bool // int -> int*
GenArgResultStruct bool
ClientStreaming bool
Server Streaming bool
}
}