-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_proto.go
91 lines (83 loc) · 2.11 KB
/
data_proto.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package generator
import (
"bytes"
_ "embed"
"fmt"
"os"
"path"
"strings"
"text/template"
"github.com/iancoleman/strcase"
"github.com/yoogoc/kratos-scaffold/pkg/field"
"github.com/yoogoc/kratos-scaffold/pkg/util"
"golang.org/x/tools/imports"
)
//go:embed tmpl/data_proto_data.tmpl
var dataProtoDataTmpl string
//go:embed tmpl/data_proto_transfer.tmpl
var dataProtoTransferTmpl string
func (d *Data) GenerateProto() error {
// 2. gen data transfer
if err := d.genTransferProto(); err != nil {
return err
}
// 3. gen data
if err := d.genDataProto(); err != nil {
return err
}
return nil
}
func (d *Data) genTransferProto() error {
fmt.Println("generating data transfer...")
buf := new(bytes.Buffer)
funcMap := template.FuncMap{
"ToLower": strings.ToLower,
"ToPlural": util.Plural,
"ToCamel": strcase.ToCamel,
"ToLowerCamel": strcase.ToLowerCamel,
"ToEntName": field.EntName,
}
tmpl, err := template.New("dataProtoTransferTmpl").Funcs(funcMap).Parse(dataProtoTransferTmpl)
if err != nil {
return err
}
err = tmpl.Execute(buf, d)
if err != nil {
return err
}
p := path.Join(d.OutPath(), strcase.ToSnake(d.Name)+"_transfer.go")
content, err := imports.Process(p, buf.Bytes(), nil)
if err != nil {
return err
}
return os.WriteFile(p, content, 0o644)
}
func (d *Data) genDataProto() error {
fmt.Println("generating data...")
buf := new(bytes.Buffer)
funcMap := template.FuncMap{
"ToLower": strings.ToLower,
"ToPlural": util.Plural,
"ToCamel": strcase.ToCamel,
"ToCamelPlural": func(s string) string { return strcase.ToCamel(util.Plural(s)) },
"ToLowerCamel": strcase.ToLowerCamel,
"last": func(x int, a []*field.Field) bool {
return x == len(a)-1
},
}
tmpl, err := template.New("dataProtoDataTmpl").Funcs(funcMap).Parse(dataProtoDataTmpl)
if err != nil {
return err
}
err = tmpl.Execute(buf, d)
if err != nil {
return err
}
p := path.Join(d.OutPath(), strcase.ToSnake(d.Name)+".go")
// content := buf.Bytes()
content, err := imports.Process(p, buf.Bytes(), nil)
if err != nil {
return err
}
return os.WriteFile(p, content, 0o644)
}