Repository navigation
Expand file tree
/
Copy pathpredicates.go
More file actions
255 lines (229 loc) · 7.74 KB
/
Copy pathpredicates.go
File metadata and controls
255 lines (229 loc) · 7.74 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
package goarchtest
import (
"strings"
)
// ResideInNamespace filters types that reside in the specified namespace/package
// It allows for filtering based on the package namespace of the type.
// Parameters:
// - namespace: A string representing the namespace to match against type packages
//
// Returns:
// - *TypeSet: Returns the filtered TypeSet containing only types whose packages match the namespace,
// allowing for method chaining
//
// Example:
//
// typeSet.ResideInNamespace("github.com/myorg/mypackage")
func (ts *TypeSet) ResideInNamespace(namespace string) *TypeSet {
ts.currentPredicate = "ResideInNamespace"
var filteredTypes []*TypeInfo
for _, t := range ts.types {
// Check exact match first
if t.FullPath == namespace {
filteredTypes = append(filteredTypes, t)
continue
}
// Check if namespace matches the end of the FullPath (relative path matching)
if strings.HasSuffix(t.FullPath, "/"+namespace) || strings.Contains(t.FullPath, "/"+namespace+"/") {
filteredTypes = append(filteredTypes, t)
continue
}
// Also check prefix match for full paths
if strings.HasPrefix(t.FullPath, namespace+"/") {
filteredTypes = append(filteredTypes, t)
continue
}
}
// Create a new TypeSet to avoid modifying the original
newTypeSet := &TypeSet{
types: filteredTypes,
originalTypes: ts.originalTypes, // Keep reference to original types
currentPredicate: ts.currentPredicate,
matchedPredicates: append([]string{}, ts.matchedPredicates...), // Copy slice
}
newTypeSet.matchedPredicates = append(newTypeSet.matchedPredicates, ts.currentPredicate)
return newTypeSet
}
// HaveDependencyOn filters types that have a dependency on the specified package
// It allows for filtering based on the import statements of the type.
// Parameters:
// - dependency: A string representing the package dependency to match against type imports
//
// Returns:
// - *TypeSet: Returns the filtered TypeSet containing only types that import the specified dependency,
// allowing for method chaining
//
// Example:
//
// typeSet.HaveDependencyOn("github.com/some/dependency")
func (ts *TypeSet) HaveDependencyOn(dependency string) *TypeSet {
ts.currentPredicate = "HaveDependencyOn"
var filteredTypes []*TypeInfo
for _, t := range ts.types {
for _, imp := range t.Imports {
// Exact match
if imp == dependency {
filteredTypes = append(filteredTypes, t)
break
}
// Prefix match with slash (for exact package boundaries)
if strings.HasPrefix(imp, dependency+"/") {
filteredTypes = append(filteredTypes, t)
break
}
// Suffix match for relative path matching (e.g., "infrastructure" matches "*/infrastructure")
if strings.HasSuffix(imp, "/"+dependency) {
filteredTypes = append(filteredTypes, t)
break
}
// Contains match for partial path matching (e.g., "infrastructure" matches "*/infrastructure/*")
if strings.Contains(imp, "/"+dependency+"/") {
filteredTypes = append(filteredTypes, t)
break
}
}
}
// Create a new TypeSet to avoid modifying the original
newTypeSet := &TypeSet{
types: filteredTypes,
originalTypes: ts.originalTypes, // Keep reference to original types
currentPredicate: ts.currentPredicate,
matchedPredicates: append([]string{}, ts.matchedPredicates...), // Copy slice
}
newTypeSet.matchedPredicates = append(newTypeSet.matchedPredicates, ts.currentPredicate)
return newTypeSet
}
// ImplementInterface filters types that implement the specified interface
// It allows for filtering based on the interfaces implemented by the type.
// Parameters:
// - interfaceName: A string representing the name of the interface to check against
//
// Returns:
// - *TypeSet: Returns the filtered TypeSet containing only types that implement the specified interface,
// allowing for method chaining
//
// Example:
//
// typeSet.ImplementInterface("MyInterface")
func (ts *TypeSet) ImplementInterface(interfaceName string) *TypeSet {
ts.currentPredicate = "ImplementInterface"
var filteredTypes []*TypeInfo
for _, t := range ts.types {
for _, iface := range t.Interfaces {
if iface == interfaceName {
filteredTypes = append(filteredTypes, t)
break
}
}
}
ts.types = filteredTypes
ts.matchedPredicates = append(ts.matchedPredicates, ts.currentPredicate)
return ts
}
// BeStruct filters types that are structs
// It allows for filtering based on whether the type is a struct.
// Returns:
// - *TypeSet: Returns the filtered TypeSet containing only struct types,
// allowing for method chaining
//
// Example:
//
// typeSet.BeStruct()
func (ts *TypeSet) BeStruct() *TypeSet {
ts.currentPredicate = "BeStruct"
var filteredTypes []*TypeInfo
for _, t := range ts.types {
if t.IsStruct {
filteredTypes = append(filteredTypes, t)
}
}
ts.types = filteredTypes
ts.matchedPredicates = append(ts.matchedPredicates, ts.currentPredicate)
return ts
}
// And combines predicates (logical AND)
// It allows for chaining multiple predicates together, ensuring that all conditions must be met.
// Returns:
// - *TypeSet: Returns the TypeSet itself to allow for method chaining
//
// Example:
//
// typeSet.And().HaveDependencyOn("github.com/some/dependency").BeStruct()
func (ts *TypeSet) And() *TypeSet {
ts.currentPredicate = "And"
// No filtering needed, this is just a logical connector
return ts
}
// Or performs a union with another TypeSet (logical OR)
// It allows for combining two TypeSets, resulting in a new TypeSet that contains types from both sets.
// Returns:
// - *TypeSet: Returns a new TypeSet that is the union of the two sets, allowing for method chaining
//
// Example:
//
// typeSet1.Or(typeSet2)
func (ts *TypeSet) Or(other *TypeSet) *TypeSet {
ts.currentPredicate = "Or"
// Create a union of the two type sets
unionMap := make(map[string]bool)
for _, t := range ts.types {
key := t.Package + "." + t.Name
unionMap[key] = true
}
for _, t := range other.types {
key := t.Package + "." + t.Name
if !unionMap[key] {
ts.types = append(ts.types, t)
unionMap[key] = true
}
}
ts.matchedPredicates = append(ts.matchedPredicates, ts.currentPredicate)
return ts
}
// Should reverses the condition for the following predicates
// It allows for asserting that the following predicates should hold true.
// Returns:
// - *TypeSet: Returns the TypeSet itself to allow for method chaining
//
// Example:
//
// ts.Should().HaveDependencyOn("github.com/some/dependency").BeStruct()
func (ts *TypeSet) Should() *TypeSet {
ts.currentPredicate = "Should"
// Store the current types for later reference
originalTypes := ts.types
ts.originalTypes = originalTypes
return ts
}
// ShouldNot reverses the condition for the following predicates
// It allows for asserting that the following predicates should not hold true.
// Returns:
// - *TypeSet: Returns the TypeSet itself to allow for method chaining
//
// Example:
//
// ts.ShouldNot().HaveDependencyOn("github.com/some/dependency").BeStruct()
func (ts *TypeSet) ShouldNot() *TypeSet {
ts.currentPredicate = "ShouldNot"
// Create a new TypeSet to avoid modifying the original
newTypeSet := &TypeSet{
types: append([]*TypeInfo{}, ts.types...), // Copy types slice
originalTypes: ts.originalTypes,
currentPredicate: ts.currentPredicate,
matchedPredicates: append([]string{}, ts.matchedPredicates...), // Copy slice
}
newTypeSet.matchedPredicates = append(newTypeSet.matchedPredicates, "Negate")
return newTypeSet
}
// Not negates the following predicate
// It allows for negating the condition of the next predicate.
// Returns:
// - *TypeSet: Returns the TypeSet itself to allow for method chaining
//
// Example:
//
// ts.Not().HaveDependencyOn("github.com/some/dependency")
func (ts *TypeSet) Not() *TypeSet {
ts.currentPredicate = "Not"
return ts
}