Skip to content

Commit

Permalink
chore: release v0.6.4 (#791)
Browse files Browse the repository at this point in the history
  • Loading branch information
welkeyever authored May 25, 2023
2 parents f7b5e2f + 98af7f5 commit aec5711
Show file tree
Hide file tree
Showing 29 changed files with 481 additions and 100 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/invalid_question.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ jobs:
uses: actions/stale@v6
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
stale-issue-message: "This issue has been marked as invalid question, please give more information by following the `issue` template. The issue will be closed in 3 days if no further activity occurs."
stale-issue-message: "This issue has been marked as invalid question, please give more information by following the `issue` template. The issue will be closed in 1 days if no further activity occurs."
stale-issue-label: "stale"
days-before-stale: 0
days-before-close: 3
days-before-close: 1
remove-stale-when-updated: true
only-labels: "invalid issue"
7 changes: 7 additions & 0 deletions cmd/hz/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ func Init() *cli.App {
protoPluginsFlag := cli.StringSliceFlag{Name: "protoc-plugins", Usage: "Specify plugins for the protoc. ({plugin_name}:{options}:{out_dir})"}
noRecurseFlag := cli.BoolFlag{Name: "no_recurse", Usage: "Generate master model only.", Destination: &globalArgs.NoRecurse}
forceNewFlag := cli.BoolFlag{Name: "force", Aliases: []string{"f"}, Usage: "Force new a project, which will overwrite the generated files", Destination: &globalArgs.ForceNew}
enableExtendsFlag := cli.BoolFlag{Name: "enable_extends", Usage: "Parse 'extends' for thrift IDL", Destination: &globalArgs.EnableExtends}

jsonEnumStrFlag := cli.BoolFlag{Name: "json_enumstr", Usage: "Use string instead of num for json enums when idl is thrift.", Destination: &globalArgs.JSONEnumStr}
unsetOmitemptyFlag := cli.BoolFlag{Name: "unset_omitempty", Usage: "Remove 'omitempty' tag for generated struct.", Destination: &globalArgs.UnsetOmitempty}
Expand Down Expand Up @@ -223,6 +224,7 @@ func Init() *cli.App {
&optPkgFlag,
&noRecurseFlag,
&forceNewFlag,
&enableExtendsFlag,

&jsonEnumStrFlag,
&unsetOmitemptyFlag,
Expand Down Expand Up @@ -255,6 +257,7 @@ func Init() *cli.App {
&protoOptionsFlag,
&optPkgFlag,
&noRecurseFlag,
&enableExtendsFlag,

&jsonEnumStrFlag,
&unsetOmitemptyFlag,
Expand Down Expand Up @@ -306,12 +309,16 @@ func Init() *cli.App {
&thriftOptionsFlag,
&protoOptionsFlag,
&noRecurseFlag,
&enableExtendsFlag,

&jsonEnumStrFlag,
&unsetOmitemptyFlag,
&protoCamelJSONTag,
&snakeNameFlag,
&excludeFilesFlag,
&customLayout,
&customLayoutData,
&customPackage,
&protoPluginsFlag,
&thriftPluginsFlag,
},
Expand Down
1 change: 1 addition & 0 deletions cmd/hz/config/argument.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ type Argument struct {
HandlerByMethod bool
ForceNew bool
SnakeStyleMiddleware bool
EnableExtends bool

CustomizeLayout string
CustomizeLayoutData string
Expand Down
131 changes: 100 additions & 31 deletions cmd/hz/generator/custom_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,14 @@ func appendUpdateFile(tplInfo *Template, renderInfo interface{}, fileContent []b
if err != nil {
return []byte(""), fmt.Errorf("write file(%s) failed, err: %v", tplInfo.Path, err)
}
_, err = buf.WriteString("\n" + appendContent + "\n")
// "\r\n" && "\n" has the same suffix
if !bytes.HasSuffix(buf.Bytes(), []byte("\n")) {
_, err = buf.WriteString("\n")
if err != nil {
return []byte(""), fmt.Errorf("write file(%s) line break failed, err: %v", tplInfo.Path, err)
}
}
_, err = buf.WriteString(appendContent)
if err != nil {
return []byte(""), fmt.Errorf("append file(%s) failed, err: %v", tplInfo.Path, err)
}
Expand Down Expand Up @@ -348,17 +355,34 @@ func (pkgGen *HttpPackageGenerator) genLoopService(tplInfo *Template, filePathRe
return err
}
}
buf := bytes.NewBuffer(nil)
_, err = buf.Write(fileContent)
if err != nil {
return fmt.Errorf("write file(%s) failed, err: %v", tplInfo.Path, err)
}
_, err = buf.Write(appendContent)
if err != nil {
return fmt.Errorf("append file(%s) failed, err: %v", tplInfo.Path, err)
if len(tplInfo.UpdateBehavior.AppendLocation) == 0 { // default, append to end of file
buf := bytes.NewBuffer(nil)
_, err = buf.Write(fileContent)
if err != nil {
return fmt.Errorf("write file(%s) failed, err: %v", tplInfo.Path, err)
}
_, err = buf.Write(appendContent)
if err != nil {
return fmt.Errorf("append file(%s) failed, err: %v", tplInfo.Path, err)
}
logs.Infof("append content for file '%s', because the update behavior is 'Append' and appendKey is 'method'", filePath)
pkgGen.files = append(pkgGen.files, File{filePath, buf.String(), false, ""})
} else { // 'append location', append new content after 'append location'
part := bytes.Split(fileContent, []byte(tplInfo.UpdateBehavior.AppendLocation))
if len(part) == 0 {
return fmt.Errorf("can not find append location '%s' for file '%s'\n", tplInfo.UpdateBehavior.AppendLocation, filePath)
}
if len(part) != 2 {
return fmt.Errorf("do not support multiple append location '%s' for file '%s'\n", tplInfo.UpdateBehavior.AppendLocation, filePath)
}
buf := bytes.NewBuffer(nil)
err = writeBytes(buf, part[0], []byte(tplInfo.UpdateBehavior.AppendLocation), appendContent, part[1])
if err != nil {
return fmt.Errorf("write file(%s) failed, err: %v", tplInfo.Path, err)
}
logs.Infof("append content for file '%s', because the update behavior is 'Append' and appendKey is 'method'", filePath)
pkgGen.files = append(pkgGen.files, File{filePath, buf.String(), false, ""})
}
logs.Infof("append content for file '%s', because the update behavior is 'Append' and appendKey is 'method'", filePath)
pkgGen.files = append(pkgGen.files, File{filePath, buf.String(), false, ""})
} else {
logs.Warnf("Loop 'service' field for '%s' only append content by appendKey for 'method', so cannot append content", filePath)
}
Expand Down Expand Up @@ -505,17 +529,34 @@ func (pkgGen *HttpPackageGenerator) genSingleCustomizedFile(tplInfo *Template, f
}
}
}
buf := bytes.NewBuffer(nil)
_, err = buf.Write(fileContent)
if err != nil {
return fmt.Errorf("write file(%s) failed, err: %v", tplInfo.Path, err)
}
_, err = buf.Write(appendContent)
if err != nil {
return fmt.Errorf("append file(%s) failed, err: %v", tplInfo.Path, err)
if len(tplInfo.UpdateBehavior.AppendLocation) == 0 { // default, append to end of file
buf := bytes.NewBuffer(nil)
_, err = buf.Write(fileContent)
if err != nil {
return fmt.Errorf("write file(%s) failed, err: %v", tplInfo.Path, err)
}
_, err = buf.Write(appendContent)
if err != nil {
return fmt.Errorf("append file(%s) failed, err: %v", tplInfo.Path, err)
}
logs.Infof("append content for file '%s', because the update behavior is 'Append' and appendKey is 'method'", filePath)
pkgGen.files = append(pkgGen.files, File{filePath, buf.String(), false, ""})
} else { // 'append location', append new content after 'append location'
part := bytes.Split(fileContent, []byte(tplInfo.UpdateBehavior.AppendLocation))
if len(part) == 0 {
return fmt.Errorf("can not find append location '%s' for file '%s'\n", tplInfo.UpdateBehavior.AppendLocation, filePath)
}
if len(part) != 2 {
return fmt.Errorf("do not support multiple append location '%s' for file '%s'\n", tplInfo.UpdateBehavior.AppendLocation, filePath)
}
buf := bytes.NewBuffer(nil)
err = writeBytes(buf, part[0], []byte(tplInfo.UpdateBehavior.AppendLocation), appendContent, part[1])
if err != nil {
return fmt.Errorf("write file(%s) failed, err: %v", tplInfo.Path, err)
}
logs.Infof("append content for file '%s', because the update behavior is 'Append' and appendKey is 'method'", filePath)
pkgGen.files = append(pkgGen.files, File{filePath, buf.String(), false, ""})
}
logs.Infof("append content for file '%s', because the update behavior is 'Append' and appendKey is 'method'", filePath)
pkgGen.files = append(pkgGen.files, File{filePath, buf.String(), false, ""})
} else if tplInfo.UpdateBehavior.AppendKey == "service" {
var appendContent []byte
for _, service := range idlPackageRenderInfo.ServiceInfos.Services {
Expand Down Expand Up @@ -550,17 +591,34 @@ func (pkgGen *HttpPackageGenerator) genSingleCustomizedFile(tplInfo *Template, f
return err
}
}
buf := bytes.NewBuffer(nil)
_, err = buf.Write(fileContent)
if err != nil {
return fmt.Errorf("write file(%s) failed, err: %v", tplInfo.Path, err)
}
_, err = buf.Write(appendContent)
if err != nil {
return fmt.Errorf("append file(%s) failed, err: %v", tplInfo.Path, err)
if len(tplInfo.UpdateBehavior.AppendLocation) == 0 { // default, append to end of file
buf := bytes.NewBuffer(nil)
_, err = buf.Write(fileContent)
if err != nil {
return fmt.Errorf("write file(%s) failed, err: %v", tplInfo.Path, err)
}
_, err = buf.Write(appendContent)
if err != nil {
return fmt.Errorf("append file(%s) failed, err: %v", tplInfo.Path, err)
}
logs.Infof("append content for file '%s', because the update behavior is 'Append' and appendKey is 'service'", filePath)
pkgGen.files = append(pkgGen.files, File{filePath, buf.String(), false, ""})
} else { // 'append location', append new content after 'append location'
part := bytes.Split(fileContent, []byte(tplInfo.UpdateBehavior.AppendLocation))
if len(part) == 0 {
return fmt.Errorf("can not find append location '%s' for file '%s'\n", tplInfo.UpdateBehavior.AppendLocation, filePath)
}
if len(part) != 2 {
return fmt.Errorf("do not support multiple append location '%s' for file '%s'\n", tplInfo.UpdateBehavior.AppendLocation, filePath)
}
buf := bytes.NewBuffer(nil)
err = writeBytes(buf, part[0], []byte(tplInfo.UpdateBehavior.AppendLocation), appendContent, part[1])
if err != nil {
return fmt.Errorf("write file(%s) failed, err: %v", tplInfo.Path, err)
}
logs.Infof("append content for file '%s', because the update behavior is 'Append' and appendKey is 'service'", filePath)
pkgGen.files = append(pkgGen.files, File{filePath, buf.String(), false, ""})
}
logs.Infof("append content for file '%s', because the update behavior is 'Append' and appendKey is 'service'", filePath)
pkgGen.files = append(pkgGen.files, File{filePath, buf.String(), false, ""})
} else { // add append content to the file directly
data := CustomizedFileForIDL{
IDLPackageRenderInfo: &idlPackageRenderInfo,
Expand All @@ -581,3 +639,14 @@ func (pkgGen *HttpPackageGenerator) genSingleCustomizedFile(tplInfo *Template, f

return nil
}

func writeBytes(buf *bytes.Buffer, bytes ...[]byte) error {
for _, b := range bytes {
_, err := buf.Write(b)
if err != nil {
return err
}
}

return nil
}
21 changes: 19 additions & 2 deletions cmd/hz/generator/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ type HttpMethod struct {
Path string
Serializer string
OutputDir string
RefPackage string
RefPackageAlias string
ModelPackage map[string]string
// Annotations map[string]string
Models map[string]*model.Model
}
Expand Down Expand Up @@ -76,13 +79,24 @@ func (pkgGen *HttpPackageGenerator) genHandler(pkg *HttpPackage, handlerDir, han
}
}
} else { // generate handler service
tmpHandlerDir := handlerDir
tmpHandlerPackage := handlerPackage
if len(s.ServiceGenDir) != 0 {
tmpHandlerDir = s.ServiceGenDir
tmpHandlerPackage = util.SubPackage(pkgGen.ProjPackage, tmpHandlerDir)
}
handler = Handler{
FilePath: filepath.Join(handlerDir, util.ToSnakeCase(s.Name)+".go"),
PackageName: util.SplitPackage(handlerPackage, ""),
FilePath: filepath.Join(tmpHandlerDir, util.ToSnakeCase(s.Name)+".go"),
PackageName: util.SplitPackage(tmpHandlerPackage, ""),
Methods: s.Methods,
ProjPackage: pkgGen.ProjPackage,
}

for _, m := range s.Methods {
m.RefPackage = tmpHandlerPackage
m.RefPackageAlias = util.BaseName(tmpHandlerPackage, "")
}

if err := pkgGen.processHandler(&handler, root, "", "", false); err != nil {
return fmt.Errorf("generate handler %s failed, err: %v", handler.FilePath, err.Error())
}
Expand Down Expand Up @@ -145,6 +159,9 @@ func (pkgGen *HttpPackageGenerator) processHandler(handler *Handler, root *Route
}

func (pkgGen *HttpPackageGenerator) updateHandler(handler interface{}, handlerTpl, filePath string, noRepeat bool) error {
if pkgGen.tplsInfo[handlerTpl].Disable {
return nil
}
isExist, err := util.PathExist(filePath)
if err != nil {
return err
Expand Down
3 changes: 3 additions & 0 deletions cmd/hz/generator/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ type Service struct {
ClientMethods []*ClientMethod
Models []*model.Model // all dependency models
BaseDomain string // base domain for client code
ServiceGroup string // service level router group
ServiceGenDir string // handler_dir for handler_by_service
}

// HttpPackageGenerator is used to record the configuration related to generating hertz http code.
Expand All @@ -61,6 +63,7 @@ type HttpPackageGenerator struct {
IdlClientDir string // client dir for "client" command
ForceClientDir string // client dir without namespace for "client" command
BaseDomain string // request domain for "client" command
ServiceGenDir string

NeedModel bool
HandlerByMethod bool // generate handler files with method dimension
Expand Down
2 changes: 1 addition & 1 deletion cmd/hz/generator/package_tpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,7 @@ func parseResponseBody(c *cli, res *response) (err error) {
}
} else {
jsonByte, jsonErr := json.Marshal(map[string]interface{}{
"status_code": res.rawResponse.StatusCode,
"status_code": res.rawResponse.StatusCode(),
"body": string(res.bodyByte),
})
if jsonErr != nil {
Expand Down
20 changes: 17 additions & 3 deletions cmd/hz/generator/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ func (routerNode *RouterNode) Insert(name string, method *HttpMethod, handlerTyp
c.HandlerPackageAlias = pkgAlias
c.Handler = pkgAlias + "." + method.Name
c.HandlerPackage = handlerPkg
method.RefPackage = c.HandlerPackage
method.RefPackageAlias = c.HandlerPackageAlias
} else { // generate handler by service
c.Handler = handlerType + "." + method.Name
}
Expand Down Expand Up @@ -290,6 +292,9 @@ var (
)

func (pkgGen *HttpPackageGenerator) updateRegister(pkg, rDir, pkgName string) error {
if pkgGen.tplsInfo[registerTplName].Disable {
return nil
}
register := RegisterInfo{
PackageName: filepath.Base(rDir),
DepPkgAlias: strings.ReplaceAll(pkgName, "/", "_"),
Expand Down Expand Up @@ -365,8 +370,10 @@ func (pkgGen *HttpPackageGenerator) genRouter(pkg *HttpPackage, root *RouterNode
// store router info
pkg.RouterInfo = &router

if err := pkgGen.TemplateGenerator.Generate(router, routerTplName, router.FilePath, false); err != nil {
return fmt.Errorf("generate router %s failed, err: %v", router.FilePath, err.Error())
if !pkgGen.tplsInfo[routerTplName].Disable {
if err := pkgGen.TemplateGenerator.Generate(router, routerTplName, router.FilePath, false); err != nil {
return fmt.Errorf("generate router %s failed, err: %v", router.FilePath, err.Error())
}
}
if err := pkgGen.updateMiddlewareReg(router, middlewareTplName, filepath.Join(routerDir, "middleware.go")); err != nil {
return fmt.Errorf("generate middleware %s failed, err: %v", filepath.Join(routerDir, "middleware.go"), err.Error())
Expand All @@ -379,6 +386,9 @@ func (pkgGen *HttpPackageGenerator) genRouter(pkg *HttpPackage, root *RouterNode
}

func (pkgGen *HttpPackageGenerator) updateMiddlewareReg(router interface{}, middlewareTpl, filePath string) error {
if pkgGen.tplsInfo[middlewareTpl].Disable {
return nil
}
isExist, err := util.PathExist(filePath)
if err != nil {
return err
Expand All @@ -405,7 +415,11 @@ func (pkgGen *HttpPackageGenerator) updateMiddlewareReg(router interface{}, midd
}

for _, mw := range middlewareList {
if bytes.Contains(file, []byte(mw)) {
mwName := mw + "Mw"
if pkgGen.SnakeStyleMiddleware {
mwName = mw + "_mw"
}
if bytes.Contains(file, []byte(mwName)) {
continue
}
middlewareSingleTpl := pkgGen.tpls[middlewareSingleTplName]
Expand Down
14 changes: 8 additions & 6 deletions cmd/hz/generator/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,24 @@ const (
)

type Template struct {
Default bool // Update command behavior; skip/cover/append
Default bool // Is it the default template
Path string `yaml:"path"` // The generated path and its filename, such as biz/handler/ping.go
Delims [2]string `yaml:"delims"` // Template Action Instruction Identifier, default: "{{}}"
Body string `yaml:"body"` // Render template, currently only supports go template syntax
Disable bool `yaml:"disable"` // Disable generating file, used to disable default package template
LoopMethod bool `yaml:"loop_method"` // Loop generate files based on "method"
LoopService bool `yaml:"loop_service"` // Loop generate files based on "service"
UpdateBehavior UpdateBehavior `yaml:"update_behavior"` // Update command behavior; 0:unchanged, 1:regenerate, 2:append
}

type UpdateBehavior struct {
Type string `yaml:"type"` // Update behavior type; skip/cover/append
Type string `yaml:"type"` // Update behavior type: skip/cover/append
// the following variables are used for append update
AppendKey string `yaml:"append_key"` // Append content based in key; for example: 'method'/'service'
InsertKey string `yaml:"insert_key"` // Insert content by "insert_key"
AppendTpl string `yaml:"append_content_tpl"` // Append content if UpdateBehavior is "append"
ImportTpl []string `yaml:"import_tpl"` // Import insert template
AppendKey string `yaml:"append_key"` // Append content based in key; for example: 'method'/'service'
InsertKey string `yaml:"insert_key"` // Insert content by "insert_key"
AppendTpl string `yaml:"append_content_tpl"` // Append content if UpdateBehavior is "append"
ImportTpl []string `yaml:"import_tpl"` // Import insert template
AppendLocation string `yaml:"append_location"` // AppendLocation specifies the location of append, the default is the end of the file
}

// TemplateGenerator contains information about the output template
Expand Down
2 changes: 1 addition & 1 deletion cmd/hz/meta/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package meta
import "runtime"

// Version hz version
const Version = "v0.6.3"
const Version = "v0.6.4"

// Mode hz run modes
type Mode int
Expand Down
Loading

0 comments on commit aec5711

Please sign in to comment.