mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-02-03 00:38:40 +08:00
Fix/issue#1447 (#1458)
* Add data for template to render * fix #1447 Co-authored-by: anqiansong <anqiansong@bytedance.com>
This commit is contained in:
parent
bf2feee5b7
commit
e57fa8ff53
@ -38,6 +38,7 @@ func genDelete(table Table, withCache, postgreSql bool) (string, string, error)
|
||||
"originalPrimaryKey": wrapWithRawString(table.PrimaryKey.Name.Source(), postgreSql),
|
||||
"keyValues": strings.Join(keyVariableSet.KeysStr(), ", "),
|
||||
"postgreSql": postgreSql,
|
||||
"data": table,
|
||||
})
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
@ -54,6 +55,7 @@ func genDelete(table Table, withCache, postgreSql bool) (string, string, error)
|
||||
Execute(map[string]interface{}{
|
||||
"lowerStartCamelPrimaryKey": stringx.From(table.PrimaryKey.Name.ToCamel()).Untitle(),
|
||||
"dataType": table.PrimaryKey.DataType,
|
||||
"data": table,
|
||||
})
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
|
@ -9,11 +9,11 @@ import (
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||
)
|
||||
|
||||
func genFields(fields []*parser.Field) (string, error) {
|
||||
func genFields(table Table, fields []*parser.Field) (string, error) {
|
||||
var list []string
|
||||
|
||||
for _, field := range fields {
|
||||
result, err := genField(field)
|
||||
result, err := genField(table, field)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@ -24,8 +24,8 @@ func genFields(fields []*parser.Field) (string, error) {
|
||||
return strings.Join(list, "\n"), nil
|
||||
}
|
||||
|
||||
func genField(field *parser.Field) (string, error) {
|
||||
tag, err := genTag(field.NameOriginal)
|
||||
func genField(table Table, field *parser.Field) (string, error) {
|
||||
tag, err := genTag(table, field.NameOriginal)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@ -43,6 +43,7 @@ func genField(field *parser.Field) (string, error) {
|
||||
"tag": tag,
|
||||
"hasComment": field.Comment != "",
|
||||
"comment": field.Comment,
|
||||
"data": table,
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
@ -26,6 +26,7 @@ func genFindOne(table Table, withCache, postgreSql bool) (string, string, error)
|
||||
"cacheKey": table.PrimaryCacheKey.KeyExpression,
|
||||
"cacheKeyVariable": table.PrimaryCacheKey.KeyLeft,
|
||||
"postgreSql": postgreSql,
|
||||
"data": table,
|
||||
})
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
@ -42,6 +43,7 @@ func genFindOne(table Table, withCache, postgreSql bool) (string, string, error)
|
||||
"upperStartCamelObject": camel,
|
||||
"lowerStartCamelPrimaryKey": stringx.From(table.PrimaryKey.Name.ToCamel()).Untitle(),
|
||||
"dataType": table.PrimaryKey.DataType,
|
||||
"data": table,
|
||||
})
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
|
@ -40,6 +40,7 @@ func genFindOneByField(table Table, withCache, postgreSql bool) (*findOneCode, e
|
||||
"upperStartCamelPrimaryKey": table.PrimaryKey.Name.ToCamel(),
|
||||
"originalField": originalFieldString,
|
||||
"postgreSql": postgreSql,
|
||||
"data": table,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -71,6 +72,7 @@ func genFindOneByField(table Table, withCache, postgreSql bool) (*findOneCode, e
|
||||
"upperStartCamelObject": camelTableName,
|
||||
"upperField": key.FieldNameJoin.Camel().With("").Source(),
|
||||
"in": in,
|
||||
"data": table,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -91,6 +93,7 @@ func genFindOneByField(table Table, withCache, postgreSql bool) (*findOneCode, e
|
||||
"lowerStartCamelObject": stringx.From(camelTableName).Untitle(),
|
||||
"originalPrimaryField": wrapWithRawString(table.PrimaryKey.Name.Source(), postgreSql),
|
||||
"postgreSql": postgreSql,
|
||||
"data": table,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -217,17 +217,17 @@ func (g *defaultGenerator) genModel(in parser.Table, withCache bool) (string, er
|
||||
|
||||
primaryKey, uniqueKey := genCacheKeys(in)
|
||||
|
||||
importsCode, err := genImports(withCache, in.ContainsTime())
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
var table Table
|
||||
table.Table = in
|
||||
table.PrimaryCacheKey = primaryKey
|
||||
table.UniqueCacheKey = uniqueKey
|
||||
table.ContainsUniqueCacheKey = len(uniqueKey) > 0
|
||||
|
||||
importsCode, err := genImports(withCache, in.ContainsTime(), table)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
varsCode, err := genVars(table, withCache, g.isPostgreSql)
|
||||
if err != nil {
|
||||
return "", err
|
||||
@ -284,7 +284,7 @@ func (g *defaultGenerator) genModel(in parser.Table, withCache bool) (string, er
|
||||
cacheExtra: ret.cacheExtra,
|
||||
}
|
||||
|
||||
output, err := g.executeModel(code)
|
||||
output, err := g.executeModel(table, code)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@ -292,7 +292,7 @@ func (g *defaultGenerator) genModel(in parser.Table, withCache bool) (string, er
|
||||
return output.String(), nil
|
||||
}
|
||||
|
||||
func (g *defaultGenerator) executeModel(code *code) (*bytes.Buffer, error) {
|
||||
func (g *defaultGenerator) executeModel(table Table, code *code) (*bytes.Buffer, error) {
|
||||
text, err := pathx.LoadTemplate(category, modelTemplateFile, template.Model)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -311,6 +311,7 @@ func (g *defaultGenerator) executeModel(code *code) (*bytes.Buffer, error) {
|
||||
"update": code.updateCode,
|
||||
"delete": code.deleteCode,
|
||||
"extraMethod": code.cacheExtra,
|
||||
"data": table,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -6,7 +6,7 @@ import (
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||
)
|
||||
|
||||
func genImports(withCache, timeImport bool) (string, error) {
|
||||
func genImports(withCache, timeImport bool, table Table) (string, error) {
|
||||
if withCache {
|
||||
text, err := pathx.LoadTemplate(category, importsTemplateFile, template.Imports)
|
||||
if err != nil {
|
||||
@ -15,6 +15,7 @@ func genImports(withCache, timeImport bool) (string, error) {
|
||||
|
||||
buffer, err := util.With("import").Parse(text).Execute(map[string]interface{}{
|
||||
"time": timeImport,
|
||||
"data": table,
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
@ -30,6 +31,7 @@ func genImports(withCache, timeImport bool) (string, error) {
|
||||
|
||||
buffer, err := util.With("import").Parse(text).Execute(map[string]interface{}{
|
||||
"time": timeImport,
|
||||
"data": table,
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
@ -62,6 +62,7 @@ func genInsert(table Table, withCache, postgreSql bool) (string, string, error)
|
||||
"expressionValues": strings.Join(expressionValues, ", "),
|
||||
"keys": strings.Join(keySet.KeysStr(), "\n"),
|
||||
"keyValues": strings.Join(keyVariableSet.KeysStr(), ", "),
|
||||
"data": table,
|
||||
})
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
@ -75,6 +76,7 @@ func genInsert(table Table, withCache, postgreSql bool) (string, string, error)
|
||||
|
||||
insertMethodOutput, err := util.With("insertMethod").Parse(text).Execute(map[string]interface{}{
|
||||
"upperStartCamelObject": camel,
|
||||
"data": table,
|
||||
})
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
|
@ -25,6 +25,7 @@ func genNew(table Table, withCache, postgreSql bool) (string, error) {
|
||||
"table": t,
|
||||
"withCache": withCache,
|
||||
"upperStartCamelObject": table.Name.ToCamel(),
|
||||
"data": table,
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
@ -6,7 +6,7 @@ import (
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
||||
)
|
||||
|
||||
func genTag(in string) (string, error) {
|
||||
func genTag(table Table, in string) (string, error) {
|
||||
if in == "" {
|
||||
return in, nil
|
||||
}
|
||||
@ -18,6 +18,7 @@ func genTag(in string) (string, error) {
|
||||
|
||||
output, err := util.With("tag").Parse(text).Execute(map[string]interface{}{
|
||||
"field": in,
|
||||
"data": table,
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
@ -8,7 +8,7 @@ import (
|
||||
|
||||
func genTypes(table Table, methods string, withCache bool) (string, error) {
|
||||
fields := table.Fields
|
||||
fieldsString, err := genFields(fields)
|
||||
fieldsString, err := genFields(table, fields)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@ -25,6 +25,7 @@ func genTypes(table Table, methods string, withCache bool) (string, error) {
|
||||
"method": methods,
|
||||
"upperStartCamelObject": table.Name.ToCamel(),
|
||||
"fields": fieldsString,
|
||||
"data": table,
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
@ -58,6 +58,7 @@ func genUpdate(table Table, withCache, postgreSql bool) (string, string, error)
|
||||
"originalPrimaryKey": wrapWithRawString(table.PrimaryKey.Name.Source(), postgreSql),
|
||||
"expressionValues": strings.Join(expressionValues, ", "),
|
||||
"postgreSql": postgreSql,
|
||||
"data": table,
|
||||
})
|
||||
if err != nil {
|
||||
return "", "", nil
|
||||
@ -73,6 +74,7 @@ func genUpdate(table Table, withCache, postgreSql bool) (string, string, error)
|
||||
Parse(text).
|
||||
Execute(map[string]interface{}{
|
||||
"upperStartCamelObject": camelTableName,
|
||||
"data": table,
|
||||
})
|
||||
if err != nil {
|
||||
return "", "", nil
|
||||
|
@ -31,6 +31,7 @@ func genVars(table Table, withCache, postgreSql bool) (string, error) {
|
||||
"originalPrimaryKey": wrapWithRawString(table.PrimaryKey.Name.Source(), postgreSql),
|
||||
"withCache": withCache,
|
||||
"postgreSql": postgreSql,
|
||||
"data": table,
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
Loading…
Reference in New Issue
Block a user