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

34 lines
911 B
Go
Raw Normal View History

2020-07-29 17:11:41 +08:00
package gen
import (
"strings"
2020-08-12 09:19:37 +08:00
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
"github.com/tal-tech/go-zero/tools/goctl/util/templatex"
2020-07-29 17:11:41 +08:00
)
2020-08-15 18:38:55 +08:00
func genVars(table Table, withCache bool) (string, error) {
2020-07-29 17:11:41 +08:00
keys := make([]string, 0)
2020-08-12 09:19:37 +08:00
for _, v := range table.CacheKey {
keys = append(keys, v.VarExpression)
2020-07-29 17:11:41 +08:00
}
2020-08-12 09:19:37 +08:00
camel := table.Name.Snake2Camel()
output, err := templatex.With("var").
Parse(template.Vars).
GoFmt(true).
Execute(map[string]interface{}{
"lowerStartCamelObject": stringx.From(camel).LowerStart(),
"upperStartCamelObject": camel,
"cacheKeys": strings.Join(keys, "\r\n"),
"autoIncrement": table.PrimaryKey.AutoIncrement,
"originalPrimaryKey": table.PrimaryKey.Name.Source(),
2020-08-15 18:38:55 +08:00
"withCache": withCache,
2020-08-12 09:19:37 +08:00
})
2020-07-29 17:11:41 +08:00
if err != nil {
return "", err
}
2020-08-12 09:19:37 +08:00
return output.String(), nil
2020-07-29 17:11:41 +08:00
}