go-zero/tools/goctl/model/sql/gen/vars.go

39 lines
1.0 KiB
Go
Raw Normal View History

2020-07-29 17:11:41 +08:00
package gen
import (
"strings"
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
2020-07-29 17:11:41 +08:00
)
func genVars(table Table, withCache bool) (string, error) {
2020-07-29 17:11:41 +08:00
keys := make([]string, 0)
2021-03-01 17:29:07 +08:00
keys = append(keys, table.PrimaryCacheKey.VarExpression)
for _, v := range table.UniqueCacheKey {
keys = append(keys, v.VarExpression)
2020-07-29 17:11:41 +08:00
}
2021-03-01 17:29:07 +08:00
camel := table.Name.ToCamel()
text, err := util.LoadTemplate(category, varTemplateFile, template.Vars)
if err != nil {
return "", err
}
2021-03-01 17:29:07 +08:00
output, err := util.With("var").Parse(text).
GoFmt(true).Execute(map[string]interface{}{
"lowerStartCamelObject": stringx.From(camel).Untitle(),
"upperStartCamelObject": camel,
"cacheKeys": strings.Join(keys, "\n"),
"autoIncrement": table.PrimaryKey.AutoIncrement,
"originalPrimaryKey": wrapWithRawString(table.PrimaryKey.Name.Source()),
"withCache": withCache,
})
2020-07-29 17:11:41 +08:00
if err != nil {
return "", err
}
return output.String(), nil
2020-07-29 17:11:41 +08:00
}