fix: gen model

This commit is contained in:
Rogee
2025-01-15 14:48:51 +08:00
parent 1e03cf6038
commit b5d6593fa4
2 changed files with 31 additions and 31 deletions

View File

@@ -2,6 +2,7 @@ package cmd
import (
"fmt"
"regexp"
"strings"
pgDatabase "git.ipao.vip/rogeecn/atomctl/pkg/postgres"
@@ -57,6 +58,14 @@ func commandGenModelE(cmd *cobra.Command, args []string) error {
return err
}
jsonReg := regexp.MustCompile(`Json\[\[?\]?(\w+)\]`)
builtinTypes := []string{
"string",
"int", "int8", "int16", "int32", "int64", "uint", "uint8", "uint16", "uint32", "uint64",
"float32", "float64",
"bool",
}
return postgres.GenerateDSN(
dbConf.DSN(),
dbConf.Schema,
@@ -94,8 +103,12 @@ func commandGenModelE(cmd *cobra.Command, args []string) error {
return defaultTableModelField
}
if strings.Contains(toType, "[") && strings.HasSuffix(toType, "]") {
toType = strings.Replace(toType, "[", "[fields.", 1)
// toType = jsonReg.ReplaceAllString(toType, "fields.$1")
if jsonReg.MatchString(toType) {
matches := jsonReg.FindStringSubmatch(toType)
if len(matches) == 2 && !lo.Contains(builtinTypes, matches[1]) {
toType = strings.Replace(toType, matches[1], "fields."+matches[1], 1)
}
}
defaultTableModelField = defaultTableModelField.

View File

@@ -8,37 +8,24 @@ import (
)
func Test_router(t *testing.T) {
routerPattern := regexp.MustCompile(`^(/[\w./\-{}\(\)+:$]*)[[:blank:]]+\[(\w+)]`)
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)
})
jsonReg := regexp.MustCompile(`Json\[\[?\]?(\w+)\]`)
items := []string{
"Json[abc]",
"Json[[]abc]",
}
Convey("Pattern 2", func() {
commentLine := "/api/v1/:health [get] "
matches := routerPattern.FindStringSubmatch(commentLine)
t.Logf("matches: %v", matches)
})
types := []string{
"string",
"int", "int8", "int16", "int32", "int64", "uint", "uint8", "uint16", "uint32", "uint64",
"float32", "float64",
"bool",
}
for _, item := range items {
match := jsonReg.FindStringSubmatch(item)
if len(match) ==2 && !lo.Contains(types, match[1]) { {
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)
})
}
}
})
}