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

optimize(hz):router file insertPointPattern add compatibility with spaces format #1077

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion cmd/hz/generator/package_tpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
93 changes: 93 additions & 0 deletions cmd/hz/generator/package_tpl_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* 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) {
reg := regexp.MustCompile(insertPointPatternNew)

ValidPatternFunc := func(s string) bool {
subIndexReg := reg.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 "demo/biz/router/admin"
"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 "demo/biz/router/admin"
"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 "demo/biz/router/admin"
"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)
}
})
}
}
2 changes: 1 addition & 1 deletion cmd/hz/generator/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ func (pkgGen *HttpPackageGenerator) updateRegister(pkg, rDir, pkgName string) er

subIndexReg := regRegisterV3.FindSubmatchIndex(file)
FGYFFFF marked this conversation as resolved.
Show resolved Hide resolved
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)
Expand Down
Loading