-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
192 lines (181 loc) · 4.62 KB
/
main.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// A commandline tool for generating rpc code by service methods.
//
// This tool can generate rpc client code ,rpc arg model for specific Go project dir.
// Usage :
// $gorpc [options]
// Available options:
//
// -client generate rpc client code.
//
// -d specific go project service dir (default ./service/)
//
// -model generate rpc arg model code.
//
// -o specific rpc client code output file. (default ./rpc/client/client.go)
//
// -m specific rpc arg model output file. (default ./model/rpc.go)
//
// -s print code to stdout.
// Example:
// $ cd $GOPATH/relation
// $ gorpc
// such command will generate rpc client code by functions of ./service/* and write code to $GOPATH/relation/rpc/client/client.go
package main
import (
"bytes"
"flag"
"fmt"
"go/importer"
"io"
"io/ioutil"
"log"
"path"
"go-common/app/tool/gorpc/goparser"
"go-common/app/tool/gorpc/input"
"go-common/app/tool/gorpc/model"
"golang.org/x/tools/imports"
)
const (
tpl = `var (
_noRes = &struct{}{}
)
type Service struct {
client *rpc.Client2
}
func New(c *rpc.ClientConfig) (s *Service) {
s = &Service{}
s.client = rpc.NewDiscoveryCli(c)
return
}`
)
type output struct {
rpcClient *bytes.Buffer
methodStr *bytes.Buffer
methods *bytes.Buffer
model *bytes.Buffer
}
var out = &output{
rpcClient: new(bytes.Buffer),
methodStr: new(bytes.Buffer),
methods: new(bytes.Buffer),
model: new(bytes.Buffer),
}
var (
dir = flag.String("d", "./service/", "source code dir")
argModel = flag.Bool("model", false, "use -model to generate rpc arg model")
client = flag.Bool("client", true, "use -client to generate rpc client code")
clientfile = flag.String("o", "./rpc/client/client.go", "out file name")
modelfile = flag.String("m", "./model/rpc.go", "generate rpc arg model")
std = flag.Bool("s", false, "use -s to print code to stdout")
)
func main() {
flag.Parse()
p := &goparser.Parser{Importer: importer.Default()}
files, err := input.Files(path.Dir(*dir))
if err != nil {
panic(err)
}
if *argModel {
out.model.WriteString("package model\n")
}
for _, f := range files {
rs, err := p.Parse(string(f), files)
if err != nil {
panic(err)
}
for _, f := range rs.Funcs {
if f.IsExported && len(f.Results) <= 1 && f.Receiver != nil && f.ReturnsError {
if *client {
out.generateMeStr(f)
out.generateMethod(f)
}
if *argModel {
out.generateModel(f)
}
}
}
}
if *argModel {
m, err := imports.Process("model.go", out.model.Bytes(), &imports.Options{TabWidth: 4})
if err != nil {
log.Printf("gopimorts err %v", err)
return
}
if *std {
fmt.Printf("%s", m)
} else {
ioutil.WriteFile(*modelfile, m, 0666)
}
}
if *client {
out.rpcClient.WriteString("package client \n")
out.rpcClient.WriteString(tpl)
out.rpcClient.WriteString("\nconst ( \n")
io.Copy(out.rpcClient, out.methodStr)
out.rpcClient.WriteString("\n)\n")
io.Copy(out.rpcClient, out.methods)
c, err := imports.Process("client.go", out.rpcClient.Bytes(), &imports.Options{TabWidth: 4})
if err != nil {
log.Printf("gopimorts err %v", err)
return
}
if *std {
fmt.Printf("%s", c)
} else {
ioutil.WriteFile(*clientfile, c, 0666)
}
}
}
func (o *output) generateMeStr(f *model.Function) {
b := o.methodStr
fmt.Fprintf(b, "_%s = \"RPC.%s\"\n", f.Name, f.Name)
}
func (o *output) generateModel(f *model.Function) {
b := o.model
for _, p := range f.Parameters {
if !p.IsBasicType() {
if p.Type.String() == "context.Context" {
continue
}
return
}
}
fmt.Fprintf(b, fmt.Sprintf("type Arg%s struct{\n", f.Name))
for _, p := range f.Parameters {
fmt.Fprintln(b, string(bytes.ToUpper([]byte(p.Name))), p.Type)
}
fmt.Fprintln(b, "}")
}
func (o *output) generateMethod(f *model.Function) {
b := o.methods
name := f.Name
for _, p := range f.Parameters {
if !p.IsBasicType() {
if p.Type.String() == "context.Context" {
continue
}
return
}
}
fmt.Fprintf(b, "func(s %s)%s(", f.Receiver.Type, f.Name)
fmt.Fprintf(b, "c context.Context, arg *model.Arg%s)(", f.Name)
if f.ReturnsError {
if len(f.Results) > 0 {
fmt.Fprintf(b, "res %s", f.Results[0].Type)
fmt.Fprint(b, ",")
}
fmt.Fprintf(b, "err error)")
} else {
fmt.Fprint(b, ")")
}
fmt.Fprint(b, " {\n")
if len(f.Results) == 0 {
fmt.Fprintf(b, "err = s.client.Call(c, _%s, arg, &_noRes)", name)
} else if f.Results[0].Type.IsStar {
fmt.Fprintf(b, "res = new(%s)\n", f.Results[0].Type.String()[1:])
fmt.Fprintf(b, "err = s.client.Call(c, _%s, arg, res)", name)
} else {
fmt.Fprintf(b, "err = s.client.Call(c, _%s, arg, &res)", name)
}
fmt.Fprintf(b, "\nreturn\n}\n")
}