فهرست منبع

fix bug: miss time import (#36)

* add execute files

* add protoc-osx

* add rpc generation

* add rpc generation

* add: rpc template generation

* optimize gomod cache

* add README.md

* format error

* reactor templatex.go

* update project.go & README.md

* fix bug: miss time import
Keson 4 سال پیش
والد
کامیت
3a4e1cbb33

+ 1 - 1
tools/goctl/model/sql/gen/delete.go

@@ -22,7 +22,7 @@ func genDelete(table Table, withCache bool) (string, error) {
 	}
 	var containsIndexCache = false
 	for _, item := range table.Fields {
-		if item.IsKey {
+		if item.IsKey && !item.IsPrimaryKey {
 			containsIndexCache = true
 			break
 		}

+ 15 - 1
tools/goctl/model/sql/gen/gen.go

@@ -126,7 +126,12 @@ func (g *defaultGenerator) genModel(in parser.Table, withCache bool) (string, er
 	if err != nil {
 		return "", err
 	}
-	importsCode := genImports(withCache)
+
+	importsCode, err := genImports(withCache, in.ContainsTime())
+	if err != nil {
+		return "", err
+	}
+
 	var table Table
 	table.Table = in
 	table.CacheKey = m
@@ -135,36 +140,44 @@ func (g *defaultGenerator) genModel(in parser.Table, withCache bool) (string, er
 	if err != nil {
 		return "", err
 	}
+
 	typesCode, err := genTypes(table, withCache)
 	if err != nil {
 		return "", err
 	}
+
 	newCode, err := genNew(table, withCache)
 	if err != nil {
 		return "", err
 	}
+
 	insertCode, err := genInsert(table, withCache)
 	if err != nil {
 		return "", err
 	}
+
 	var findCode = make([]string, 0)
 	findOneCode, err := genFindOne(table, withCache)
 	if err != nil {
 		return "", err
 	}
+
 	findOneByFieldCode, err := genFineOneByField(table, withCache)
 	if err != nil {
 		return "", err
 	}
+
 	findCode = append(findCode, findOneCode, findOneByFieldCode)
 	updateCode, err := genUpdate(table, withCache)
 	if err != nil {
 		return "", err
 	}
+
 	deleteCode, err := genDelete(table, withCache)
 	if err != nil {
 		return "", err
 	}
+
 	output, err := t.Execute(map[string]interface{}{
 		"imports": importsCode,
 		"vars":    varsCode,
@@ -178,5 +191,6 @@ func (g *defaultGenerator) genModel(in parser.Table, withCache bool) (string, er
 	if err != nil {
 		return "", err
 	}
+
 	return output.String(), nil
 }

+ 16 - 3
tools/goctl/model/sql/gen/imports.go

@@ -2,12 +2,25 @@ package gen
 
 import (
 	"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
+	"github.com/tal-tech/go-zero/tools/goctl/util"
 )
 
-func genImports(withCache bool) string {
+func genImports(withCache, timeImport bool) (string, error) {
 	if withCache {
-		return template.Imports
+		buffer, err := util.With("import").Parse(template.Imports).Execute(map[string]interface{}{
+			"time": timeImport,
+		})
+		if err != nil {
+			return "", err
+		}
+		return buffer.String(), nil
 	} else {
-		return template.ImportsNoCache
+		buffer, err := util.With("import").Parse(template.ImportsNoCache).Execute(map[string]interface{}{
+			"time": timeImport,
+		})
+		if err != nil {
+			return "", err
+		}
+		return buffer.String(), nil
 	}
 }

+ 13 - 0
tools/goctl/model/sql/parser/parser.go

@@ -16,6 +16,10 @@ const (
 	spatial
 )
 
+const (
+	timeImport = "time.Time"
+)
+
 type (
 	Table struct {
 		Name       stringx.String
@@ -135,3 +139,12 @@ func Parse(ddl string) (*Table, error) {
 		Fields:     fields,
 	}, nil
 }
+
+func (t *Table) ContainsTime() bool {
+	for _, item := range t.Fields {
+		if item.DataType == timeImport {
+			return true
+		}
+	}
+	return false
+}

+ 1 - 1
tools/goctl/model/sql/template/delete.go

@@ -2,7 +2,7 @@ package template
 
 var Delete = `
 func (m *{{.upperStartCamelObject}}Model) Delete({{.lowerStartCamelPrimaryKey}} {{.dataType}}) error {
-	{{if .withCache}}{{if .containsIndexCache}}_, err:=m.FindOne({{.lowerStartCamelPrimaryKey}})
+	{{if .withCache}}{{if .containsIndexCache}}data, err:=m.FindOne({{.lowerStartCamelPrimaryKey}})
 	if err!=nil{
 		return err
 	}{{end}}

+ 2 - 1
tools/goctl/model/sql/template/import.go

@@ -5,6 +5,7 @@ var (
 	"database/sql"
 	"fmt"
 	"strings"
+	{{if .time}}"time"{{end}}
 
 	"github.com/tal-tech/go-zero/core/stores/cache"
 	"github.com/tal-tech/go-zero/core/stores/sqlc"
@@ -16,7 +17,7 @@ var (
 	ImportsNoCache = `import (
 	"database/sql"
 	"strings"
-	"time"
+	{{if .time}}"time"{{end}}
 
 	"github.com/tal-tech/go-zero/core/stores/sqlc"
 	"github.com/tal-tech/go-zero/core/stores/sqlx"