Skip to content

Commit 74cc593

Browse files
committed
add data/mapper plugins
1 parent 67dced0 commit 74cc593

File tree

9 files changed

+290
-19
lines changed

9 files changed

+290
-19
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*.dll
55
*.so
66
*.dylib
7+
*.zip
78

89
# Test binary, built with `go test -c`
910
*.test
@@ -13,3 +14,5 @@
1314

1415
# Dependency directories (remove the comment below to include it)
1516
# vendor/
17+
18+
/.idea/

example/example.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,28 @@ import "time"
99

1010
// +gobatis:data
1111
type UserDo struct {
12-
//
13-
UserId int64 `tableId:"user_id,idType=auto"`
14-
UserName string `tableField:"user_name"`
15-
Status int8 `tableField:"status" tableLogic:"0,delval=1"`
16-
CreateTime time.Time `tableField:"create_time,fill=insert"`
17-
RecVersion uint64 `tableRecVer:"rec_var"`
12+
// +gobatis:tableid:value=user_id,idType=auto
13+
UserId int64
14+
15+
// +gobatis:tablefield:value=user_name
16+
UserName string
17+
18+
// +gobatis:tablefield:value=status
19+
// +gobatis:tablelogic:value=0,delval=1
20+
Status int8
21+
22+
// +gobatis:tablefield:value=create_time,fill=insert
23+
CreateTime time.Time
24+
25+
// +gobatis:tablefield:value=rec_var
26+
// +gobatis:version
27+
RecVersion uint64
1828
}
1929

2030
// +gobatis:mapper
2131
type UserMapper interface {
22-
// +gobatis:select="select * from "
32+
// +gobatis:select="select * from tbl_user where id = #{UserDo.UserId}"
33+
Select(vo UserDo) ([]UserDo, error)
2334

2435
Insert(vo ...UserDo) (err error)
2536
}

pkg/generator/gobatis-gen.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package generator
77

88
import (
99
"fmt"
10+
"github.com/xfali/gobatis-plus/pkg/plugin"
1011
"io"
1112
"k8s.io/gengo/args"
1213
"k8s.io/gengo/examples/set-gen/sets"
@@ -18,11 +19,6 @@ import (
1819
"strings"
1920
)
2021

21-
const (
22-
delimiterLeft = "{{"
23-
delimiterRight = "}}"
24-
)
25-
2622
var (
2723
gobatisImports = []string{"github.com/xfali/gobatis"}
2824
)
@@ -78,9 +74,9 @@ func GenPackages(ctx *generator.Context, args *args.GeneratorArgs) generator.Pac
7874
header = append(header, boilerplate...)
7975
}
8076

81-
for i, dir := range inputs {
82-
klog.V(5).Infof("Parsing pkg %s\n", dir)
83-
pkg := ctx.Universe[i]
77+
for in := range inputs {
78+
klog.V(5).Infof("Parsing pkg %s\n", in)
79+
pkg := ctx.Universe[in]
8480
if pkg == nil {
8581
continue
8682
}
@@ -92,7 +88,7 @@ func GenPackages(ctx *generator.Context, args *args.GeneratorArgs) generator.Pac
9288
continue
9389
}
9490

95-
klog.V(5).Infof("Generating package %s...\n", dir)
91+
klog.V(5).Infof("Generating package %s...\n", in)
9692

9793
pkgs = append(pkgs, &generator.DefaultPackage{
9894
PackageName: strings.Split(filepath.Base(pkg.Path), ".")[0],
@@ -180,9 +176,11 @@ func (g *gobatisGen) PackageConsts(ctx *generator.Context) []string {
180176

181177
// GenerateType should emit the code for a particular type.
182178
func (g *gobatisGen) GenerateType(ctx *generator.Context, t *types.Type, w io.Writer) error {
183-
sw := generator.NewSnippetWriter(w, ctx, delimiterLeft, delimiterRight)
184-
sw = sw
185-
return nil
179+
p := plugin.FindPlugin(t)
180+
if p == nil {
181+
return fmt.Errorf("Cannot handle type: %s. ", t.String())
182+
}
183+
return p.Generate(ctx, w, t)
186184
}
187185

188186
func parseAnnotations(annotation string, t *types.Type) gobatisAnnotions {

pkg/plugin/constants.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright (C) 2019-2022, Xiongfa Li.
2+
// @author xiongfa.li
3+
// @version V1.0
4+
// Description:
5+
6+
package plugin
7+
8+
const (
9+
delimiterLeft = "{{"
10+
delimiterRight = "}}"
11+
)

pkg/plugin/data.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright (C) 2019-2022, Xiongfa Li.
2+
// @author xiongfa.li
3+
// @version V1.0
4+
// Description:
5+
6+
package plugin
7+
8+
import (
9+
"io"
10+
"k8s.io/gengo/generator"
11+
"k8s.io/gengo/types"
12+
"strings"
13+
)
14+
15+
type dataPlugin struct {
16+
}
17+
18+
func (p *dataPlugin) Annotation() string {
19+
return "+gobatis:data"
20+
}
21+
22+
func (p *dataPlugin) CouldHandle(t *types.Type) bool {
23+
if t.Kind == types.Struct {
24+
for _, c := range t.CommentLines {
25+
i := strings.Index(c, p.Annotation())
26+
if i != -1 {
27+
return true
28+
}
29+
}
30+
}
31+
return false
32+
}
33+
34+
func (p *dataPlugin) Generate(ctx *generator.Context, w io.Writer, t *types.Type) (err error) {
35+
//sw := generator.NewSnippetWriter(w, ctx, delimiterLeft, delimiterRight)
36+
return nil
37+
}

pkg/plugin/mapper.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,32 @@
11
package plugin
2+
3+
import (
4+
"io"
5+
"k8s.io/gengo/generator"
6+
"k8s.io/gengo/types"
7+
"strings"
8+
)
9+
10+
type mapperPlugin struct {
11+
}
12+
13+
func (p *mapperPlugin) Annotation() string {
14+
return "+gobatis:mapper"
15+
}
16+
17+
func (p *mapperPlugin) CouldHandle(t *types.Type) bool {
18+
if t.Kind == types.Interface {
19+
for _, c := range t.CommentLines {
20+
i := strings.Index(c, p.Annotation())
21+
if i != -1 {
22+
return true
23+
}
24+
}
25+
}
26+
return false
27+
}
28+
29+
func (p *mapperPlugin) Generate(ctx *generator.Context, w io.Writer, t *types.Type) (err error) {
30+
//sw := generator.NewSnippetWriter(w, ctx, delimiterLeft, delimiterRight)
31+
return nil
32+
}

pkg/plugin/plugin.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright (C) 2019-2022, Xiongfa Li.
2+
// @author xiongfa.li
3+
// @version V1.0
4+
// Description:
5+
6+
package plugin
7+
8+
import (
9+
"io"
10+
"k8s.io/gengo/generator"
11+
"k8s.io/gengo/types"
12+
)
13+
14+
type Plugin interface {
15+
Annotation() string
16+
CouldHandle(t *types.Type) bool
17+
Generate(ctx *generator.Context, w io.Writer, t *types.Type) (err error)
18+
}

pkg/plugin/register.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright (C) 2019-2022, Xiongfa Li.
2+
// @author xiongfa.li
3+
// @version V1.0
4+
// Description:
5+
6+
package plugin
7+
8+
import "k8s.io/gengo/types"
9+
10+
var (
11+
gPlugins = []Plugin{
12+
&mapperPlugin{},
13+
&dataPlugin{},
14+
}
15+
)
16+
17+
func FindPlugin(t *types.Type) Plugin {
18+
for i := len(gPlugins) - 1; i >= 0; i-- {
19+
v := gPlugins[i]
20+
if v.CouldHandle(t) {
21+
return v
22+
}
23+
}
24+
return nil
25+
}
26+
27+
func RegisterPlugin(plugin Plugin) {
28+
gPlugins = append(gPlugins, plugin)
29+
}

template/service_gobatis_imapper.tmpl

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
{{- $Module := currentModule -}}
2+
{{- $TableInfo := currentModuleTableInfo -}}
3+
{{- $ModuleName := firstUpper $Module.Name -}}
4+
{{- $SessionKeyName := firstLower $TableInfo.DbName | printf "_%s_gobatis_sess_key" -}}
5+
6+
package {{$Module.Pkg}}
7+
8+
import (
9+
"context"
10+
"github.com/xfali/gobatis"
11+
"github.com/xfali/pagehelper"
12+
"github.com/xfali/xlog"
13+
"{{.Value.App.ModName}}/pkg/apis/{{$Module.Pkg}}"
14+
)
15+
16+
type {{$ModuleName}}ServicImpl struct {
17+
log xlog.Logger
18+
SessMgr *gobatis.SessionManager `inject:"{{$TableInfo.DbName}}"`
19+
}
20+
21+
func New{{$ModuleName}}Service() *{{$ModuleName}}ServicImpl {
22+
return &{{$ModuleName}}ServicImpl{
23+
log: xlog.GetLogger(),
24+
}
25+
}
26+
27+
func (s *{{$ModuleName}}ServicImpl) Query{{$ModuleName}}(ctx context.Context, request {{$Module.Pkg}}.{{$ModuleName}}) (*{{$Module.Pkg}}.{{$ModuleName}}, error) {
28+
sess := s.getSession(ctx)
29+
if sess == nil {
30+
sess = s.SessMgr.NewSession().SetContext(ctx)
31+
}
32+
v, err := Select{{$ModuleName}}(sess, request)
33+
if err != nil {
34+
return nil, err
35+
}
36+
if len(v) == 0 {
37+
return nil, nil
38+
}
39+
return &v[0], nil
40+
}
41+
42+
func (s *{{$ModuleName}}ServicImpl) Query{{$ModuleName}}List(ctx context.Context, request {{$Module.Pkg}}.{{$ModuleName}}, page int64, pageSize int64) ([]{{$Module.Pkg}}.{{$ModuleName}}, int64, error) {
43+
sess := s.getSession(ctx)
44+
if sess == nil {
45+
sess = s.SessMgr.NewSession()
46+
}
47+
{{- if .Config.Gobatis.Paging.Total}}
48+
sqlCtx := pagehelper.C(ctx).PageWithCount(page, pageSize, "").Build()
49+
{{- else}}
50+
sqlCtx := pagehelper.C(ctx).Page(page, pageSize).Build()
51+
{{- end}}
52+
v, err := Select{{$ModuleName}}(sess.SetContext(sqlCtx), request)
53+
if err != nil {
54+
return nil, 0, err
55+
}
56+
pageInfo := pagehelper.GetPageInfo(sqlCtx)
57+
if pageInfo == nil {
58+
return v, 0, nil
59+
} else {
60+
return v, pageInfo.GetTotal(), nil
61+
}
62+
}
63+
64+
func (s *{{$ModuleName}}ServicImpl) Insert{{$ModuleName}}(ctx context.Context, request {{$Module.Pkg}}.{{$ModuleName}}) (int64, error) {
65+
sess := s.getSession(ctx)
66+
if sess == nil {
67+
sess = s.SessMgr.NewSession().SetContext(ctx)
68+
}
69+
_, id, err := Insert{{$ModuleName}}(sess, request)
70+
if err != nil {
71+
return -1, err
72+
}
73+
return id, nil
74+
}
75+
76+
func (s *{{$ModuleName}}ServicImpl) Insert{{$ModuleName}}Batch(ctx context.Context, requests ...{{$Module.Pkg}}.{{$ModuleName}}) error {
77+
sess := s.getSession(ctx)
78+
if sess == nil {
79+
sess = s.SessMgr.NewSession().SetContext(ctx)
80+
}
81+
_, _, err := InsertBatch{{$ModuleName}}(sess, requests)
82+
return err
83+
}
84+
85+
func (s *{{$ModuleName}}ServicImpl) Update{{$ModuleName}}(ctx context.Context, request {{$Module.Pkg}}.{{$ModuleName}}) (bool, error) {
86+
sess := s.getSession(ctx)
87+
if sess == nil {
88+
sess = s.SessMgr.NewSession().SetContext(ctx)
89+
}
90+
c, err := Update{{$ModuleName}}(sess, request)
91+
if err != nil {
92+
return false, err
93+
}
94+
return c > 0, nil
95+
}
96+
97+
func (s *{{$ModuleName}}ServicImpl) Delete{{$ModuleName}}(ctx context.Context, request {{$Module.Pkg}}.{{$ModuleName}}) (bool, error) {
98+
sess := s.getSession(ctx)
99+
if sess == nil {
100+
sess = s.SessMgr.NewSession().SetContext(ctx)
101+
}
102+
c, err := Delete{{$ModuleName}}(sess, request)
103+
if err != nil {
104+
return false, err
105+
}
106+
return c > 0, nil
107+
}
108+
109+
// Transaction
110+
func (s *{{$ModuleName}}ServicImpl) Tx(ctx context.Context, action func(ctx context.Context) error) error {
111+
sess := s.getSession(ctx)
112+
if sess == nil {
113+
return s.SessMgr.NewSession().SetContext(ctx).Tx(func(sess *gobatis.Session) error {
114+
return action(s.withSession(ctx, sess))
115+
})
116+
} else {
117+
return action(ctx)
118+
}
119+
}
120+
121+
func (s *{{$ModuleName}}ServicImpl) getSession(ctx context.Context) *gobatis.Session {
122+
v := ctx.Value({{$SessionKeyName}})
123+
if v == nil {
124+
return nil
125+
}
126+
return v.(*gobatis.Session)
127+
}
128+
129+
func (s *{{$ModuleName}}ServicImpl) withSession(ctx context.Context, sess *gobatis.Session) context.Context {
130+
return context.WithValue(ctx, {{$SessionKeyName}}, sess)
131+
}
132+
133+
var {{$SessionKeyName}} = "{{$SessionKeyName}}"

0 commit comments

Comments
 (0)