Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(hz): add more template parameters #795

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 27 additions & 22 deletions cmd/hz/generator/custom_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,17 @@ type IDLPackageRenderInfo struct {

type CustomizedFileForMethod struct {
*HttpMethod
FilePath string
FilePackage string
ServiceInfo *Service // service info for that method
FilePath string
FilePackage string
ServiceInfo *Service // service info for this method
IDLPackageInfo *IDLPackageRenderInfo // IDL info for this service
}

type CustomizedFileForService struct {
*Service
FilePath string
FilePackage string
IDLPackageInfo *IDLPackageRenderInfo // IDL info for that service
IDLPackageInfo *IDLPackageRenderInfo // IDL info for this service
}

type CustomizedFileForIDL struct {
Expand Down Expand Up @@ -110,7 +111,7 @@ func (pkgGen *HttpPackageGenerator) genCustomizedFile(pkg *HttpPackage) error {
filePathRenderInfo.ServiceName = service.Name
filePathRenderInfo.MethodName = method.Name
filePathRenderInfo.HandlerGenPath = method.OutputDir
err := pkgGen.genLoopMethod(tplInfo, filePathRenderInfo, method, service)
err := pkgGen.genLoopMethod(tplInfo, filePathRenderInfo, method, service, &idlPackageRenderInfo)
if err != nil {
return err
}
Expand Down Expand Up @@ -323,10 +324,11 @@ func (pkgGen *HttpPackageGenerator) genLoopService(tplInfo *Template, filePathRe
if tplInfo.UpdateBehavior.AppendKey == "method" {
for _, method := range service.Methods {
data := CustomizedFileForMethod{
HttpMethod: method,
FilePath: filePath,
FilePackage: util.SplitPackage(filepath.Dir(filePath), ""),
ServiceInfo: service,
HttpMethod: method,
FilePath: filePath,
FilePackage: util.SplitPackage(filepath.Dir(filePath), ""),
ServiceInfo: service,
IDLPackageInfo: idlPackageRenderInfo,
}
insertKey, err := renderInsertKey(tplInfo, data)
if err != nil {
Expand Down Expand Up @@ -395,7 +397,7 @@ func (pkgGen *HttpPackageGenerator) genLoopService(tplInfo *Template, filePathRe
}

// genLoopMethod used to generate files by 'method'
func (pkgGen *HttpPackageGenerator) genLoopMethod(tplInfo *Template, filePathRenderInfo FilePathRenderInfo, method *HttpMethod, service *Service) error {
func (pkgGen *HttpPackageGenerator) genLoopMethod(tplInfo *Template, filePathRenderInfo FilePathRenderInfo, method *HttpMethod, service *Service, idlPackageRenderInfo *IDLPackageRenderInfo) error {
filePath, err := renderFilePath(tplInfo, filePathRenderInfo)
if err != nil {
return err
Expand All @@ -408,10 +410,11 @@ func (pkgGen *HttpPackageGenerator) genLoopMethod(tplInfo *Template, filePathRen

if !exist { // create file
data := CustomizedFileForMethod{
HttpMethod: method,
FilePath: filePath,
FilePackage: util.SplitPackage(filepath.Dir(filePath), ""),
ServiceInfo: service,
HttpMethod: method,
FilePath: filePath,
FilePackage: util.SplitPackage(filepath.Dir(filePath), ""),
ServiceInfo: service,
IDLPackageInfo: idlPackageRenderInfo,
}
err := pkgGen.TemplateGenerator.Generate(data, tplInfo.Path, filePath, false)
if err != nil {
Expand All @@ -426,10 +429,11 @@ func (pkgGen *HttpPackageGenerator) genLoopMethod(tplInfo *Template, filePathRen
// re-generate
logs.Infof("re-generate file '%s', because the update behavior is 'Regenerate'", filePath)
data := CustomizedFileForMethod{
HttpMethod: method,
FilePath: filePath,
FilePackage: util.SplitPackage(filepath.Dir(filePath), ""),
ServiceInfo: service,
HttpMethod: method,
FilePath: filePath,
FilePackage: util.SplitPackage(filepath.Dir(filePath), ""),
ServiceInfo: service,
IDLPackageInfo: idlPackageRenderInfo,
}
err := pkgGen.TemplateGenerator.Generate(data, tplInfo.Path, filePath, false)
if err != nil {
Expand Down Expand Up @@ -497,10 +501,11 @@ func (pkgGen *HttpPackageGenerator) genSingleCustomizedFile(tplInfo *Template, f
for _, service := range idlPackageRenderInfo.ServiceInfos.Services {
for _, method := range service.Methods {
data := CustomizedFileForMethod{
HttpMethod: method,
FilePath: filePath,
FilePackage: util.SplitPackage(filepath.Dir(filePath), ""),
ServiceInfo: service,
HttpMethod: method,
FilePath: filePath,
FilePackage: util.SplitPackage(filepath.Dir(filePath), ""),
ServiceInfo: service,
IDLPackageInfo: &idlPackageRenderInfo,
}
insertKey, err := renderInsertKey(tplInfo, data)
if err != nil {
Expand Down