forked from fastwego/wxwork
/
build.go
376 lines (319 loc) · 9.85 KB
/
build.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
// Copyright 2020 FastWeGo
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"flag"
"fmt"
"io/ioutil"
"net/url"
"os"
"path"
"regexp"
"strings"
"github.com/iancoleman/strcase"
)
type Param struct {
Name string
Type string
}
type Api struct {
Name string
Description string
Request string
See string
FuncName string
GetParams []Param
}
type ApiGroup struct {
Name string
Apis []Api
Package string
}
var apiConfig = []ApiGroup{}
func main() {
var pkgFlag string
flag.StringVar(&pkgFlag, "package", "default", "")
flag.Parse()
apiConfig = apiConfigCrop
for _, group := range apiConfig {
if group.Package == pkgFlag || pkgFlag == "all" {
if group.Package == "oauth" {
continue
}
build(group)
}
}
if pkgFlag == "apilist" {
apilist()
}
}
func apilist() {
for _, group := range apiConfig {
fmt.Printf("- %s(%s)\n", group.Name, group.Package)
for _, api := range group.Apis {
split := strings.Split(api.Request, " ")
parse, _ := url.Parse(split[1])
if api.FuncName == "" {
api.FuncName = strcase.ToCamel(path.Base(parse.Path))
}
godocLink := fmt.Sprintf("https://pkg.go.dev/github.com/linbaozhong/wxwork/corporation/apis/%s?tab=doc#%s", group.Package, api.FuncName)
fmt.Printf("\t- [%s](%s) \n\t\t- [%s (%s)](%s)\n", api.Name, api.See, api.FuncName, parse.Path, godocLink)
}
}
}
func build(group ApiGroup) {
var funcs []string
var consts []string
var testFuncs []string
var exampleFuncs []string
for _, api := range group.Apis {
tpl := postFuncTpl
_FUNC_NAME_ := ""
_GET_PARAMS_ := ""
_GET_SUFFIX_PARAMS_ := ""
_UPLOAD_ := "media"
_FIELD_NAME_ := ""
_FIELDS_ := ""
_PAYLOAD_ := ""
switch {
case strings.Contains(api.Request, "GET http"):
tpl = getFuncTpl
case strings.Contains(api.Request, "POST http"):
tpl = postFuncTpl
case strings.Contains(api.Request, "POST(@media"):
tpl = postUploadFuncTpl
_UPLOAD_ = "media"
pattern := `POST\(@media\|field=(\S+)\) http`
reg := regexp.MustCompile(pattern)
matched := reg.FindAllStringSubmatch(api.Request, -1)
if matched != nil {
_FIELD_NAME_ = matched[0][1]
_PAYLOAD_ = ", payload []byte"
}
}
if len(api.GetParams) > 0 {
_GET_PARAMS_ = `, params url.Values`
//if strings.Contains(api.Request, "POST") {
// _GET_PARAMS_ = `, ` + _GET_PARAMS_
//}
_GET_SUFFIX_PARAMS_ = `+ "?" + params.Encode()`
}
split := strings.Split(api.Request, " ")
parseUrl, _ := url.Parse(split[1])
if api.FuncName == "" {
_FUNC_NAME_ = strcase.ToCamel(path.Base(parseUrl.Path))
} else {
_FUNC_NAME_ = api.FuncName
}
isTplApi := false
if (group.Package == "material" && api.FuncName == "Get") || group.Package == "material" && api.FuncName == "Jssdk" {
file, err := ioutil.ReadFile("tpl/" + group.Package + "." + api.FuncName + ".tpl")
if err != nil {
continue
}
tpl = commentTpl + string(file)
isTplApi = true
}
tpl = strings.ReplaceAll(tpl, "_TITLE_", api.Name)
tpl = strings.ReplaceAll(tpl, "_DESCRIPTION_", api.Description)
tpl = strings.ReplaceAll(tpl, "_REQUEST_", api.Request)
tpl = strings.ReplaceAll(tpl, "_SEE_", api.See)
tpl = strings.ReplaceAll(tpl, "_FUNC_NAME_", _FUNC_NAME_)
tpl = strings.ReplaceAll(tpl, "_UPLOAD_", _UPLOAD_)
tpl = strings.ReplaceAll(tpl, "_GET_PARAMS_", _GET_PARAMS_)
tpl = strings.ReplaceAll(tpl, "_GET_SUFFIX_PARAMS_", _GET_SUFFIX_PARAMS_)
if _FIELD_NAME_ != "" {
_FIELDS_ = strings.ReplaceAll(fieldTpl, "_FIELD_NAME_", _FIELD_NAME_)
}
tpl = strings.ReplaceAll(tpl, "_FIELDS_", _FIELDS_)
tpl = strings.ReplaceAll(tpl, "_PAYLOAD_", _PAYLOAD_)
funcs = append(funcs, tpl)
tpl = strings.ReplaceAll(constTpl, "_FUNC_NAME_", _FUNC_NAME_)
tpl = strings.ReplaceAll(tpl, "_API_PATH_", parseUrl.Path)
consts = append(consts, tpl)
if isTplApi { // 不用 test case
continue
}
// TestFunc
_TEST_ARGS_STRUCT_ := ""
switch {
case strings.Contains(api.Request, "GET http"):
_TEST_ARGS_STRUCT_ = `ctx *corporation.App , ` + _GET_PARAMS_
case strings.Contains(api.Request, "POST http"):
_TEST_ARGS_STRUCT_ = `ctx *corporation.App , payload []byte`
if _GET_PARAMS_ != "" {
_TEST_ARGS_STRUCT_ += `,` + _GET_PARAMS_
}
case strings.Contains(api.Request, "POST(@media"):
_TEST_ARGS_STRUCT_ = `ctx *corporation.App , ` + _UPLOAD_ + ` string` + _PAYLOAD_ + _GET_PARAMS_
}
_TEST_ARGS_STRUCT_ = strings.ReplaceAll(_TEST_ARGS_STRUCT_, ",", "\n")
_TEST_FUNC_SIGNATURE_ := ""
_EXAMPLE_ARGS_STMT_ := ""
if strings.TrimSpace(_TEST_ARGS_STRUCT_) != "" {
signatures := strings.Split(_TEST_ARGS_STRUCT_, "\n")
paramNames := []string{}
exampleStmt := []string{}
for _, signature := range signatures {
signature = strings.TrimSpace(signature)
tmp := strings.Split(signature, " ")
//fmt.Println(tmp)
if len(tmp[0]) > 0 {
paramNames = append(paramNames, "tt.args."+tmp[0])
switch tmp[1] {
case `[]byte`:
exampleStmt = append(exampleStmt, tmp[0]+" := []byte(\"{}\")")
case `string`:
exampleStmt = append(exampleStmt, tmp[0]+" := \"\"")
case `url.Values`:
exampleStmt = append(exampleStmt, tmp[0]+" := url.Values{}")
}
}
}
_TEST_FUNC_SIGNATURE_ = strings.Join(paramNames, ",")
_EXAMPLE_ARGS_STMT_ = strings.Join(exampleStmt, "\n")
}
tpl = strings.ReplaceAll(testFuncTpl, "_FUNC_NAME_", _FUNC_NAME_)
tpl = strings.ReplaceAll(tpl, "_TEST_ARGS_STRUCT_", _TEST_ARGS_STRUCT_)
tpl = strings.ReplaceAll(tpl, "_TEST_FUNC_SIGNATURE_", _TEST_FUNC_SIGNATURE_)
testFuncs = append(testFuncs, tpl)
//Example
tpl = strings.ReplaceAll(exampleFuncTpl, "_FUNC_NAME_", _FUNC_NAME_)
tpl = strings.ReplaceAll(tpl, "_PACKAGE_", path.Base(group.Package))
tpl = strings.ReplaceAll(tpl, "_TEST_FUNC_SIGNATURE_", strings.ReplaceAll(_TEST_FUNC_SIGNATURE_, "tt.args.", ""))
tpl = strings.ReplaceAll(tpl, "_EXAMPLE_ARGS_STMT_", _EXAMPLE_ARGS_STMT_)
exampleFuncs = append(exampleFuncs, tpl)
}
fileContent := fmt.Sprintf(fileTpl, path.Base(group.Package), group.Name, path.Base(group.Package), strings.Join(consts, ``), strings.Join(funcs, ``))
filename := "./../apis/" + group.Package + "/" + path.Base(group.Package) + ".go"
_ = os.MkdirAll(path.Dir(filename), 0644)
ioutil.WriteFile(filename, []byte(fileContent), 0644)
// output Test
testFileContent := fmt.Sprintf(testFileTpl, path.Base(group.Package), strings.Join(testFuncs, ``))
//fmt.Println(testFileContent)
ioutil.WriteFile("./../apis/"+group.Package+"/"+path.Base(group.Package)+"_test.go", []byte(testFileContent), 0644)
// output example
exampleFileContent := fmt.Sprintf(exampleFileTpl, path.Base(group.Package), strings.Join(exampleFuncs, ``))
//fmt.Println(testFileContent)
ioutil.WriteFile("./../apis/"+group.Package+"/example_"+path.Base(group.Package)+"_test.go", []byte(exampleFileContent), 0644)
}
var constTpl = `
api_FUNC_NAME_ = "_API_PATH_"`
var commentTpl = `
/*
_TITLE_
_DESCRIPTION_
See: _SEE_
_REQUEST_
*/`
var postFuncTpl = commentTpl + `
func _FUNC_NAME_(ctx *corporation.App, payload []byte_GET_PARAMS_) (resp []byte, err error) {
return ctx.Client.HTTPPost(api_FUNC_NAME__GET_SUFFIX_PARAMS_, bytes.NewReader(payload), "application/json;charset=utf-8")
}
`
var getFuncTpl = commentTpl + `
func _FUNC_NAME_(ctx *corporation.App _GET_PARAMS_) (resp []byte, err error) {
return ctx.Client.HTTPGet(api_FUNC_NAME__GET_SUFFIX_PARAMS_)
}
`
var postUploadFuncTpl = commentTpl + `
func _FUNC_NAME_(ctx *corporation.App, _UPLOAD_ string_PAYLOAD__GET_PARAMS_) (resp []byte, err error) {
r, w := io.Pipe()
m := multipart.NewWriter(w)
go func() {
defer w.Close()
defer m.Close()
part, err := m.CreateFormFile("_UPLOAD_", path.Base(_UPLOAD_))
if err != nil {
return
}
file, err := os.Open(_UPLOAD_)
if err != nil {
return
}
defer file.Close()
if _, err = io.Copy(part, file); err != nil {
return
}
_FIELDS_
}()
return ctx.Client.HTTPPost(api_FUNC_NAME__GET_SUFFIX_PARAMS_, r, m.FormDataContentType())
}
`
var fieldTpl = `
// field
err = m.WriteField("_FIELD_NAME_", string(payload))
if err != nil {
return
}
`
var fileTpl = `// Package %s %s
package %s
const (
%s
)
%s`
var testFileTpl = `package %s
func TestMain(m *testing.M) {
test.Setup()
os.Exit(m.Run())
}
%s
`
var testFuncTpl = `
func Test_FUNC_NAME_(t *testing.T) {
mockResp := map[string][]byte{
"case1": []byte("{\"errcode\":0,\"errmsg\":\"ok\"}"),
}
var resp []byte
test.MockSvrHandler.HandleFunc(api_FUNC_NAME_, func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(resp))
})
type args struct {
_TEST_ARGS_STRUCT_
}
tests := []struct {
name string
args args
wantResp []byte
wantErr bool
}{
{name: "case1", args: args{ctx: test.MockApp }, wantResp: mockResp["case1"], wantErr: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
resp = mockResp[tt.name]
gotResp, err := _FUNC_NAME_(_TEST_FUNC_SIGNATURE_)
//fmt.Println(string(gotResp), err)
if (err != nil) != tt.wantErr {
t.Errorf("_FUNC_NAME_() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(gotResp, tt.wantResp) {
t.Errorf("_FUNC_NAME_() gotResp = %v, want %v", gotResp, tt.wantResp)
}
})
}
}`
var exampleFileTpl = `package %s_test
%s
`
var exampleFuncTpl = `
func Example_FUNC_NAME_() {
var ctx *corporation.App
_EXAMPLE_ARGS_STMT_
resp, err := _PACKAGE_._FUNC_NAME_(_TEST_FUNC_SIGNATURE_)
fmt.Println(resp, err)
}
`