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