MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / extract

Function extract

src/resolution/frameworks/go.ts:103–167  ·  view source on GitHub ↗
(filePath, content)

Source from the content-addressed store, hash-verified

101 },
102
103 extract(filePath, content) {
104 if (!filePath.endsWith('.go')) return { nodes: [], references: [] };
105 const nodes: Node[] = [];
106 const references: UnresolvedRef[] = [];
107 const now = Date.now();
108 const safe = stripCommentsForRegex(content, 'go');
109
110 // <anyVar>.METHOD("/path", handler) — Gin (GET/POST/...), Chi (Get/Post/...),
111 // net/http (HandleFunc/Handle). The receiver is ANY identifier, not just
112 // router|r|mux|app|e: real apps route on GROUP vars (`v1.GET`, `PublicGroup.GET`,
113 // `userRouter.POST`), which the fixed name list missed (gin-vue-admin: 4 routes
114 // for 625 files). The verb + string-path + handler-arg gates keep it route-specific.
115 const routeRegex = /\b\w+\.(GET|POST|PUT|PATCH|DELETE|OPTIONS|HEAD|Get|Post|Put|Patch|Delete|Handle|HandleFunc)\s*\(\s*"([^"]+)"\s*,\s*([^)]+)\)/g;
116 let match: RegExpExecArray | null;
117 while ((match = routeRegex.exec(safe)) !== null) {
118 const [, rawMethod, routePath, handlerExpr] = match;
119
120 // The first argument must be URL-shaped, or this is just a method that
121 // happens to share a verb name — `cache.Put("key", val)`, `store.Get(...)`,
122 // `bus.Handle("user.created", h)` all polluted the route index (#1259).
123 // Real registrations use "/path" (every router), or net/http's Go 1.22
124 // "METHOD /path" patterns on Handle/HandleFunc.
125 const methodPrefix = matchGo122MethodPattern(routePath!, rawMethod!);
126 if (!routePath!.startsWith('/') && !methodPrefix) continue;
127
128 const line = safe.slice(0, match.index).split('\n').length;
129 // "GET /users/{id}" -> method GET, path /users/{id}
130 const path = methodPrefix ? routePath!.slice(methodPrefix.length).trimStart() : routePath!;
131 const method = methodPrefix
132 ? methodPrefix
133 : rawMethod === 'Handle' || rawMethod === 'HandleFunc'
134 ? 'ANY'
135 : rawMethod!.toUpperCase();
136
137 const routeNode: Node = {
138 id: `route:${filePath}:${line}:${method}:${path}`,
139 kind: 'route',
140 name: `${method} ${path}`,
141 qualifiedName: `${filePath}::route:${path}`,
142 filePath,
143 startLine: line,
144 endLine: line,
145 startColumn: 0,
146 endColumn: match[0].length,
147 language: 'go',
148 updatedAt: now,
149 };
150 nodes.push(routeNode);
151
152 const handlerName = extractGoTailIdent(handlerExpr!);
153 if (handlerName) {
154 references.push({
155 fromNodeId: routeNode.id,
156 referenceName: handlerName,
157 referenceKind: 'references',
158 line,
159 column: 0,
160 filePath,

Callers

nothing calls this directly

Calls 5

stripCommentsForRegexFunction · 0.90
matchGo122MethodPatternFunction · 0.85
extractGoTailIdentFunction · 0.85
pushMethod · 0.80
execMethod · 0.65

Tested by

no test coverage detected