完成AllModels序列化

This commit is contained in:
eson
2023-06-21 12:21:56 +08:00
parent 389f12b8c5
commit 7fe53f749e
9 changed files with 260 additions and 34 deletions

View File

@@ -1,16 +1,24 @@
package main
import (
"log"
"regexp"
"testing"
_ "github.com/go-sql-driver/mysql"
)
func TestXMain(t *testing.T) {
testGenDir = "../" + testGenDir
GenAllModels(testGenDir, TableNameComment{
Name: "FsFont",
Comment: "测试",
})
a := " FsAddress *FsAddressModel // fs_address 用户地址表"
re := regexp.MustCompile(`([A-Za-z0-9_]+) [^/]+ // ([^ ]+) (.+)$`)
for _, line := range re.FindStringSubmatch(a) {
log.Println(line)
}
log.Println(re.FindStringSubmatch(a))
// testGenDir = "../" + testGenDir
// GenAllModels(testGenDir, TableNameComment{
// Name: "FsFont",
// Comment: "测试",
// })
// Now you can use the generated GORM model to interact with the database
}

View File

@@ -8,6 +8,7 @@ import (
"os"
"os/exec"
"regexp"
"sort"
"strconv"
"strings"
@@ -70,6 +71,7 @@ package gmodel
import "gorm.io/gorm"
// AllModelsGen 所有Model集合,修改单行,只要不改字段名,不会根据新的内容修改,需要修改的话手动删除
type AllModelsGen struct {
}
@@ -85,6 +87,7 @@ package gmodel
import "gorm.io/gorm"
// AllModelsGen 所有Model集合,修改单行,只要不改字段名,不会根据新的内容修改,需要修改的话手动删除
type AllModelsGen struct {
%s
}
@@ -103,34 +106,71 @@ type TableNameComment struct {
Comment string
}
type TMCS []TableNameComment
func (u TMCS) Len() int {
return len(u)
}
func (u TMCS) Less(i, j int) bool {
return u[i].Name < u[j].Name
}
func (u TMCS) Swap(i, j int) {
u[i], u[j] = u[j], u[i]
}
func GenAllModels(filedir string, tmcs ...TableNameComment) {
fileName := filedir + "/var_gen.go"
var dupMap map[string]TableNameComment = make(map[string]TableNameComment)
for _, tmc := range tmcs {
dupMap[tmc.Name] = tmc
}
if _, err := os.Stat(fileName); err == nil {
log.Printf("%s exists!", fileName)
data, err := os.ReadFile(fileName)
if err != nil {
panic(err)
}
filestr := string(data)
filelines := strings.Split(filestr, "\n")
re := regexp.MustCompile(` +([^:]+)[^/]+ // ([^ ]+) (.?)$`)
re := regexp.MustCompile(`([A-Za-z0-9_]+) [^/]+ // ([^ ]+) (.+)$`)
for _, line := range filelines {
result := re.FindStringSubmatch(line)
if len(result) > 0 {
// key := result[0]
if len(result) != 4 {
log.Println(result)
}
log.Println(result)
tmcs = append(tmcs, TableNameComment{
Name: result[1],
GoName: result[0],
Comment: result[2],
})
tmc := TableNameComment{
Name: result[2],
GoName: result[1],
Comment: result[3],
}
if newTmc, ok := dupMap[tmc.Name]; ok {
log.Printf("not change: (old)%v -> (new)%v", tmc, newTmc)
}
dupMap[tmc.Name] = tmc
}
}
tmcs = nil
for _, tmc := range dupMap {
tmcs = append(tmcs, tmc)
}
sort.Sort(TMCS(tmcs))
structStr := ""
newModelsStr := ""
for _, tmc := range tmcs {
fsline := fmt.Sprintf("%s %sModel // %s %s\n", tmc.GoName, tmc.GoName, tmc.Name, tmc.Comment)
fsline := fmt.Sprintf("%s *%sModel // %s %s\n", tmc.GoName, tmc.GoName, tmc.Name, tmc.Comment)
structStr += fsline
nmline := fmt.Sprintf("%s: New%sModel(gdb),\n", tmc.GoName, tmc.GoName)
newModelsStr += nmline

View File

@@ -1,6 +1,7 @@
package main
import (
"os"
"testing"
)
@@ -8,6 +9,6 @@ func TestMain(t *testing.T) {
// args := []string{"-name", "fs_guest"}
testGenDir = "../" + testGenDir
// os.Args = []string{"cmd", "-name=fs_guest"}
os.Args = []string{"cmd", "-name=-"}
main()
}