Skip to content

Commit 190d4e3

Browse files
committed
add source codes
1 parent 47e0d65 commit 190d4e3

File tree

10 files changed

+672
-0
lines changed

10 files changed

+672
-0
lines changed

cmd/gobatis-plus/customargs/args.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright (C) 2019-2022, Xiongfa Li.
2+
// @author xiongfa.li
3+
// @version V1.0
4+
// Description:
5+
6+
package customargs
7+
8+
import (
9+
"fmt"
10+
"github.com/spf13/pflag"
11+
"k8s.io/gengo/args"
12+
)
13+
14+
type gobatisArgs struct {
15+
Prefix string
16+
}
17+
18+
func NewDefault() (*args.GeneratorArgs, *gobatisArgs) {
19+
args := args.Default().WithoutDefaultFlagParsing()
20+
cusArgs := &gobatisArgs{
21+
Prefix: "gobatis",
22+
}
23+
args.CustomArgs = cusArgs
24+
args.OutputFileBaseName = "zz_generated"
25+
return args, cusArgs
26+
}
27+
28+
func (arg *gobatisArgs) String() string {
29+
return arg.Prefix
30+
}
31+
32+
func (arg *gobatisArgs) AddFlags(fs *pflag.FlagSet) {
33+
fs.StringVar(&arg.Prefix, "annotation", arg.Prefix, "Annotation name")
34+
}
35+
36+
func Validate(args *args.GeneratorArgs) error {
37+
_ = args.CustomArgs.(*gobatisArgs)
38+
39+
if len(args.OutputFileBaseName) == 0 {
40+
return fmt.Errorf("Output file base name cannot be empty. ")
41+
}
42+
if len(args.InputDirs) == 0 {
43+
return fmt.Errorf("Input directory cannot be empty. ")
44+
}
45+
return nil
46+
}

cmd/gobatis-plus/main.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (C) 2019-2022, Xiongfa Li.
2+
// @author xiongfa.li
3+
// @version V1.0
4+
// Description:
5+
6+
package main
7+
8+
import (
9+
"flag"
10+
"github.com/spf13/pflag"
11+
"github.com/xfali/gobatis-plus/cmd/gobatis-plus/customargs"
12+
"github.com/xfali/gobatis-plus/pkg/generator"
13+
"k8s.io/klog/v2"
14+
)
15+
16+
func main() {
17+
klog.InitFlags(nil)
18+
args, cusArgs := customargs.NewDefault()
19+
20+
args.AddFlags(pflag.CommandLine)
21+
cusArgs.AddFlags(pflag.CommandLine)
22+
23+
_ = flag.Set("logtostderr", "true")
24+
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
25+
pflag.Parse()
26+
27+
if err := customargs.Validate(args); err != nil {
28+
klog.Fatalln(err)
29+
}
30+
31+
if err := args.Execute(generator.NameSystems(), generator.DefaultNameSystem(), generator.GenPackages); err != nil {
32+
klog.Fatalln(err)
33+
}
34+
klog.V(2).Info("Success")
35+
}

example/example.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (C) 2019-2022, Xiongfa Li.
2+
// @author xiongfa.li
3+
// @version V1.0
4+
// Description:
5+
6+
package example
7+
8+
// gobatis:enable
9+
type TestDo struct {
10+
11+
}
12+
13+
// gobatis:mapper
14+
type TestMapper interface {
15+
Insert()
16+
}

go.mod

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module github.com/xfali/gobatis-plus
2+
3+
go 1.16
4+
5+
require (
6+
github.com/spf13/pflag v1.0.5
7+
github.com/xfali/goid v0.0.0-20190301093704-7650c15f1ecc // indirect
8+
k8s.io/gengo v0.0.0-20220613173612-397b4ae3bce7
9+
k8s.io/klog/v2 v2.2.0
10+
)

go.sum

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2+
github.com/go-logr/logr v0.2.0 h1:QvGt2nLcHH0WK9orKa+ppBPAxREcH364nPUedEpK0TY=
3+
github.com/go-logr/logr v0.2.0/go.mod h1:z6/tIYblkpsD+a4lm/fGIIU9mZ+XfAiaFtq7xTgseGU=
4+
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
5+
github.com/google/gofuzz v1.1.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
6+
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
7+
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
8+
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
9+
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
10+
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
11+
github.com/xfali/goid v0.0.0-20190301093704-7650c15f1ecc h1:X/z++Ipzd3N/uFWFFTJJkc3jLR9kH0r+DcMqscPalMI=
12+
github.com/xfali/goid v0.0.0-20190301093704-7650c15f1ecc/go.mod h1:kqSlG3N04yLjSmRSwvnsnf5mrebdKD62QpztaRJpPDc=
13+
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
14+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
15+
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
16+
golang.org/x/mod v0.2.0 h1:KU7oHjnv3XNWfa5COkzUifxZmxp1TyI7ImMXqFxLwvQ=
17+
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
18+
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
19+
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
20+
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
21+
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
22+
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
23+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
24+
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
25+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
26+
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
27+
golang.org/x/tools v0.0.0-20200505023115-26f46d2f7ef8 h1:BMFHd4OFnFtWX46Xj4DN6vvT1btiBxyq+s0orYBqcQY=
28+
golang.org/x/tools v0.0.0-20200505023115-26f46d2f7ef8/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
29+
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
30+
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
31+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
32+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
33+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
34+
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
35+
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
36+
k8s.io/gengo v0.0.0-20220613173612-397b4ae3bce7 h1:RGb68G3yotdQggcyenx9y0+lnVJCXXcLa6geXOMlf5o=
37+
k8s.io/gengo v0.0.0-20220613173612-397b4ae3bce7/go.mod h1:FiNAH4ZV3gBg2Kwh89tzAEV2be7d5xI0vBa/VySYy3E=
38+
k8s.io/klog/v2 v2.2.0 h1:XRvcwJozkgZ1UQJmfMGpvRthQHOvihEhYtDfAaxMz/A=
39+
k8s.io/klog/v2 v2.2.0/go.mod h1:Od+F08eJP+W3HUb4pSrPpgp9DGU4GzlpG/TmITuYh/Y=
40+
sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc=

pkg/generator/gobatis-gen.go

Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
// Copyright (C) 2019-2022, Xiongfa Li.
2+
// @author xiongfa.li
3+
// @version V1.0
4+
// Description:
5+
6+
package generator
7+
8+
import (
9+
"fmt"
10+
"io"
11+
"k8s.io/gengo/args"
12+
"k8s.io/gengo/examples/set-gen/sets"
13+
"k8s.io/gengo/generator"
14+
"k8s.io/gengo/namer"
15+
"k8s.io/gengo/types"
16+
"k8s.io/klog/v2"
17+
"path/filepath"
18+
"strings"
19+
)
20+
21+
const (
22+
delimiterLeft = "{{"
23+
delimiterRight = "}}"
24+
)
25+
26+
var (
27+
gobatisImports = []string{"github.com/xfali/gobatis"}
28+
)
29+
30+
type gobatisAnnotion struct {
31+
rawTypeName string
32+
body string
33+
}
34+
35+
type gobatisAnnotions map[string]*gobatisAnnotion
36+
37+
type gobatisGen struct {
38+
name string
39+
prefix string
40+
targetPkg string
41+
pkg *types.Package
42+
imports namer.ImportTracker
43+
}
44+
45+
func NameSystems() namer.NameSystems {
46+
return namer.NameSystems{
47+
"public": namer.NewPrivateNamer(0, ""),
48+
"raw": namer.NewRawNamer("", nil),
49+
}
50+
}
51+
52+
func DefaultNameSystem() string {
53+
return "public"
54+
}
55+
56+
func checkEnable(annotation string, comments []string) bool {
57+
key := annotation + "enable"
58+
for _, c := range comments {
59+
if strings.HasPrefix(c, key) {
60+
return true
61+
}
62+
}
63+
return false
64+
}
65+
66+
func GenPackages(ctx *generator.Context, args *args.GeneratorArgs) generator.Packages {
67+
inputs := sets.NewString(ctx.Inputs...)
68+
pkgs := generator.Packages{}
69+
annotation := args.CustomArgs.(fmt.Stringer).String()
70+
71+
boilerplate, err := args.LoadGoBoilerplate()
72+
if err != nil {
73+
klog.Warningf("LoadGoBoilerplate failed: %v. ", err)
74+
boilerplate = nil
75+
}
76+
header := []byte(fmt.Sprintf("// +build !%s\n\n", args.GeneratedBuildTag))
77+
if boilerplate != nil {
78+
header = append(header, boilerplate...)
79+
}
80+
81+
for i, dir := range inputs {
82+
klog.V(5).Infof("Parsing pkg %s\n", dir)
83+
pkg := ctx.Universe[i]
84+
if pkg == nil {
85+
continue
86+
}
87+
for _, i := range pkg.Imports {
88+
ctx.AddDirectory(i.Path)
89+
}
90+
91+
if !checkEnable(annotation, pkg.Comments) {
92+
continue
93+
}
94+
95+
klog.V(5).Infof("Generating package %s...\n", dir)
96+
97+
pkgs = append(pkgs, &generator.DefaultPackage{
98+
PackageName: strings.Split(filepath.Base(pkg.Path), ".")[0],
99+
PackagePath: pkg.Path,
100+
HeaderText: header,
101+
GeneratorFunc: func(context *generator.Context) []generator.Generator {
102+
return []generator.Generator{
103+
NewGobatisGenerator(args.OutputFileBaseName, annotation, pkg),
104+
}
105+
},
106+
FilterFunc: func(context *generator.Context, i *types.Type) bool {
107+
return i.Name.Package == pkg.Path
108+
},
109+
})
110+
}
111+
return pkgs
112+
}
113+
114+
func NewGobatisGenerator(name, prefix string, pkg *types.Package) *gobatisGen {
115+
ret := &gobatisGen{
116+
name: name,
117+
prefix: prefix,
118+
pkg: pkg,
119+
imports: generator.NewImportTracker(),
120+
}
121+
122+
return ret
123+
}
124+
125+
// The name of this generator. Will be included in generated comments.
126+
func (g *gobatisGen) Name() string {
127+
return g.name
128+
}
129+
130+
// Filter should return true if this generator cares about this type.
131+
// (otherwise, GenerateType will not be called.)
132+
//
133+
// Filter is called before any of the generator's other functions;
134+
// subsequent calls will get a context with only the types that passed
135+
// this filter.
136+
func (g *gobatisGen) Filter(ctx *generator.Context, t *types.Type) bool {
137+
return true
138+
}
139+
140+
// If this generator needs special namers, return them here. These will
141+
// override the original namers in the context if there is a collision.
142+
// You may return nil if you don't need special names. These names will
143+
// be available in the context passed to the rest of the generator's
144+
// functions.
145+
//
146+
// A use case for this is to return a namer that tracks imports.
147+
func (g *gobatisGen) Namers(ctx *generator.Context) namer.NameSystems {
148+
return namer.NameSystems{
149+
"raw": namer.NewRawNamer(g.targetPkg, g.imports),
150+
}
151+
}
152+
153+
// Init should write an init function, and any other content that's not
154+
// generated per-type. (It's not intended for generator specific
155+
// initialization! Do that when your Package constructs the
156+
// Generators.)
157+
func (g *gobatisGen) Init(ctx *generator.Context, w io.Writer) error {
158+
return nil
159+
}
160+
161+
// Finalize should write finish up functions, and any other content that's not
162+
// generated per-type.
163+
func (g *gobatisGen) Finalize(ctx *generator.Context, w io.Writer) error {
164+
return nil
165+
}
166+
167+
// PackageVars should emit an array of variable lines. They will be
168+
// placed in a var ( ... ) block. There's no need to include a leading
169+
// \t or trailing \n.
170+
func (g *gobatisGen) PackageVars(ctx *generator.Context) []string {
171+
return nil
172+
}
173+
174+
// PackageConsts should emit an array of constant lines. They will be
175+
// placed in a const ( ... ) block. There's no need to include a leading
176+
// \t or trailing \n.
177+
func (g *gobatisGen) PackageConsts(ctx *generator.Context) []string {
178+
return nil
179+
}
180+
181+
// GenerateType should emit the code for a particular type.
182+
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
186+
}
187+
188+
func parseAnnotations(annotation string, t *types.Type) gobatisAnnotions {
189+
//ret := gobatisAnnotions{}
190+
//t.CommentLines
191+
return nil
192+
}
193+
194+
// Imports should return a list of necessary imports. They will be
195+
// formatted correctly. You do not need to include quotation marks,
196+
// return only the package name; alternatively, you can also return
197+
// imports in the format `name "path/to/pkg"`. Imports will be called
198+
// after Init, PackageVars, PackageConsts, and GenerateType, to allow
199+
// you to keep track of what imports you actually need.
200+
func (g *gobatisGen) Imports(ctx *generator.Context) []string {
201+
imports := g.imports.ImportLines()
202+
imports = append(imports, gobatisImports...)
203+
return imports
204+
}
205+
206+
// Preferred file name of this generator, not including a path. It is
207+
// allowed for multiple generators to use the same filename, but it's
208+
// up to you to make sure they don't have colliding import names.
209+
// TODO: provide per-file import tracking, removing the requirement
210+
// that generators coordinate..
211+
func (g *gobatisGen) Filename() string {
212+
return g.name + ".go"
213+
}
214+
215+
// A registered file type in the context to generate this file with. If
216+
// the FileType is not found in the context, execution will stop.
217+
func (g *gobatisGen) FileType() string {
218+
return generator.GolangFileType
219+
}

0 commit comments

Comments
 (0)