-
Notifications
You must be signed in to change notification settings - Fork 0
/
findone.go
53 lines (47 loc) · 1.72 KB
/
findone.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
package gen
import (
"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 genFindOne(table Table, withCache, postgreSql bool) (string, string, error) {
camel := table.Name.ToCamel()
text, err := pathx.LoadTemplate(category, findOneTemplateFile, template.FindOne)
if err != nil {
return "", "", err
}
output, err := util.With("findOne").
Parse(text).
Execute(map[string]any{
"withCache": withCache,
"upperStartCamelObject": camel,
"lowerStartCamelObject": stringx.From(camel).Untitle(),
"originalPrimaryKey": wrapWithRawString(table.PrimaryKey.Name.Source(), postgreSql),
"lowerStartCamelPrimaryKey": util.EscapeGolangKeyword(stringx.From(table.PrimaryKey.Name.ToCamel()).Untitle()),
"dataType": table.PrimaryKey.DataType,
"cacheKey": table.PrimaryCacheKey.KeyExpression,
"cacheKeyVariable": table.PrimaryCacheKey.KeyLeft,
"postgreSql": postgreSql,
"data": table,
})
if err != nil {
return "", "", err
}
text, err = pathx.LoadTemplate(category, findOneMethodTemplateFile, template.FindOneMethod)
if err != nil {
return "", "", err
}
findOneMethod, err := util.With("findOneMethod").
Parse(text).
Execute(map[string]any{
"upperStartCamelObject": camel,
"lowerStartCamelPrimaryKey": util.EscapeGolangKeyword(stringx.From(table.PrimaryKey.Name.ToCamel()).Untitle()),
"dataType": table.PrimaryKey.DataType,
"data": table,
})
if err != nil {
return "", "", err
}
return output.String(), findOneMethod.String(), nil
}