Closed zengxiaolou closed 6 months ago
cwgo 的模版支持 git 托管,可能更适合你 https://www.cloudwego.io/zh/docs/cwgo/tutorials/templete-extension/
参考 cwgo 的模版 https://github.com/cloudwego/cwgo/tree/main/tpl/hertz/server/standard
参考了,但是里面没有关于将所有service放到一个文件中的例子。 只有Method的,我已经根据模版将handler分到不同的文件中了,现在需要将service聚合。做DDD,目前这个MVC不太符合我项目的需求。
cc @FGYFFFF 帮看下怎么搞
cwgo 的模版支持 git 托管,可能更适合你 https://www.cloudwego.io/zh/docs/cwgo/tutorials/templete-extension/
按照文档的描述应该是和Methods差不多,但是使用Services 进行获取,就会报错
[ERROR]/Users/ruler/go/pkg/mod/github.com/cloudwego/hertz/cmd/hz@v0.8.1/thrift/plugin.go:162: generate package failed: render template 'biz/service/{{ToSnakeCase .ServiceName}}.go' failed, err: template: biz/service/{{ToSnakeCase .ServiceName}}.go:9:25: executing "biz/service/{{ToSnakeCase .ServiceName}}.go" at <.Services>: can't evaluate field Services in type generator.CustomizedFileForIDL
不需要 {{.Service}},这个变量直接被嵌套了,用的时候直接使用 {{.Name}} {{.Methods}} 之类的就可以了
不需要 {{.Service}},这个变量直接被嵌套了,用的时候直接使用 {{.Name}} {{.Methods}} 之类的就可以了
不是,大佬我这边是要将所有的service放到一个文件中,那这里应该就是Services了,按照Methods 的方式,然后在模版中进行遍历,你说的这个是在一个service在一个文件的调用方式吧?
不需要 {{.Service}},这个变量直接被嵌套了,用的时候直接使用 {{.Name}} {{.Methods}} 之类的就可以了
body: |-
package {{.FilePackage}}
import (
"context"
"github.com/cloudwego/hertz/pkg/app"
)
{{.Name}}```
[ERROR]/Users/ruler/go/pkg/mod/github.com/cloudwego/hertz/cmd/hz@v0.8.1/thrift/plugin.go:162: generate package failed: render template 'biz/service/{{ToSnakeCase .ServiceName}}.go' failed, err: template: biz/service/{{ToSnakeCase .ServiceName}}.go:9:2: executing "biz/service/{{ToSnakeCase .ServiceName}}.go" at <.Name>: can't evaluate field Name in type generator.CustomizedFileForIDL
没法直接获取.Name
不需要 {{.Service}},这个变量直接被嵌套了,用的时候直接使用 {{.Name}} {{.Methods}} 之类的就可以了
body: |- package {{.FilePackage}} import ( "context" "github.com/cloudwego/hertz/pkg/app" ) {{.Name}}``` [ERROR]/Users/ruler/go/pkg/mod/github.com/cloudwego/hertz/cmd/hz@v0.8.1/thrift/plugin.go:162: generate package failed: render template 'biz/service/{{ToSnakeCase .ServiceName}}.go' failed, err: template: biz/service/{{ToSnakeCase .ServiceName}}.go:9:2: executing "biz/service/{{ToSnakeCase .ServiceName}}.go" at <.Name>: can't evaluate field Name in type generator.CustomizedFileForIDL 没法直接获取.Name
你是使用的是"generator.CustomizedFileForIDL"结构,具体的变量都在这, 可以根据这个进行取值 https://github.com/cloudwego/hertz/blob/develop/cmd/hz/generator/custom_files.go#L64
不需要 {{.Service}},这个变量直接被嵌套了,用的时候直接使用 {{.Name}} {{.Methods}} 之类的就可以了
body: |- package {{.FilePackage}} import ( "context" "github.com/cloudwego/hertz/pkg/app" ) {{.Name}}``` [ERROR]/Users/ruler/go/pkg/mod/github.com/cloudwego/hertz/cmd/hz@v0.8.1/thrift/plugin.go:162: generate package failed: render template 'biz/service/{{ToSnakeCase .ServiceName}}.go' failed, err: template: biz/service/{{ToSnakeCase .ServiceName}}.go:9:2: executing "biz/service/{{ToSnakeCase .ServiceName}}.go" at <.Name>: can't evaluate field Name in type generator.CustomizedFileForIDL 没法直接获取.Name
你是使用的是"generator.CustomizedFileForIDL"结构,具体的变量都在这, 可以根据这个进行取值 https://github.com/cloudwego/hertz/blob/develop/cmd/hz/generator/custom_files.go#L64
感谢大佬,知道怎么弄了
Describe the Question
我现在需要采用DDD的方式,对项目进行分层。在使用模版生成时,发现按照教程,只能将handle聚合在一个文件中。采用类似Methods的方式 行不通。
Reproducible Code
path: "biz/service/{{ToSnakeCase .ServiceName}}.go" body: |-
package {{.FilePackage}} import ( "context"
"github.com/cloudwego/hertz/pkg/app" ) {{range $_, $Service := .Services}} func {{$Service.Name}}(ctx context.Context, c *app.RequestContext) { // Implement service logic here } {{end}}
Expected behavior
我想将所有的Service都放到一个文件中,但是没有 .Services
Screenshots
If applicable, add screenshots to help explain your question.
Hertz version:
Please provide the version of Hertz you are using.
Environment:
The output of
go env
.Additional context
Add any other context about the question here.