This repository was archived by the owner on Mar 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen.go
187 lines (164 loc) · 3.91 KB
/
gen.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
package pica
import (
"encoding/json"
"fmt"
"os"
"strings"
"text/template"
"unicode"
_ "embed"
"github.com/jerloo/funny"
"github.com/mozillazg/go-pinyin"
"github.com/rbretecher/go-postman-collection"
)
type ScriptsGenerator interface {
Name() string
Generate(filename string) string
}
func NewScriptsGenerator(name string) ScriptsGenerator {
return map[string]ScriptsGenerator{
"postman": &PostmanScriptsGenerator{},
"swagger2": &Swagger2ScriptsGenerator{},
}[name]
}
type Swagger2ScriptsGenerator struct {
}
func (generator *Swagger2ScriptsGenerator) Name() string {
return "swagger2"
}
func (generator *Swagger2ScriptsGenerator) Generate(filename string) string {
return ""
}
type PostmanScriptsGenerator struct {
}
func (generator *PostmanScriptsGenerator) Name() string {
return "postman"
}
func (generator *PostmanScriptsGenerator) Generate(filename string) string {
file, err := os.Open(filename)
defer func() {
file.Close()
}()
if err != nil {
panic(err)
}
c, err := postman.ParseCollection(file)
if err != nil {
panic(err)
}
names := make(map[string]bool)
items := genItems("", c.Items)
for index, item := range items {
if item.Description == "" {
item.Description = item.Name
}
hansName := false
for _, r := range item.Name {
if unicode.Is(unicode.Han, r) {
hansName = true
}
}
if hansName {
item.Name = safePinYin(item.Name)
}
if _, ok := names[item.Name]; ok {
item.Name = fmt.Sprintf("%s%d", item.Name, index)
} else {
names[item.Name] = true
}
}
t, err := template.New("postman").Funcs(template.FuncMap{
"getQueryNames": getQuery,
"joinQuery": joinQuery,
"joinStrArr": joinStrArr,
"body2Map": body2Map,
}).Parse(GenFromPostmanTemplate)
if err != nil {
panic(err)
}
builder := strings.Builder{}
err = t.Execute(&builder, items)
if err != nil {
panic(err)
}
// for _, item := range items {
// item.Request.Method
// item.Request.URL.Variables
// item.Request.Body.Raw
// }
data := builder.String()
return funny.Format([]byte(data), "")
}
//go:embed gen_template.funny.txt
var GenFromPostmanTemplate string
//字符首字母大写转换
func Capitalize(str string) string {
var upperStr string
vv := []rune(str) // 后文有介绍
for i := 0; i < len(vv); i++ {
if i == 0 {
if vv[i] >= 97 && vv[i] <= 122 { // 后文有介绍
vv[i] -= 32 // string的码表相差32位
upperStr += string(vv[i])
} else {
fmt.Println("Not begins with lowercase letter,")
return str
}
} else {
upperStr += string(vv[i])
}
}
return upperStr
}
func body2Map(body string) (result map[string]interface{}) {
err := json.Unmarshal([]byte(body), &result)
if err != nil {
panic(err)
}
return
}
func joinStrArr(items []string, sp string) string {
return strings.Join(items, sp)
}
func getQuery(item *postman.Items) []string {
var arr []string
if item.Request.URL.Query != nil {
for _, arg := range item.Request.URL.Query.([]interface{}) {
a := arg.(map[string]interface{})
arr = append(arr, a["key"].(string))
}
}
return arr
}
func joinQuery(item *postman.Items) string {
var arr []string
if item.Request.URL.Query != nil {
for _, arg := range item.Request.URL.Query.([]interface{}) {
if arg != nil {
a := arg.(map[string]interface{})
arr = append(arr, a["key"].(string))
}
}
}
return strings.Join(arr, ",")
}
func safePinYin(hanz string) string {
ps := pinyin.LazyConvert(hanz, nil)
for index, psItem := range ps {
ps[index] = Capitalize(psItem)
}
return strings.Join(ps, "")
}
func genItems(nameRoot string, item []*postman.Items) (results []*postman.Items) {
for _, child := range item {
if child.IsGroup() {
results = append(results, genItems(fmt.Sprintf("%s_%s", nameRoot, safePinYin(child.Name)), child.Items)...)
} else {
if nameRoot != "" {
child.Name = fmt.Sprintf("%s_%s", nameRoot, child.Name)
}
results = append(results, child)
}
}
return results
}