Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8dd93d59a0 | ||
|
|
3a4e1cbb33 | ||
|
|
d1129e3974 | ||
|
|
1e85f74fd8 |
@@ -10,14 +10,16 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type ExpandLogic struct {
|
type ExpandLogic struct {
|
||||||
ctx context.Context
|
|
||||||
logx.Logger
|
logx.Logger
|
||||||
|
ctx context.Context
|
||||||
|
svcCtx *svc.ServiceContext
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewExpandLogic(ctx context.Context, svcCtx *svc.ServiceContext) ExpandLogic {
|
func NewExpandLogic(ctx context.Context, svcCtx *svc.ServiceContext) ExpandLogic {
|
||||||
return ExpandLogic{
|
return ExpandLogic{
|
||||||
ctx: ctx,
|
|
||||||
Logger: logx.WithContext(ctx),
|
Logger: logx.WithContext(ctx),
|
||||||
|
ctx: ctx,
|
||||||
|
svcCtx: svcCtx,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,14 +10,16 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type ShortenLogic struct {
|
type ShortenLogic struct {
|
||||||
ctx context.Context
|
|
||||||
logx.Logger
|
logx.Logger
|
||||||
|
ctx context.Context
|
||||||
|
svcCtx *svc.ServiceContext
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewShortenLogic(ctx context.Context, svcCtx *svc.ServiceContext) ShortenLogic {
|
func NewShortenLogic(ctx context.Context, svcCtx *svc.ServiceContext) ShortenLogic {
|
||||||
return ShortenLogic{
|
return ShortenLogic{
|
||||||
ctx: ctx,
|
|
||||||
Logger: logx.WithContext(ctx),
|
Logger: logx.WithContext(ctx),
|
||||||
|
ctx: ctx,
|
||||||
|
svcCtx: svcCtx,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,16 +10,16 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type ExpandLogic struct {
|
type ExpandLogic struct {
|
||||||
|
logx.Logger
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
svcCtx *svc.ServiceContext
|
svcCtx *svc.ServiceContext
|
||||||
logx.Logger
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewExpandLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ExpandLogic {
|
func NewExpandLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ExpandLogic {
|
||||||
return &ExpandLogic{
|
return &ExpandLogic{
|
||||||
|
Logger: logx.WithContext(ctx),
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
svcCtx: svcCtx,
|
svcCtx: svcCtx,
|
||||||
Logger: logx.WithContext(ctx),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,16 +12,16 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type ShortenLogic struct {
|
type ShortenLogic struct {
|
||||||
|
logx.Logger
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
svcCtx *svc.ServiceContext
|
svcCtx *svc.ServiceContext
|
||||||
logx.Logger
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewShortenLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ShortenLogic {
|
func NewShortenLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ShortenLogic {
|
||||||
return &ShortenLogic{
|
return &ShortenLogic{
|
||||||
|
Logger: logx.WithContext(ctx),
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
svcCtx: svcCtx,
|
svcCtx: svcCtx,
|
||||||
Logger: logx.WithContext(ctx),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -20,16 +20,16 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type {{.logic}} struct {
|
type {{.logic}} struct {
|
||||||
|
logx.Logger
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
svcCtx *svc.ServiceContext
|
svcCtx *svc.ServiceContext
|
||||||
logx.Logger
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func New{{.logic}}(ctx context.Context, svcCtx *svc.ServiceContext) {{.logic}} {
|
func New{{.logic}}(ctx context.Context, svcCtx *svc.ServiceContext) {{.logic}} {
|
||||||
return {{.logic}}{
|
return {{.logic}}{
|
||||||
|
Logger: logx.WithContext(ctx),
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
svcCtx: svcCtx,
|
svcCtx: svcCtx,
|
||||||
Logger: logx.WithContext(ctx),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -57,6 +57,7 @@ func genLogicByRoute(dir string, group spec.Group, route spec.Route) error {
|
|||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("missing handler annotation for %q", route.Path)
|
return fmt.Errorf("missing handler annotation for %q", route.Path)
|
||||||
}
|
}
|
||||||
|
|
||||||
handler = strings.TrimSuffix(handler, "handler")
|
handler = strings.TrimSuffix(handler, "handler")
|
||||||
handler = strings.TrimSuffix(handler, "Handler")
|
handler = strings.TrimSuffix(handler, "Handler")
|
||||||
filename := strings.ToLower(handler)
|
filename := strings.ToLower(handler)
|
||||||
@@ -65,6 +66,7 @@ func genLogicByRoute(dir string, group spec.Group, route spec.Route) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if !created {
|
if !created {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -74,11 +76,11 @@ func genLogicByRoute(dir string, group spec.Group, route spec.Route) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
imports := genLogicImports(route, parentPkg)
|
|
||||||
|
|
||||||
responseString := ""
|
imports := genLogicImports(route, parentPkg)
|
||||||
returnString := ""
|
var responseString string
|
||||||
requestString := ""
|
var returnString string
|
||||||
|
var requestString string
|
||||||
if len(route.ResponseType.Name) > 0 {
|
if len(route.ResponseType.Name) > 0 {
|
||||||
resp := strings.Title(route.ResponseType.Name)
|
resp := strings.Title(route.ResponseType.Name)
|
||||||
responseString = "(*types." + resp + ", error)"
|
responseString = "(*types." + resp + ", error)"
|
||||||
@@ -104,6 +106,7 @@ func genLogicByRoute(dir string, group spec.Group, route spec.Route) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
formatCode := formatCode(buffer.String())
|
formatCode := formatCode(buffer.String())
|
||||||
_, err = fp.WriteString(formatCode)
|
_, err = fp.WriteString(formatCode)
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import (
|
|||||||
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
||||||
)
|
)
|
||||||
|
|
||||||
// struct匹配
|
// struct match
|
||||||
const typeRegex = `(?m)(?m)(^ *type\s+[a-zA-Z][a-zA-Z0-9_-]+\s+(((struct)\s*?\{[\w\W]*?[^\{]\})|([a-zA-Z][a-zA-Z0-9_-]+)))|(^ *type\s*?\([\w\W]+\}\s*\))`
|
const typeRegex = `(?m)(?m)(^ *type\s+[a-zA-Z][a-zA-Z0-9_-]+\s+(((struct)\s*?\{[\w\W]*?[^\{]\})|([a-zA-Z][a-zA-Z0-9_-]+)))|(^ *type\s*?\([\w\W]+\}\s*\))`
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ func genDelete(table Table, withCache bool) (string, error) {
|
|||||||
}
|
}
|
||||||
var containsIndexCache = false
|
var containsIndexCache = false
|
||||||
for _, item := range table.Fields {
|
for _, item := range table.Fields {
|
||||||
if item.IsKey {
|
if item.IsKey && !item.IsPrimaryKey {
|
||||||
containsIndexCache = true
|
containsIndexCache = true
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -126,7 +126,12 @@ func (g *defaultGenerator) genModel(in parser.Table, withCache bool) (string, er
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
importsCode := genImports(withCache)
|
|
||||||
|
importsCode, err := genImports(withCache, in.ContainsTime())
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
var table Table
|
var table Table
|
||||||
table.Table = in
|
table.Table = in
|
||||||
table.CacheKey = m
|
table.CacheKey = m
|
||||||
@@ -135,36 +140,44 @@ func (g *defaultGenerator) genModel(in parser.Table, withCache bool) (string, er
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
typesCode, err := genTypes(table, withCache)
|
typesCode, err := genTypes(table, withCache)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
newCode, err := genNew(table, withCache)
|
newCode, err := genNew(table, withCache)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
insertCode, err := genInsert(table, withCache)
|
insertCode, err := genInsert(table, withCache)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
var findCode = make([]string, 0)
|
var findCode = make([]string, 0)
|
||||||
findOneCode, err := genFindOne(table, withCache)
|
findOneCode, err := genFindOne(table, withCache)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
findOneByFieldCode, err := genFineOneByField(table, withCache)
|
findOneByFieldCode, err := genFineOneByField(table, withCache)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
findCode = append(findCode, findOneCode, findOneByFieldCode)
|
findCode = append(findCode, findOneCode, findOneByFieldCode)
|
||||||
updateCode, err := genUpdate(table, withCache)
|
updateCode, err := genUpdate(table, withCache)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
deleteCode, err := genDelete(table, withCache)
|
deleteCode, err := genDelete(table, withCache)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
output, err := t.Execute(map[string]interface{}{
|
output, err := t.Execute(map[string]interface{}{
|
||||||
"imports": importsCode,
|
"imports": importsCode,
|
||||||
"vars": varsCode,
|
"vars": varsCode,
|
||||||
@@ -178,5 +191,6 @@ func (g *defaultGenerator) genModel(in parser.Table, withCache bool) (string, er
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
return output.String(), nil
|
return output.String(), nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,12 +2,25 @@ package gen
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
|
"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 {
|
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 {
|
} 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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,16 +16,20 @@ const (
|
|||||||
spatial
|
spatial
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const timeImport = "time.Time"
|
||||||
|
|
||||||
type (
|
type (
|
||||||
Table struct {
|
Table struct {
|
||||||
Name stringx.String
|
Name stringx.String
|
||||||
PrimaryKey Primary
|
PrimaryKey Primary
|
||||||
Fields []Field
|
Fields []Field
|
||||||
}
|
}
|
||||||
|
|
||||||
Primary struct {
|
Primary struct {
|
||||||
Field
|
Field
|
||||||
AutoIncrement bool
|
AutoIncrement bool
|
||||||
}
|
}
|
||||||
|
|
||||||
Field struct {
|
Field struct {
|
||||||
Name stringx.String
|
Name stringx.String
|
||||||
DataBaseType string
|
DataBaseType string
|
||||||
@@ -34,6 +38,7 @@ type (
|
|||||||
IsPrimaryKey bool
|
IsPrimaryKey bool
|
||||||
Comment string
|
Comment string
|
||||||
}
|
}
|
||||||
|
|
||||||
KeyType int
|
KeyType int
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -135,3 +140,12 @@ func Parse(ddl string) (*Table, error) {
|
|||||||
Fields: fields,
|
Fields: fields,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *Table) ContainsTime() bool {
|
||||||
|
for _, item := range t.Fields {
|
||||||
|
if item.DataType == timeImport {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ package template
|
|||||||
|
|
||||||
var Delete = `
|
var Delete = `
|
||||||
func (m *{{.upperStartCamelObject}}Model) Delete({{.lowerStartCamelPrimaryKey}} {{.dataType}}) error {
|
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{
|
if err!=nil{
|
||||||
return err
|
return err
|
||||||
}{{end}}
|
}{{end}}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ var (
|
|||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
{{if .time}}"time"{{end}}
|
||||||
|
|
||||||
"github.com/tal-tech/go-zero/core/stores/cache"
|
"github.com/tal-tech/go-zero/core/stores/cache"
|
||||||
"github.com/tal-tech/go-zero/core/stores/sqlc"
|
"github.com/tal-tech/go-zero/core/stores/sqlc"
|
||||||
@@ -16,7 +17,7 @@ var (
|
|||||||
ImportsNoCache = `import (
|
ImportsNoCache = `import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
{{if .time}}"time"{{end}}
|
||||||
|
|
||||||
"github.com/tal-tech/go-zero/core/stores/sqlc"
|
"github.com/tal-tech/go-zero/core/stores/sqlc"
|
||||||
"github.com/tal-tech/go-zero/core/stores/sqlx"
|
"github.com/tal-tech/go-zero/core/stores/sqlx"
|
||||||
|
|||||||
Reference in New Issue
Block a user