-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlayer_classifier.go
More file actions
181 lines (167 loc) · 5.44 KB
/
layer_classifier.go
File metadata and controls
181 lines (167 loc) · 5.44 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
package analyzer
import (
"regexp"
"strings"
"github.com/randomcodespace/codeiq/internal/model"
)
// LayerClassifier assigns a Layer value to every CodeNode based on
// (kind, framework, file_path) heuristics. Pure, deterministic, first-match
// wins. Priority order mirrors LayerClassifier.java:
// 1. Node kind (frontend / backend / infra)
// 2. Language (infra)
// 3. File extension + path
// 4. Framework
// 5. Shared node kinds
// 6. Fallback package/path heuristics + Java src/main convention
type LayerClassifier struct{}
var (
frontendKinds = map[model.NodeKind]struct{}{
model.NodeComponent: {},
model.NodeHook: {},
}
backendKinds = map[model.NodeKind]struct{}{
model.NodeGuard: {},
model.NodeMiddleware: {},
model.NodeEndpoint: {},
model.NodeRepository: {},
model.NodeDatabaseConnection: {},
model.NodeQuery: {},
model.NodeEntity: {},
model.NodeMigration: {},
model.NodeService: {},
model.NodeTopic: {},
model.NodeQueue: {},
model.NodeEvent: {},
model.NodeMessageQueue: {},
model.NodeRMIInterface: {},
model.NodeWebSocketEndpoint: {},
}
infraKinds = map[model.NodeKind]struct{}{
model.NodeInfraResource: {},
model.NodeAzureResource: {},
model.NodeAzureFunction: {},
model.NodeSQLEntity: {},
}
sharedKinds = map[model.NodeKind]struct{}{
model.NodeConfigFile: {},
model.NodeConfigKey: {},
model.NodeConfigDefinition: {},
model.NodeProtocolMessage: {},
}
infraLangs = map[string]struct{}{
"terraform": {},
"bicep": {},
"dockerfile": {},
}
frontendFrameworks = map[string]struct{}{
"react": {},
"vue": {},
"angular": {},
"svelte": {},
"nextjs": {},
}
backendFrameworks = map[string]struct{}{
"express": {},
"nestjs": {},
"flask": {},
"django": {},
"fastapi": {},
"spring": {},
"spring_boot": {},
"spring_mvc": {},
"spring_data": {},
"spring_security": {},
"gin": {},
"echo": {},
"fiber": {},
"actix": {},
"rocket": {},
"axum": {},
"asp.net": {},
"koa": {},
"hapi": {},
"fastify": {},
}
frontendPathRE = regexp.MustCompile(`(?:^|/)(?:src/)?(?:components|pages|views|app/ui|public)/`)
backendPathRE = regexp.MustCompile(`(?:^|/)(?:src/)?(?:server|api|controllers|services|routes|handlers)/`)
frontendExtRE = regexp.MustCompile(`\.(?:tsx|jsx)$`)
backendPkgRE = regexp.MustCompile(`(?i)(?:^|/|\.)(?:controller|controllers|api|web|rest|resource|resources|model|models|entity|entities|domain|dto|dtos|repository|repositories|dao|persistence|service|services|business|logic|routes|handlers|handler|middleware|middlewares|schemas)(?:/|\.|$)`)
sharedPkgRE = regexp.MustCompile(`(?i)(?:^|/|\.)(?:config|configuration|util|utils|helper|helpers|common|shared|exception|exceptions|constants|enums)(?:/|\.|$)`)
frontendPkgRE = regexp.MustCompile(`(?i)(?:^|/|\.)(?:components|views|pages|ui|widgets|screens|templates|layouts)(?:/|\.|$)`)
)
// Classify sets the Layer property on every node in the slice.
func (c *LayerClassifier) Classify(nodes []*model.CodeNode) {
for _, n := range nodes {
n.Layer = c.classifyOne(n)
}
}
// classifyOne returns the Layer for a single node. Exported as lowercase
// because callers should go through Classify; exposed package-internally so
// tests can exercise individual rules without a slice.
func (c *LayerClassifier) classifyOne(n *model.CodeNode) model.Layer {
// 1. Node kind rules.
if _, ok := frontendKinds[n.Kind]; ok {
return model.LayerFrontend
}
if _, ok := backendKinds[n.Kind]; ok {
return model.LayerBackend
}
if _, ok := infraKinds[n.Kind]; ok {
return model.LayerInfra
}
// 2. Language rules.
if lang, _ := n.Properties["language"].(string); lang != "" {
if _, ok := infraLangs[lang]; ok {
return model.LayerInfra
}
}
// 3. File path rules.
if n.FilePath != "" {
if frontendExtRE.MatchString(n.FilePath) {
return model.LayerFrontend
}
if frontendPathRE.MatchString(n.FilePath) {
return model.LayerFrontend
}
if backendPathRE.MatchString(n.FilePath) {
return model.LayerBackend
}
}
// 4. Framework rules.
if fw, _ := n.Properties["framework"].(string); fw != "" {
if _, ok := frontendFrameworks[fw]; ok {
return model.LayerFrontend
}
if _, ok := backendFrameworks[fw]; ok {
return model.LayerBackend
}
}
// 5. Shared node kinds.
if _, ok := sharedKinds[n.Kind]; ok {
return model.LayerShared
}
// 6. Fallback: package-name / path-pattern heuristics over both file path
// and node ID (the ID often carries package info for JVM-style IDs).
combined := n.FilePath + "|" + n.ID
if frontendPkgRE.MatchString(combined) {
return model.LayerFrontend
}
if backendPkgRE.MatchString(combined) {
return model.LayerBackend
}
if sharedPkgRE.MatchString(combined) {
return model.LayerShared
}
// 7. Java-family final fallback: files under src/main/java or
// src/main/kotlin in standard Spring/Java layouts are virtually always
// backend code.
if strings.HasSuffix(n.FilePath, ".java") ||
strings.HasSuffix(n.FilePath, ".kt") ||
strings.HasSuffix(n.FilePath, ".scala") {
if strings.Contains(n.FilePath, "src/main/java/") ||
strings.Contains(n.FilePath, "src/main/kotlin/") {
return model.LayerBackend
}
}
return model.LayerUnknown
}