使用gorm 实现 php现在的逻辑

This commit is contained in:
2024-04-10 14:58:19 +08:00
parent 13782b799e
commit ad3c959586
71 changed files with 1396 additions and 1108 deletions

View File

@@ -11,6 +11,7 @@ import (
"os"
"path/filepath"
"regexp"
"runtime"
"strings"
"testing"
"text/template"
@@ -66,6 +67,7 @@ func genFile(tpl *template.Template, executeTemplate string, genFilePath string,
}
type actionsFunc struct {
ModuleName string
ActionName string
FuncName string
ParamStruct *paramStruct
@@ -84,8 +86,10 @@ type paramStruct struct {
}
func getActionsInfo() map[string]*actionsFunc {
regActionRe := regexp.MustCompile(`(?i)action +([^ ]+)`)
paramStructRe := regexp.MustCompile(` +([a-zA-Z_\-/]+)(\?{0,1}): +(\w+) ?;`)
moduleName := GetModuleName(6)
dir := "actions"
actionsMap := make(map[string]*actionsFunc)
@@ -118,6 +122,7 @@ func getActionsInfo() map[string]*actionsFunc {
matches := regActionRe.FindStringSubmatch(comment.Text)
if len(matches) != 0 {
af = &actionsFunc{
ModuleName: moduleName,
ActionName: matches[1],
FuncName: fn.Name.Name,
}
@@ -191,3 +196,42 @@ func toCamelCase(s string) string {
}
return sb.String()
}
func GetModuleName(maxDepth int) string {
gomodPath, err := FindGoModFile(maxDepth, "go.mod")
if err != nil {
panic(err)
}
// 解析当前目录
data, err := os.ReadFile(gomodPath)
if err != nil {
panic(err)
}
matches := regexp.MustCompile(`module +([^ \n]+)`).FindStringSubmatch(string(data))
if len(matches) == 0 {
panic("can't find module name")
}
return matches[1]
}
func FindGoModFile(maxDepth int, fileName string) (string, error) {
_, currentFile, _, ok := runtime.Caller(0)
if !ok {
return "", fmt.Errorf("failed to get current file path")
}
currentDir := filepath.Dir(currentFile)
for i := 0; i < maxDepth; i++ {
modFilePath := filepath.Join(currentDir, fileName)
_, err := os.Stat(modFilePath)
if err == nil {
return modFilePath, nil
} else if !os.IsNotExist(err) {
return "", err
}
currentDir = filepath.Dir(currentDir)
}
return "", fmt.Errorf("go.mod file not found within %d parent directories", maxDepth)
}

View File

@@ -2,8 +2,8 @@ package actions
import (
"github.com/gin-gonic/gin"
"github.com/iapologizewhenimwrong/Vestmore_GO/utils/basic"
"github.com/iapologizewhenimwrong/Vestmore_GO/utils/log"
"{{.ModuleName}}/utils/basic"
"{{.ModuleName}}/utils/log"
)
var HandlersFuncRoutes map[string]gin.HandlerFunc = make(map[string]gin.HandlerFunc)

View File

@@ -26,7 +26,8 @@ func AppV1_0(ctx *gin.Context) {
func main() {
log.SetFlags(log.Llongfile)
model.Models.SetSqlxDriver("mysql", "php:aFk3i4Dj#76!4sd@tcp(47.243.100.6:3306)/zunxinfinance?charset=utf8mb4&timeout=10s")
model.ModelInit("php:aFk3i4Dj#76!4sd@tcp(47.243.100.6:3306)/zunxinfinance?charset=utf8mb4&timeout=10s")
r := gin.Default()
cors.SetCors(r)

View File

@@ -1,6 +1,8 @@
package main
import (
"log"
"reflect"
"testing"
_ "github.com/go-sql-driver/mysql"
@@ -11,3 +13,12 @@ func TestMain(t *testing.T) {
// model.Models.KillaraCustomerModel.Find(context.TODO())
main()
}
func TestCaseZero(t *testing.T) {
var a string = ""
var b string
var c int = 1
a = ""
log.Println(reflect.ValueOf(a).IsZero(), reflect.ValueOf(b).IsZero(), reflect.ValueOf(c).IsZero())
}