-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
183 lines (155 loc) · 3.76 KB
/
util.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
package gogen
import (
"bytes"
"fmt"
"io"
"strings"
"text/template"
"github.com/wangliujing/goctl/api/spec"
"github.com/wangliujing/goctl/api/util"
"github.com/wangliujing/goctl/pkg/golang"
"github.com/wangliujing/goctl/util/pathx"
"github.com/zeromicro/go-zero/core/collection"
)
type fileGenConfig struct {
dir string
subdir string
filename string
templateName string
category string
templateFile string
builtinTemplate string
data any
}
func genFile(c fileGenConfig) error {
fp, created, err := util.MaybeCreateFile(c.dir, c.subdir, c.filename)
if err != nil {
return err
}
if !created {
return nil
}
defer fp.Close()
var text string
if len(c.category) == 0 || len(c.templateFile) == 0 {
text = c.builtinTemplate
} else {
text, err = pathx.LoadTemplate(c.category, c.templateFile, c.builtinTemplate)
if err != nil {
return err
}
}
t := template.Must(template.New(c.templateName).Parse(text))
buffer := new(bytes.Buffer)
err = t.Execute(buffer, c.data)
if err != nil {
return err
}
code := golang.FormatCode(buffer.String())
_, err = fp.WriteString(code)
return err
}
func writeProperty(writer io.Writer, name, tag, comment string, tp spec.Type, indent int) error {
util.WriteIndent(writer, indent)
var err error
if len(comment) > 0 {
comment = strings.TrimPrefix(comment, "//")
comment = "//" + comment
_, err = fmt.Fprintf(writer, "%s %s %s %s\n", strings.Title(name), tp.Name(), tag, comment)
} else {
_, err = fmt.Fprintf(writer, "%s %s %s\n", strings.Title(name), tp.Name(), tag)
}
return err
}
func getAuths(api *spec.ApiSpec) []string {
authNames := collection.NewSet()
for _, g := range api.Service.Groups {
jwt := g.GetAnnotation("jwt")
if len(jwt) > 0 {
authNames.Add(jwt)
}
}
return authNames.KeysStr()
}
func getJwtTrans(api *spec.ApiSpec) []string {
jwtTransList := collection.NewSet()
for _, g := range api.Service.Groups {
jt := g.GetAnnotation(jwtTransKey)
if len(jt) > 0 {
jwtTransList.Add(jt)
}
}
return jwtTransList.KeysStr()
}
func getMiddleware(api *spec.ApiSpec) []string {
result := collection.NewSet()
for _, g := range api.Service.Groups {
middleware := g.GetAnnotation("middleware")
if len(middleware) > 0 {
for _, item := range strings.Split(middleware, ",") {
result.Add(strings.TrimSpace(item))
}
}
}
return result.KeysStr()
}
func responseGoTypeName(r spec.Route, pkg ...string) string {
if r.ResponseType == nil {
return ""
}
resp := golangExpr(r.ResponseType, pkg...)
switch r.ResponseType.(type) {
case spec.DefineStruct:
if !strings.HasPrefix(resp, "*") {
return "*" + resp
}
}
return resp
}
func requestGoTypeName(r spec.Route, pkg ...string) string {
if r.RequestType == nil {
return ""
}
return golangExpr(r.RequestType, pkg...)
}
func golangExpr(ty spec.Type, pkg ...string) string {
switch v := ty.(type) {
case spec.PrimitiveType:
return v.RawName
case spec.DefineStruct:
if len(pkg) > 1 {
panic("package cannot be more than 1")
}
if len(pkg) == 0 {
return v.RawName
}
return fmt.Sprintf("%s.%s", pkg[0], strings.Title(v.RawName))
case spec.ArrayType:
if len(pkg) > 1 {
panic("package cannot be more than 1")
}
if len(pkg) == 0 {
return v.RawName
}
return fmt.Sprintf("[]%s", golangExpr(v.Value, pkg...))
case spec.MapType:
if len(pkg) > 1 {
panic("package cannot be more than 1")
}
if len(pkg) == 0 {
return v.RawName
}
return fmt.Sprintf("map[%s]%s", v.Key, golangExpr(v.Value, pkg...))
case spec.PointerType:
if len(pkg) > 1 {
panic("package cannot be more than 1")
}
if len(pkg) == 0 {
return v.RawName
}
return fmt.Sprintf("*%s", golangExpr(v.Type, pkg...))
case spec.InterfaceType:
return v.RawName
}
return ""
}