Repository navigation
Expand file tree
/
Copy pathtypes.go
More file actions
279 lines (245 loc) · 7.46 KB
/
Copy pathtypes.go
File metadata and controls
279 lines (245 loc) · 7.46 KB
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
package goarchtest
import (
"fmt"
"go/ast"
"go/token"
"os"
"strings"
"golang.org/x/tools/go/packages"
)
// Types represents the entry point for architecture testing
type Types struct {
pkgs []*packages.Package
typeSet *TypeSet
}
// TypeSet represents a collection of types that match certain criteria
type TypeSet struct {
types []*TypeInfo
originalTypes []*TypeInfo
currentPredicate string
matchedPredicates []string
}
// TypeInfo contains comprehensive information about a Go type.
//
// This structure holds all the metadata needed for architectural analysis,
// including the type's name, location, dependencies, and structural characteristics.
//
// Fields:
// - Name: The name of the type (e.g., "UserService")
// - Package: The package name where the type is defined (e.g., "services")
// - FullPath: The full import path (e.g., "github.com/myorg/myapp/services")
// - Imports: All import paths that this type's package depends on
// - Interfaces: For interface types, the method names defined in the interface
// - IsStruct: true if this type is a struct
// - IsInterface: true if this type is an interface
//
// TypeInfo is used throughout GoArchTest's predicate system to make architectural
// decisions and validate constraints.
type TypeInfo struct {
Name string
Package string
FullPath string
Imports []string
Interfaces []string
IsStruct bool
IsInterface bool
}
// InPath creates a new Types instance for packages in the specified directory path.
//
// This is the primary entry point for GoArchTest. It analyzes all Go packages
// found recursively in the given directory path and prepares them for architectural testing.
//
// Parameters:
// - path: The directory path to analyze. Use "." for current directory or provide an absolute path.
//
// Returns:
// - *Types: A Types instance containing all discovered types, ready for filtering and testing.
//
// Example:
//
// // Analyze current project
// types := goarchtest.InPath("./")
//
// // Analyze specific directory
// types := goarchtest.InPath("/path/to/project")
//
// // Start testing architecture
// result := types.That().
// ResideInNamespace("domain").
// ShouldNot().
// HaveDependencyOn("infrastructure").
// GetResult()
//
// The function uses Go's package loading mechanism to extract comprehensive
// type information including names, packages, imports, and structural details.
func InPath(path string) *Types {
cfg := &packages.Config{
Mode: packages.NeedName | packages.NeedTypes | packages.NeedSyntax | packages.NeedTypesInfo | packages.NeedImports,
Dir: path,
}
pkgs, err := packages.Load(cfg, "./...")
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to load packages: %v\n", err)
return &Types{
pkgs: []*packages.Package{},
typeSet: &TypeSet{types: []*TypeInfo{}, originalTypes: []*TypeInfo{}},
}
}
return &Types{
pkgs: pkgs,
typeSet: extractTypesFromPackages(pkgs),
}
}
// That starts a filter chain to select types
func (t *Types) That() *TypeSet {
return t.typeSet.That()
}
// extractTypesFromPackages processes the packages to extract type information
func extractTypesFromPackages(pkgs []*packages.Package) *TypeSet {
var types []*TypeInfo
for _, pkg := range pkgs {
// Skip packages with errors
if len(pkg.Errors) > 0 {
continue
}
imports := make([]string, 0)
for importPath := range pkg.Imports {
imports = append(imports, importPath)
}
// Get types from this package using syntax trees since we can't easily
// map from types.Object to struct/interface information
for _, file := range pkg.Syntax {
for _, decl := range file.Decls {
genDecl, ok := decl.(*ast.GenDecl)
if !ok || genDecl.Tok != token.TYPE {
continue
}
for _, spec := range genDecl.Specs {
typeSpec, ok := spec.(*ast.TypeSpec)
if !ok {
continue
}
typeInfo := &TypeInfo{
Name: typeSpec.Name.Name,
Package: pkg.Name,
FullPath: pkg.PkgPath,
Imports: imports,
}
// Check if it's a struct
if _, ok := typeSpec.Type.(*ast.StructType); ok {
typeInfo.IsStruct = true
}
// Check if it's an interface
if interfaceType, ok := typeSpec.Type.(*ast.InterfaceType); ok {
typeInfo.IsInterface = true
// Collect method names from the interface
if interfaceType.Methods != nil {
for _, method := range interfaceType.Methods.List {
if method.Names != nil {
for _, name := range method.Names {
typeInfo.Interfaces = append(typeInfo.Interfaces, name.Name)
}
}
}
}
}
types = append(types, typeInfo)
}
}
}
}
return &TypeSet{
types: types,
originalTypes: types,
}
}
// That starts a filter chain
func (ts *TypeSet) That() *TypeSet {
ts.currentPredicate = "That"
return ts
}
// Result represents the outcome of architecture tests.
//
// It contains information about whether the architectural rule passed or failed,
// along with details about any types that violated the rule.
//
// Fields:
// - IsSuccessful: true if the architectural test passed, false otherwise
// - FailingTypes: slice of TypeInfo for types that didn't meet the criteria
//
// Example usage:
//
// result := types.That().ResideInNamespace("domain").GetResult()
// if !result.IsSuccessful {
// fmt.Printf("Found %d violations\n", len(result.FailingTypes))
// for _, failing := range result.FailingTypes {
// fmt.Printf("- %s in %s\n", failing.Name, failing.Package)
// }
// }
type Result struct {
IsSuccessful bool
FailingTypes []*TypeInfo
}
// GetResult evaluates the predicates and returns the result
func (ts *TypeSet) GetResult() *Result {
// If no predicates were applied, the test passes
if len(ts.matchedPredicates) == 0 {
return &Result{
IsSuccessful: true,
}
}
// Check if we have a negation in the predicates
shouldNegate := false
for _, pred := range ts.matchedPredicates {
if pred == "Negate" {
shouldNegate = true
break
}
}
// If we're negating, the result is successful if we have NO matching types
if shouldNegate {
return &Result{
IsSuccessful: len(ts.types) == 0,
FailingTypes: ts.types, // If we're negating, the failing types are the ones that matched
}
}
// Otherwise, the result is successful if we have matching types
return &Result{
IsSuccessful: len(ts.types) > 0,
FailingTypes: ts.getFailingTypes(),
}
}
// getFailingTypes returns types that didn't match the predicates
func (ts *TypeSet) getFailingTypes() []*TypeInfo {
var failingTypes []*TypeInfo
// Compare original types with the filtered ones
for _, origType := range ts.originalTypes {
found := false
for _, filteredType := range ts.types {
if origType.FullPath == filteredType.FullPath && origType.Name == filteredType.Name {
found = true
break
}
}
if !found {
failingTypes = append(failingTypes, origType)
}
}
return failingTypes
}
// GetAllTypes returns all types in the TypeSet
func (ts *TypeSet) GetAllTypes() []*TypeInfo {
return ts.types
}
// GetFailureDetails returns a detailed error message about failing types
func (r *Result) GetFailureDetails() string {
if r.IsSuccessful {
return "No failures detected"
}
var details strings.Builder
details.WriteString(fmt.Sprintf("Found %d failing type(s):\n", len(r.FailingTypes)))
for i, failingType := range r.FailingTypes {
details.WriteString(fmt.Sprintf("%d. %s in package %s\n", i+1, failingType.Name, failingType.Package))
}
return details.String()
}