From bb874305b4504346b58b68c7219a619e41fd8a44 Mon Sep 17 00:00:00 2001 From: ypo2478 Date: Wed, 15 Nov 2023 22:54:05 +0800 Subject: [PATCH 1/2] fix(hz):Append content cannot use template's function --- cmd/hz/generator/custom_files.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/hz/generator/custom_files.go b/cmd/hz/generator/custom_files.go index 1efaae07a..1315948f1 100644 --- a/cmd/hz/generator/custom_files.go +++ b/cmd/hz/generator/custom_files.go @@ -192,7 +192,7 @@ func renderImportTpl(tplInfo *Template, data interface{}) ([]string, error) { // renderAppendContent used to render append content for 'update' command func renderAppendContent(tplInfo *Template, renderInfo interface{}) (string, error) { - tpl, err := template.New(tplInfo.Path).Parse(tplInfo.UpdateBehavior.AppendTpl) + tpl, err := template.New(tplInfo.Path).Funcs(funcMap).Parse(tplInfo.UpdateBehavior.AppendTpl) if err != nil { return "", fmt.Errorf("parse append content template(%s) failed, err: %v", tplInfo.Path, err) } From 71d437491fd4281f5bc14da9e09313b53d2820f4 Mon Sep 17 00:00:00 2001 From: ypo2478 Date: Sat, 18 Nov 2023 14:17:58 +0800 Subject: [PATCH 2/2] optimize(hz): handler_single.go supports the same methods as handler.go --- cmd/hz/generator/handler.go | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/cmd/hz/generator/handler.go b/cmd/hz/generator/handler.go index 2a949c5d6..609e1d6cf 100644 --- a/cmd/hz/generator/handler.go +++ b/cmd/hz/generator/handler.go @@ -57,6 +57,13 @@ type Handler struct { Methods []*HttpMethod } +type SingleHandler struct { + *HttpMethod + FilePath string + PackageName string + ProjPackage string +} + type Client struct { Handler ServiceName string @@ -234,13 +241,12 @@ func (pkgGen *HttpPackageGenerator) updateHandler(handler interface{}, handlerTp if handlerSingleTpl == nil { return fmt.Errorf("tpl %s not found", handlerSingleTplName) } - data := make(map[string]string, 5) - data["Comment"] = method.Comment - data["Name"] = method.Name - data["RequestTypeName"] = method.RequestTypeName - data["ReturnTypeName"] = method.ReturnTypeName - data["Serializer"] = method.Serializer - data["OutputDir"] = method.OutputDir + data := SingleHandler{ + HttpMethod: method, + FilePath: handler.(Handler).FilePath, + PackageName: handler.(Handler).PackageName, + ProjPackage: handler.(Handler).ProjPackage, + } handlerFunc := bytes.NewBuffer(nil) err = handlerSingleTpl.Execute(handlerFunc, data) if err != nil {