feat: adjust fiber route generation

This commit is contained in:
Rogee
2024-12-23 15:58:17 +08:00
parent 9d1fcdd88b
commit 319b59ce72
44 changed files with 18105 additions and 70 deletions

View File

@@ -1,64 +1,44 @@
package main
import (
"strings"
"regexp"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
type ParamDefinition struct {
Name string
Type string
Key string
Table string
Model string
Position string
}
func Test_router(t *testing.T) {
routerPattern := regexp.MustCompile(`^(/[\w./\-{}\(\)+:$]*)[[:blank:]]+\[(\w+)]`)
func parseBind(bind string) ParamDefinition {
var param ParamDefinition
parts := strings.FieldsFunc(bind, func(r rune) bool {
return r == ' ' || r == '(' || r == ')'
Convey("Test routerPattern", t, func() {
Convey("Pattern 1", func() {
commentLine := "/api/v1/health [GET] # Check health status"
matches := routerPattern.FindStringSubmatch(commentLine)
t.Logf("matches: %v", matches)
})
Convey("Pattern 2", func() {
commentLine := "/api/v1/:health [get] "
matches := routerPattern.FindStringSubmatch(commentLine)
t.Logf("matches: %v", matches)
})
Convey("Pattern 3", func() {
commentLine := "/api/v1/get_users-:id [get] "
pattern := regexp.MustCompile(`<.*?>`)
commentLine = pattern.ReplaceAllString(commentLine, "")
matches := routerPattern.FindStringSubmatch(commentLine)
t.Logf("matches: %v", matches)
})
Convey("Pattern 4", func() {
commentLine := "/api/v1/get_users-:id<int>/name/:name<string> [get] "
pattern := regexp.MustCompile(`:(\w+)(<.*?>)?`)
commentLine = pattern.ReplaceAllString(commentLine, "{$1}")
matches := routerPattern.FindStringSubmatch(commentLine)
t.Logf("matches: %v", matches)
})
})
// 过滤掉空的元素
var newParts []string
for _, part := range parts {
part = strings.TrimSpace(part)
if part != "" {
newParts = append(newParts, part)
}
}
for i, part := range parts {
switch part {
case "@Bind":
param.Name = parts[i+1]
param.Position = parts[i+2]
case "key":
param.Key = parts[i+1]
case "table":
param.Table = parts[i+1]
case "model":
param.Model = parts[i+1]
}
}
return param
}
func Test_T(t *testing.T) {
// @Bind [Name] [Type] [Key] [Table] [Model]
suites := []string{
`@Bind name query key("a") table(b) model("c")`,
`@Bind id uri key(a)`,
`@Bind id uri table(b)`,
`@Bind id uri key(b) model(c)`,
`@Bind id uri key(b) table(c)`,
`@Bind id uri table(b) key(c)`,
`@Bind id uri`,
}
for _, suite := range suites {
param := parseBind(suite)
t.Logf("Parsed Param: %+v", param)
}
}