From a0f7af3da5b0353d43d4ec81fd3e662782a98c33 Mon Sep 17 00:00:00 2001 From: kevinzhang Date: Tue, 27 Feb 2024 14:35:08 +0800 Subject: [PATCH 1/2] feat: add append_direction into update_behavior --- cmd/hz/generator/custom_files.go | 24 ++++++++++++++++++------ cmd/hz/generator/template.go | 11 ++++++----- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/cmd/hz/generator/custom_files.go b/cmd/hz/generator/custom_files.go index 1315948f1..ba28727c1 100644 --- a/cmd/hz/generator/custom_files.go +++ b/cmd/hz/generator/custom_files.go @@ -369,7 +369,7 @@ func (pkgGen *HttpPackageGenerator) genLoopService(tplInfo *Template, filePathRe } 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' + } else { // 'append location', append new content before or 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) @@ -378,7 +378,11 @@ func (pkgGen *HttpPackageGenerator) genLoopService(tplInfo *Template, filePathRe 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 tplInfo.UpdateBehavior.AppendDirection == "before" { + err = writeBytes(buf, part[0], appendContent, []byte(tplInfo.UpdateBehavior.AppendLocation), part[1]) + } else { + 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) } @@ -546,7 +550,7 @@ func (pkgGen *HttpPackageGenerator) genSingleCustomizedFile(tplInfo *Template, f } 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' + } else { // 'append location', append new content before or 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) @@ -555,7 +559,11 @@ func (pkgGen *HttpPackageGenerator) genSingleCustomizedFile(tplInfo *Template, f 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 tplInfo.UpdateBehavior.AppendDirection == "before" { + err = writeBytes(buf, part[0], appendContent, []byte(tplInfo.UpdateBehavior.AppendLocation), part[1]) + } else { + 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) } @@ -608,7 +616,7 @@ func (pkgGen *HttpPackageGenerator) genSingleCustomizedFile(tplInfo *Template, f } 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' + } else { // 'append location', append new content before or 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) @@ -617,7 +625,11 @@ func (pkgGen *HttpPackageGenerator) genSingleCustomizedFile(tplInfo *Template, f 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 tplInfo.UpdateBehavior.AppendDirection == "before" { + err = writeBytes(buf, part[0], appendContent, []byte(tplInfo.UpdateBehavior.AppendLocation), part[1]) + } else { + 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) } diff --git a/cmd/hz/generator/template.go b/cmd/hz/generator/template.go index b339fbbd2..cfee632e1 100644 --- a/cmd/hz/generator/template.go +++ b/cmd/hz/generator/template.go @@ -56,11 +56,12 @@ type Template struct { type UpdateBehavior struct { 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 - AppendLocation string `yaml:"append_location"` // AppendLocation specifies the location of append, the default is the end of the file + 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 + AppendDirection string `yaml:"append_direction"` // AppendDirection specifies the direction of append, the default is "after", and the other option is "before" } // TemplateGenerator contains information about the output template From 1cc97a524575857acacb4b68ed29cbf94c297407 Mon Sep 17 00:00:00 2001 From: kevinzhang Date: Wed, 20 Mar 2024 11:58:12 +0800 Subject: [PATCH 2/2] style: Add `before`, `after` constants --- cmd/hz/generator/custom_files.go | 6 +++--- cmd/hz/generator/template.go | 2 ++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/cmd/hz/generator/custom_files.go b/cmd/hz/generator/custom_files.go index ba28727c1..5c7fcb0b9 100644 --- a/cmd/hz/generator/custom_files.go +++ b/cmd/hz/generator/custom_files.go @@ -378,7 +378,7 @@ func (pkgGen *HttpPackageGenerator) genLoopService(tplInfo *Template, filePathRe return fmt.Errorf("do not support multiple append location '%s' for file '%s'\n", tplInfo.UpdateBehavior.AppendLocation, filePath) } buf := bytes.NewBuffer(nil) - if tplInfo.UpdateBehavior.AppendDirection == "before" { + if tplInfo.UpdateBehavior.AppendDirection == Before { err = writeBytes(buf, part[0], appendContent, []byte(tplInfo.UpdateBehavior.AppendLocation), part[1]) } else { err = writeBytes(buf, part[0], []byte(tplInfo.UpdateBehavior.AppendLocation), appendContent, part[1]) @@ -559,7 +559,7 @@ func (pkgGen *HttpPackageGenerator) genSingleCustomizedFile(tplInfo *Template, f return fmt.Errorf("do not support multiple append location '%s' for file '%s'\n", tplInfo.UpdateBehavior.AppendLocation, filePath) } buf := bytes.NewBuffer(nil) - if tplInfo.UpdateBehavior.AppendDirection == "before" { + if tplInfo.UpdateBehavior.AppendDirection == Before { err = writeBytes(buf, part[0], appendContent, []byte(tplInfo.UpdateBehavior.AppendLocation), part[1]) } else { err = writeBytes(buf, part[0], []byte(tplInfo.UpdateBehavior.AppendLocation), appendContent, part[1]) @@ -625,7 +625,7 @@ func (pkgGen *HttpPackageGenerator) genSingleCustomizedFile(tplInfo *Template, f return fmt.Errorf("do not support multiple append location '%s' for file '%s'\n", tplInfo.UpdateBehavior.AppendLocation, filePath) } buf := bytes.NewBuffer(nil) - if tplInfo.UpdateBehavior.AppendDirection == "before" { + if tplInfo.UpdateBehavior.AppendDirection == Before { err = writeBytes(buf, part[0], appendContent, []byte(tplInfo.UpdateBehavior.AppendLocation), part[1]) } else { err = writeBytes(buf, part[0], []byte(tplInfo.UpdateBehavior.AppendLocation), appendContent, part[1]) diff --git a/cmd/hz/generator/template.go b/cmd/hz/generator/template.go index cfee632e1..ee520b7b7 100644 --- a/cmd/hz/generator/template.go +++ b/cmd/hz/generator/template.go @@ -40,6 +40,8 @@ const ( Skip = "skip" Cover = "cover" Append = "append" + Before = "before" + After = "after" ) type Template struct {