-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate.go
293 lines (261 loc) · 8.37 KB
/
generate.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
package interfaces
import (
"bytes"
"embed"
"fmt"
"go/format"
"os"
"path"
"reflect"
"regexp"
"strings"
"text/template"
"github.com/cloudquery/plugin-sdk/v4/caser"
"github.com/jpillora/longestcommon"
"golang.org/x/exp/maps"
)
//go:embed templates/*.go.tpl
var templatesFS embed.FS
type Options struct {
// ShouldInclude tests whether a method should be included in the generated interfaces. If it returns true,
// the method will be included. MethodHasPrefix and MethodHasSuffix can be used inside a custom function here
// to customize the behavior.
ShouldInclude func(reflect.Method) bool
// ExtraImports can add extra imports for a method
ExtraImports func(reflect.Method) []string
// SinglePackage allows to generate all passed clients into a single package.
// The clients will get their package name as prefix to the interface name (e.g., s3.Client -> S3Client)
SinglePackage string
}
func (o *Options) SetDefaults() {
if o.ShouldInclude == nil {
o.ShouldInclude = func(reflect.Method) bool { return true }
}
if o.ExtraImports == nil {
o.ExtraImports = func(reflect.Method) []string { return nil }
}
}
type Option func(*Options)
func WithIncludeFunc(f func(reflect.Method) bool) Option {
return func(o *Options) {
o.ShouldInclude = f
}
}
func WithExtraImports(f func(reflect.Method) []string) Option {
return func(o *Options) {
o.ExtraImports = f
}
}
func WithSinglePackage(name string) Option {
return func(o *Options) {
o.SinglePackage = name
}
}
func getPackageNames(clientInfos []clientInfo) []string {
versionPattern := regexp.MustCompile(`/v\d+$`)
allImports := make([]string, len(clientInfos))
for i, clientInfo := range clientInfos {
allImports[i] = clientInfo.Import
}
// To get the shortest possible package name without collisions, we need to find the longest common prefix
importPrefix := longestcommon.Prefix(allImports)
packageNames := make([]string, len(clientInfos))
for i, clientInfo := range clientInfos {
var pkgName string
if clientInfo.Import == importPrefix {
pkgName = versionPattern.ReplaceAllString(clientInfo.Import, "")
pkgName = path.Base(pkgName)
} else {
pkgName = strings.TrimPrefix(clientInfo.Import, importPrefix)
pkgName = strings.ReplaceAll(versionPattern.ReplaceAllString(pkgName, ""), "/", "_")
}
packageNames[i] = strings.ReplaceAll(pkgName, "-", "")
}
return packageNames
}
func getTemplateDataFromClientInfos(clientInfos []clientInfo, options *Options) []serviceTemplateData {
packageNames := getPackageNames(clientInfos)
services := make([]serviceTemplateData, 0)
serviceMap := make(map[string][]clientInfo)
for i, clientInfo := range clientInfos {
serviceMap[packageNames[i]] = append(serviceMap[packageNames[i]], clientInfo)
}
for packageName, infos := range serviceMap {
imports := make(map[string]bool)
clientsTemplateData := make([]clientTemplateData, 0)
for _, clientInfo := range infos {
imports[clientInfo.Import] = true
for _, extraImport := range clientInfo.ExtraImports {
imports[extraImport] = true
}
clientsTemplateData = append(clientsTemplateData, clientInfo.templateData(len(options.SinglePackage) > 0))
}
svc := serviceTemplateData{
PackageName: packageName,
FileName: packageName,
Imports: maps.Keys(imports),
Clients: clientsTemplateData,
}
if len(options.SinglePackage) > 0 {
svc.PackageName = options.SinglePackage
}
services = append(services, svc)
}
return services
}
// Generate generates service interfaces to be used for generating
// mocks. The clients passed in as the first argument should be structs that will be used to
// generate the service interfaces. The second argument, dir, is the path to the output
// directory where the service interface files will be created.
func Generate(clients []any, dir string, opts ...Option) error {
options := &Options{}
for _, opt := range opts {
opt(options)
}
options.SetDefaults()
clientInfos := make([]clientInfo, 0)
for _, client := range clients {
clientInfos = append(clientInfos, getClientInfo(client, options))
}
// write individual service files
serviceTpl, err := template.New("service.go.tpl").ParseFS(templatesFS, "templates/service.go.tpl")
if err != nil {
return err
}
services := getTemplateDataFromClientInfos(clientInfos, options)
for _, service := range services {
buff := bytes.Buffer{}
if err := serviceTpl.Execute(&buff, service); err != nil {
return fmt.Errorf("failed to execute template: %w", err)
}
err := formatAndWriteFile(service.getFilePath(dir), buff)
if err != nil {
return fmt.Errorf("failed to format and write file for service %v: %w", service, err)
}
}
return nil
}
func normalizedGenericTypeName(str string) string {
// Generic output types have the full import path in the string value, so we need to normalize it
pattern := regexp.MustCompile(`\[(.*?)\]`)
groups := pattern.FindStringSubmatch((str))
if len(groups) < 2 {
return str
}
typeName := groups[1]
normalizedGenericTypeName := strings.Split(typeName, "/")
importName := normalizedGenericTypeName[len(normalizedGenericTypeName)-1]
versionPattern := regexp.MustCompile(`/v\d+\.`)
if versionPattern.MatchString(typeName) {
// Example typeName: github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/appconfiguration/armappconfiguration/v2.ConfigurationStoresClientCreateResponse
importName = normalizedGenericTypeName[len(normalizedGenericTypeName)-2] + "." + strings.Split(normalizedGenericTypeName[len(normalizedGenericTypeName)-1], ".")[1]
}
return pattern.ReplaceAllString(str, "["+importName+"]")
}
// Adapted from https://stackoverflow.com/a/54129236
func signature(name string, f any) string {
t := reflect.TypeOf(f)
if t.Kind() != reflect.Func {
return "<not a function>"
}
buf := strings.Builder{}
buf.WriteString(name + "(")
for i := 0; i < t.NumIn(); i++ {
if i > 0 {
buf.WriteString(", ")
}
if t.IsVariadic() && i == t.NumIn()-1 {
buf.WriteString("..." + strings.TrimPrefix(t.In(i).String(), "[]"))
} else {
buf.WriteString(t.In(i).String())
}
}
buf.WriteString(")")
if numOut := t.NumOut(); numOut > 0 {
if numOut > 1 {
buf.WriteString(" (")
} else {
buf.WriteString(" ")
}
for i := 0; i < t.NumOut(); i++ {
if i > 0 {
buf.WriteString(", ")
}
buf.WriteString(normalizedGenericTypeName(t.Out(i).String()))
}
if numOut > 1 {
buf.WriteString(")")
}
}
return buf.String()
}
type clientInfo struct {
Import string
ClientName string
Signatures []string
ExtraImports []string
}
func (c clientInfo) templateData(singlePackageMode bool) clientTemplateData {
var packageName string
if singlePackageMode {
packageName = caser.New().ToPascal(getPackageNames([]clientInfo{c})[0])
}
return clientTemplateData{
Name: packageName + c.ClientName,
Signatures: c.Signatures,
}
}
type clientTemplateData struct {
Name string
Signatures []string
}
type serviceTemplateData struct {
PackageName string
FileName string
Imports []string
Clients []clientTemplateData
}
func (s serviceTemplateData) getFilePath(baseDir string) string {
if s.FileName == s.PackageName {
return path.Join(baseDir, s.PackageName, fmt.Sprintf("%s.go", s.PackageName))
}
return path.Join(baseDir, fmt.Sprintf("%s.go", s.FileName))
}
func getClientInfo(client any, opts *Options) clientInfo {
v := reflect.ValueOf(client)
t := v.Type()
pkgPath := t.Elem().PkgPath()
clientName := t.Elem().Name()
signatures := make([]string, 0)
extraImports := make([]string, 0)
for i := 0; i < t.NumMethod(); i++ {
method := t.Method(i)
if opts.ShouldInclude(method) {
sig := signature(method.Name, v.Method(i).Interface())
signatures = append(signatures, sig)
}
extraImports = append(extraImports, opts.ExtraImports(method)...)
}
return clientInfo{
Import: pkgPath,
ClientName: clientName,
Signatures: signatures,
ExtraImports: extraImports,
}
}
func formatAndWriteFile(filePath string, buff bytes.Buffer) error {
content := buff.Bytes()
formattedContent, err := format.Source(buff.Bytes())
if err != nil {
fmt.Printf("failed to format source: %s: %v\n", filePath, err)
} else {
content = formattedContent
}
if err := os.MkdirAll(path.Dir(filePath), 0755); err != nil {
return fmt.Errorf("failed to create directory %s: %w", path.Dir(filePath), err)
}
if err := os.WriteFile(filePath, content, 0644); err != nil {
return fmt.Errorf("failed to write file %s: %w", filePath, err)
}
return nil
}