From 3e986250a7c9c6bb4c156279132d114ee3c97534 Mon Sep 17 00:00:00 2001 From: chenghonour Date: Sun, 10 Mar 2024 21:19:04 +0800 Subject: [PATCH 1/4] optimize(hz):router file insertPointPattern add compatibility with spaces format --- cmd/hz/generator/package_tpl.go | 2 +- cmd/hz/generator/router.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/hz/generator/package_tpl.go b/cmd/hz/generator/package_tpl.go index 7f72b9b91..a03e27824 100644 --- a/cmd/hz/generator/package_tpl.go +++ b/cmd/hz/generator/package_tpl.go @@ -29,7 +29,7 @@ var ( idlClientName = "idl_client.go" // client of service for quick call insertPointNew = "//INSERT_POINT: DO NOT DELETE THIS LINE!" - insertPointPatternNew = `//INSERT_POINT\: DO NOT DELETE THIS LINE\!` + insertPointPatternNew = `//\s?INSERT_POINT\: DO NOT DELETE THIS LINE\!` ) var templateNameSet = map[string]string{ diff --git a/cmd/hz/generator/router.go b/cmd/hz/generator/router.go index 10f431657..42b186efe 100644 --- a/cmd/hz/generator/router.go +++ b/cmd/hz/generator/router.go @@ -327,7 +327,7 @@ func (pkgGen *HttpPackageGenerator) updateRegister(pkg, rDir, pkgName string) er subIndexReg := regRegisterV3.FindSubmatchIndex(file) if len(subIndexReg) != 2 || subIndexReg[0] < 1 { - return fmt.Errorf("wrong format %s: insert-point '%s' not found", string(file), insertPointPatternNew) + return fmt.Errorf("wrong format %s: insert-point '%s' not found", string(file), insertPointNew) } bufReg := bytes.NewBuffer(nil) From 0d8d05d404cf407fb97be74d06d84fd998d051d6 Mon Sep 17 00:00:00 2001 From: chenghonour Date: Thu, 28 Mar 2024 17:21:55 +0800 Subject: [PATCH 2/4] add insertPointPattern unit test --- cmd/hz/generator/package_tpl_test.go | 96 ++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 cmd/hz/generator/package_tpl_test.go diff --git a/cmd/hz/generator/package_tpl_test.go b/cmd/hz/generator/package_tpl_test.go new file mode 100644 index 000000000..5a60dbb5a --- /dev/null +++ b/cmd/hz/generator/package_tpl_test.go @@ -0,0 +1,96 @@ +/* + * Copyright 2022 CloudWeGo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package generator + +import ( + "regexp" + "testing" +) + +func TestMatchInsertPointPatternNew(t *testing.T) { + regRegisterV3 = regexp.MustCompile(insertPointPatternNew) + + ValidPatternFunc := func(s string) bool { + subIndexReg := regRegisterV3.FindSubmatchIndex([]byte(s)) + if len(subIndexReg) != 2 || subIndexReg[0] < 1 { + return false + } + return true + } + + type args struct { + name string + fileContent string + } + tests := []struct { + name string + args args + WantValid bool + }{ + {"TestInsertPointPatternNoSpace", args{"test", `// Code generated by hertz generator. DO NOT EDIT. + +package router + +import ( + admin "export-go/biz/router/admin" + export "export-go/biz/router/export" + "github.com/cloudwego/hertz/pkg/app/server" +) + +// GeneratedRegister registers routers generated by IDL. +func GeneratedRegister(r *server.Hertz) { + //INSERT_POINT: DO NOT DELETE THIS LINE! + admin.Register(r) +}`}, true}, + {"TestInsertPointPatternHasSpace", args{"test", `// Code generated by hertz generator. DO NOT EDIT. + +package router + +import ( + admin "export-go/biz/router/admin" + export "export-go/biz/router/export" + "github.com/cloudwego/hertz/pkg/app/server" +) + +// GeneratedRegister registers routers generated by IDL. +func GeneratedRegister(r *server.Hertz) { + // INSERT_POINT: DO NOT DELETE THIS LINE! + admin.Register(r) +}`}, true}, + {"TestInsertPointPatternWrongFormat", args{"test", `// Code generated by hertz generator. DO NOT EDIT. + +package router + +import ( + admin "export-go/biz/router/admin" + export "export-go/biz/router/export" + "github.com/cloudwego/hertz/pkg/app/server" +) + +// GeneratedRegister registers routers generated by IDL. +func GeneratedRegister(r *server.Hertz) { + admin.Register(r) +}`}, false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := ValidPatternFunc(tt.args.fileContent); got != tt.WantValid { + t.Errorf("wrong format %s: insert-point '%s' not found", tt.args.fileContent, insertPointNew) + } + }) + } +} From 5884b110268a3e1e0214fad4be0bee8309b79dfc Mon Sep 17 00:00:00 2001 From: chenghonour Date: Thu, 28 Mar 2024 17:34:23 +0800 Subject: [PATCH 3/4] Update package_tpl_test.go --- cmd/hz/generator/package_tpl_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/hz/generator/package_tpl_test.go b/cmd/hz/generator/package_tpl_test.go index 5a60dbb5a..7e9e5f24d 100644 --- a/cmd/hz/generator/package_tpl_test.go +++ b/cmd/hz/generator/package_tpl_test.go @@ -22,10 +22,10 @@ import ( ) func TestMatchInsertPointPatternNew(t *testing.T) { - regRegisterV3 = regexp.MustCompile(insertPointPatternNew) - + reg := regexp.MustCompile(insertPointPatternNew) + ValidPatternFunc := func(s string) bool { - subIndexReg := regRegisterV3.FindSubmatchIndex([]byte(s)) + subIndexReg := reg.FindSubmatchIndex([]byte(s)) if len(subIndexReg) != 2 || subIndexReg[0] < 1 { return false } From 4c1fb4fa1795a443fb9fc1d4a098f5ca75bd7aca Mon Sep 17 00:00:00 2001 From: chenghonour Date: Fri, 29 Mar 2024 11:00:35 +0800 Subject: [PATCH 4/4] Update package_tpl_test.go --- cmd/hz/generator/package_tpl_test.go | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/cmd/hz/generator/package_tpl_test.go b/cmd/hz/generator/package_tpl_test.go index 7e9e5f24d..9252a5329 100644 --- a/cmd/hz/generator/package_tpl_test.go +++ b/cmd/hz/generator/package_tpl_test.go @@ -46,8 +46,7 @@ func TestMatchInsertPointPatternNew(t *testing.T) { package router import ( - admin "export-go/biz/router/admin" - export "export-go/biz/router/export" + admin "demo/biz/router/admin" "github.com/cloudwego/hertz/pkg/app/server" ) @@ -61,8 +60,7 @@ func GeneratedRegister(r *server.Hertz) { package router import ( - admin "export-go/biz/router/admin" - export "export-go/biz/router/export" + admin "demo/biz/router/admin" "github.com/cloudwego/hertz/pkg/app/server" ) @@ -76,8 +74,7 @@ func GeneratedRegister(r *server.Hertz) { package router import ( - admin "export-go/biz/router/admin" - export "export-go/biz/router/export" + admin "demo/biz/router/admin" "github.com/cloudwego/hertz/pkg/app/server" )