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 ( import (
"fmt" "fmt"
"regexp"
"strings" "strings"
pgDatabase "git.ipao.vip/rogeecn/atomctl/pkg/postgres" pgDatabase "git.ipao.vip/rogeecn/atomctl/pkg/postgres"
@@ -57,6 +58,14 @@ func commandGenModelE(cmd *cobra.Command, args []string) error {
return err 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( return postgres.GenerateDSN(
dbConf.DSN(), dbConf.DSN(),
dbConf.Schema, dbConf.Schema,
@@ -94,8 +103,12 @@ func commandGenModelE(cmd *cobra.Command, args []string) error {
return defaultTableModelField return defaultTableModelField
} }
if strings.Contains(toType, "[") && strings.HasSuffix(toType, "]") { // toType = jsonReg.ReplaceAllString(toType, "fields.$1")
toType = strings.Replace(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. defaultTableModelField = defaultTableModelField.

View File

@@ -8,37 +8,24 @@ import (
) )
func Test_router(t *testing.T) { func Test_router(t *testing.T) {
routerPattern := regexp.MustCompile(`^(/[\w./\-{}\(\)+:$]*)[[:blank:]]+\[(\w+)]`)
Convey("Test routerPattern", t, func() { Convey("Test routerPattern", t, func() {
Convey("Pattern 1", func() { jsonReg := regexp.MustCompile(`Json\[\[?\]?(\w+)\]`)
commentLine := "/api/v1/health [GET] # Check health status" items := []string{
matches := routerPattern.FindStringSubmatch(commentLine) "Json[abc]",
t.Logf("matches: %v", matches) "Json[[]abc]",
}) }
Convey("Pattern 2", func() { types := []string{
commentLine := "/api/v1/:health [get] " "string",
matches := routerPattern.FindStringSubmatch(commentLine) "int", "int8", "int16", "int32", "int64", "uint", "uint8", "uint16", "uint32", "uint64",
t.Logf("matches: %v", matches) "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)
})
}) })
} }