/
utils.go
397 lines (354 loc) 路 11.6 KB
/
utils.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
package generator
import (
"fmt"
"go/types"
"os"
"path/filepath"
"strings"
"golang.org/x/tools/go/packages"
)
// FindAbsolutePathFromRelative finds the absolute path from a relative path.
func FindAbsolutePathFromRelative(relativePath string) string {
// Determine if the path is relative or absolute
if relativePath[0] == '/' {
return relativePath
}
// Remove the dot
if relativePath[0] == '.' && relativePath[1] == '/' {
relativePath = relativePath[2:]
}
wd, err := os.Getwd()
if err != nil {
FailErr(err)
}
return wd + "/" + relativePath
}
// FailErr prints the error and exits the program.
func FailErr(err error) {
if err != nil {
if _, fmtErr := fmt.Fprintf(os.Stderr, "Error: %v\n", err); fmtErr != nil {
panic(fmtErr)
}
os.Exit(1)
}
}
// isNamedType reports whether t is the named type path.name.
func IsNamedType(t types.Type, path, name string) bool {
n, ok := t.(*types.Named)
if !ok {
return false
}
obj := n.Obj()
return obj.Name() == name && obj.Pkg() != nil && obj.Pkg().Path() == path
}
// GetPathAndQualifiedName returns the package path and type name of a given type.
func GetPathAndQualifiedName(sourceType string, config Config) (string, string) {
idx := strings.LastIndexByte(sourceType, '/')
if idx == -1 {
FailErr(
fmt.Errorf(
`expected qualified type as "pkg/path.MyType", however found: %v`,
sourceType,
),
)
}
// Find the index of the first letter
firstLetterIndex := FindFirstLetterIndex(sourceType)
// Extract the package path and type name
typeName := sourceType[:firstLetterIndex] + sourceType[idx+1:]
packagePath := sourceType[firstLetterIndex:]
if config.gobTypes == nil {
config.gobTypes = make(map[string]string)
}
config.gobTypes[typeName] = packagePath
return packagePath, typeName
}
// SplitSourceType splits the source type into package and type name.
func SplitSourceType(sourceType string) (string, string) {
idx := strings.LastIndexByte(sourceType, '.')
if idx == -1 {
FailErr(
fmt.Errorf(
`expected qualified type as "pkg/path.MyType", however found: %v`,
sourceType,
),
)
}
sourceTypePackage := sourceType[0:idx]
sourceTypeName := sourceType[idx+1:]
return sourceTypePackage, sourceTypeName
}
// LoadPackage loads the package and returns the package.
func LoadPackage(path string) *packages.Package {
cfg := &packages.Config{
Mode: packages.NeedName | packages.NeedTypes | packages.NeedSyntax | packages.NeedTypesInfo | packages.NeedDeps | packages.NeedImports,
Dir: "",
Context: nil,
Logf: nil,
Env: nil,
BuildFlags: nil,
Fset: nil,
ParseFile: nil,
Tests: false,
Overlay: nil,
}
pkgs, err := packages.Load(cfg, path)
if err != nil {
FailErr(fmt.Errorf("loading packages for inspection: %w", err))
}
if packages.PrintErrors(pkgs) > 0 {
os.Exit(1)
}
return pkgs[0]
}
// LoadRepository loads the repository and returns the packages.
func LoadRepository() ([]*packages.Package, error) {
var pkgs []*packages.Package
dir, err := os.Getwd()
if err != nil {
return pkgs, fmt.Errorf("getting working directory: %w", err)
}
// Traverse up until go.mod is found
for {
joinedPath := filepath.Join(dir, "go.mod")
_, err = os.Stat(joinedPath)
if err != nil {
if os.IsNotExist(err) {
// No go.mod file found
dir = filepath.Dir(dir)
if dir == "/" {
return pkgs, fmt.Errorf("go.mod file not found in %s", dir)
}
continue
}
}
break
}
cfg := &packages.Config{
Mode: packages.NeedName | packages.NeedTypes | packages.NeedSyntax | packages.NeedTypesInfo | packages.NeedDeps | packages.NeedImports,
Dir: dir,
Context: nil,
Logf: nil,
Env: nil,
BuildFlags: nil,
Fset: nil,
ParseFile: nil,
Tests: false,
Overlay: nil,
}
pkgs, err = packages.Load(cfg, "./...")
if err != nil {
return pkgs, fmt.Errorf("loading packages for inspection: %w", err)
}
return pkgs, nil
}
// RequiresQual determines if the source type requires a qualified name.
func RequiresQual(sourceType string) bool {
return strings.Contains(sourceType, ".")
}
// FindFirstLetterIndex is a helper function to find the index of the first letter in a string.
func FindFirstLetterIndex(input string) int {
for i, char := range input {
if (char >= 'a' && char <= 'z') || (char >= 'A' && char <= 'Z') {
return i
}
}
return len(input)
}
// DefaultValueAsString generates the default value for a given types.Type and returns it as a string.
func DefaultValueAsString(t types.Type, config Config) (string, error) {
// Check if the type is a pointer, and dereference it if it is
if pointerType, ok := t.(*types.Pointer); ok {
t = pointerType.Elem()
}
// Create a new zero value of the type
var defaultValue any
switch t := t.(type) {
case *types.Basic:
// Basic types (e.g., int, string, bool)
switch t.Kind() {
case types.Int, types.Int8, types.Int16, types.Int32, types.Int64:
defaultValue = int64(0)
case types.Uint, types.Uint8, types.Uint16, types.Uint32, types.Uint64:
defaultValue = uint64(0)
case types.Float32, types.Float64:
defaultValue = float64(0)
case types.Complex64, types.Complex128:
defaultValue = complex128(0)
case types.String:
defaultValue = ""
case types.Bool:
defaultValue = false
case types.Invalid, types.Uintptr, types.UnsafePointer,
types.UntypedBool, types.UntypedInt, types.UntypedRune,
types.UntypedFloat, types.UntypedComplex, types.UntypedString,
types.UntypedNil:
return "", fmt.Errorf("unsupported basic type: %v", t)
}
case *types.Named:
// Named types
_, qualifiedName := GetPathAndQualifiedName(t.String(), config)
return qualifiedName + "{}", nil // Return typeName{}
case *types.Slice, *types.Map, *types.Array:
// Slice, map, and array types
defaultValue = "nil"
case *types.Struct:
// Struct type
structName := t.String() // Get the struct name
structName = strings.TrimPrefix(structName, "*") // Remove any leading *
return structName + "{}", nil // Return structName{}
case *types.Chan, *types.Interface, *types.Signature:
// Unsupported composite types
return "", fmt.Errorf("unsupported composite type: %T -> %v", t, t)
default:
// Unsupported type
return "", fmt.Errorf("unsupported type: %T -> %v", t, t)
}
// Convert the zero value to a string
return fmt.Sprintf("%v", defaultValue), nil
}
// FlattenType recursively expands composite types until primitive types are reached.
func FlattenType(typ types.Type) types.Type {
switch t := typ.(type) {
case *types.Named:
// If the type is a named type, expand its underlying type
return FlattenType(t.Underlying())
case *types.Array:
// If the type is an array, expand its element type
return types.NewSlice(FlattenType(t.Elem()))
case *types.Slice:
// If the type is a slice, expand its element type
return types.NewSlice(FlattenType(t.Elem()))
case *types.Map:
// If the type is a map, expand its key and value types
return types.NewMap(FlattenType(t.Key()), FlattenType(t.Elem()))
case *types.Pointer:
// If the type is a pointer, expand its element type
return FlattenType(t.Elem())
case *types.Chan:
// If the type is a channel, expand its element type
return FlattenType(t.Elem())
default:
// Return primitive types as is
return t
}
}
// GetPackageInfo returns the full import path and package name of a given type.
func GetPackageInfo(typ types.Type) (string, string) {
// Extract the object associated with the type
obj := typ.(*types.Named).Obj()
// Get the package of the object
pkg := obj.Pkg()
// Obtain the full import path of the package
fullPath := pkg.Path()
// Obtain just the package name
packageName := pkg.Name()
return fullPath, packageName
}
// IsGobSerializable checks if all methods of the given interface type
// only contain data that's serializable by encoding/gob.
func IsGobSerializable(interfaceType *types.Interface) bool {
// Iterate over all methods of the interface
for i := 0; i < interfaceType.NumMethods(); i++ {
method := interfaceType.Method(i)
// Check parameters of the method
for j := 0; j < method.Type().(*types.Signature).Params().Len(); j++ {
paramType := method.Type().(*types.Signature).Params().At(j).Type()
// Check if the parameter type is serializable by gob
if !IsGobType(paramType) {
return false
}
}
// Check return types of the method
for j := 0; j < method.Type().(*types.Signature).Results().Len(); j++ {
resultType := method.Type().(*types.Signature).Results().At(j).Type()
// Check if the return type is serializable by gob
if !IsGobType(resultType) {
return false
}
}
}
// All methods passed the serialization check
return true
}
// IsGobType checks if the given type is serializable by encoding/gob.
func IsGobType(typ types.Type) bool {
// Check if the type is one of the basic types supported by gob
switch typ.Underlying().(type) {
case *types.Basic:
// Check if the basic type is one of the types supported by gob
switch typ.Underlying().(*types.Basic).Kind() {
case types.Bool, types.Int, types.Int8, types.Int16, types.Int32, types.Int64,
types.Uint, types.Uint8, types.Uint16, types.Uint32, types.Uint64,
types.Uintptr, types.Float32, types.Float64, types.Complex64, types.Complex128,
types.String:
return true
case types.Invalid, types.UnsafePointer, types.UntypedBool,
types.UntypedInt, types.UntypedRune, types.UntypedFloat,
types.UntypedComplex, types.UntypedString, types.UntypedNil:
return false
}
case *types.Slice, *types.Array:
// Slices and arrays are serializable if their element type is serializable
elemType := typ.Underlying().(*types.Slice).Elem()
return IsGobType(elemType)
case *types.Pointer, *types.Map, *types.Chan, *types.Struct, *types.Interface:
// Pointers, maps, channels, structs, and interfaces are not directly supported by gob
// You can recursively check their underlying types to determine if they are serializable
return IsGobType(typ.Underlying())
default:
// Unsupported types
return false
}
return false
}
// ListImplementations returns a list of types that implement the given interface.
func ListImplementations(
interfacePkgs []*packages.Package,
interfaceName string,
) ([]types.Type, error) {
interfaceSearched, findErr := findInterfaceInfo(interfaceName, interfacePkgs)
if findErr != nil {
return nil, fmt.Errorf("finding interface info: %w", findErr)
}
implementations := findImplementations(interfaceSearched, interfacePkgs)
return implementations, nil
}
// findInterfaceInfo finds the interface info from the source packages.
func findInterfaceInfo(
interfaceName string,
srcPackages []*packages.Package,
) (*types.Interface, error) {
for _, pkg := range srcPackages {
scope := pkg.Types.Scope()
for _, name := range scope.Names() {
if name == interfaceName {
if typesObj, ok := scope.Lookup(name).(*types.TypeName); ok {
return typesObj.Type().Underlying().(*types.Interface), nil
}
}
}
}
return nil, fmt.Errorf("interface %s not found", interfaceName)
}
// findImplementations finds the implementations of the given interface.
func findImplementations(
interfaceSearched *types.Interface,
srcPackages []*packages.Package,
) []types.Type {
var result []types.Type
for _, pkg := range srcPackages {
scope := pkg.Types.Scope()
for _, name := range scope.Names() {
if typesObj, okLookup := scope.Lookup(name).(*types.TypeName); okLookup {
if _, okUnderlying := typesObj.Type().Underlying().(*types.Struct); okUnderlying {
if types.Implements(typesObj.Type(), interfaceSearched) ||
types.Implements(types.NewPointer(typesObj.Type()), interfaceSearched) {
result = append(result, typesObj.Type())
}
}
}
}
}
return result
}