-
Notifications
You must be signed in to change notification settings - Fork 0
/
vars.go
57 lines (51 loc) · 1.5 KB
/
vars.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package gen
import (
"fmt"
"sort"
"strings"
"github.com/zeromicro/go-zero/core/collection"
"github.com/jageros/goctl/model/sql/template"
"github.com/jageros/goctl/util"
"github.com/jageros/goctl/util/pathx"
"github.com/jageros/goctl/util/stringx"
)
func genVars(table Table, withCache, postgreSql bool) (string, error) {
keys := make([]string, 0)
keys = append(keys, table.PrimaryCacheKey.VarExpression)
for _, v := range table.UniqueCacheKey {
keys = append(keys, v.VarExpression)
}
camel := table.Name.ToCamel()
text, err := pathx.LoadTemplate(category, varTemplateFile, template.Vars)
if err != nil {
return "", err
}
output, err := util.With("var").Parse(text).
GoFmt(true).Execute(map[string]any{
"lowerStartCamelObject": stringx.From(camel).Untitle(),
"upperStartCamelObject": camel,
"cacheKeys": strings.Join(keys, "\n"),
"autoIncrement": table.PrimaryKey.AutoIncrement,
"originalPrimaryKey": wrapWithRawString(table.PrimaryKey.Name.Source(), postgreSql),
"withCache": withCache,
"postgreSql": postgreSql,
"data": table,
"ignoreColumns": func() string {
var set = collection.NewSet()
for _, c := range table.ignoreColumns {
if postgreSql {
set.AddStr(fmt.Sprintf(`"%s"`, c))
} else {
set.AddStr(fmt.Sprintf("\"`%s`\"", c))
}
}
list := set.KeysStr()
sort.Strings(list)
return strings.Join(list, ", ")
}(),
})
if err != nil {
return "", err
}
return output.String(), nil
}