diff --git a/cmd/gen_model.go b/cmd/gen_model.go index 92f6bfb..ed3092a 100644 --- a/cmd/gen_model.go +++ b/cmd/gen_model.go @@ -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. diff --git a/main_test.go b/main_test.go index 3fc58b6..dbc8a9c 100644 --- a/main_test.go +++ b/main_test.go @@ -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/name/:name [get] " - pattern := regexp.MustCompile(`:(\w+)(<.*?>)?`) - commentLine = pattern.ReplaceAllString(commentLine, "{$1}") - - matches := routerPattern.FindStringSubmatch(commentLine) - t.Logf("matches: %v", matches) - }) + } + } }) }