MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / extract_grpc_service_method

Function extract_grpc_service_method

src/pipeline/pass_parallel.c:2556–2616  ·  view source on GitHub ↗

Extract gRPC service and method from a callee name. * Handles patterns like: pb.NewFooServiceClient(conn).GetBar → Foo/GetBar * Also: FooServiceGrpc.newBlockingStub(ch).getBar → FooService/getBar */

Source from the content-addressed store, hash-verified

2554 * Handles patterns like: pb.NewFooServiceClient(conn).GetBar → Foo/GetBar
2555 * Also: FooServiceGrpc.newBlockingStub(ch).getBar → FooService/getBar */
2556bool extract_grpc_service_method(const char *callee, char *service, size_t srv_sz, char *method,
2557 size_t meth_sz) {
2558 service[0] = '\0';
2559 method[0] = '\0';
2560 if (!callee) {
2561 return false;
2562 }
2563 /* Find last dot to split service.Method */
2564 const char *last_dot = strrchr(callee, '.');
2565 if (!last_dot || !last_dot[SKIP_ONE]) {
2566 return false;
2567 }
2568 snprintf(method, meth_sz, "%s", last_dot + SKIP_ONE);
2569
2570 /* Extract service name: everything before the last dot, stripped of prefixes/suffixes */
2571 size_t prefix_len = (size_t)(last_dot - callee);
2572 char raw[CBM_SZ_256];
2573 if (prefix_len >= sizeof(raw)) {
2574 prefix_len = sizeof(raw) - SKIP_ONE;
2575 }
2576 memcpy(raw, callee, prefix_len);
2577 raw[prefix_len] = '\0';
2578
2579 /* Strip common prefixes: pb.New, New, pb. */
2580 const char *s = raw;
2581 if (strncmp(s, "pb.New", CBM_SZ_6) == 0) {
2582 s += CBM_SZ_6;
2583 } else if (strncmp(s, "pb.", CBM_SZ_3) == 0 || strncmp(s, "New", CBM_SZ_3) == 0) {
2584 s += CBM_SZ_3;
2585 }
2586
2587 /* Strip the generated-stub/client suffix, preserving the canonical
2588 * proto-declared service name. The proto service is `<X>Service`; the
2589 * generated client is `<X>ServiceClient` / `<X>ServiceGrpc`, so we strip
2590 * only the trailing stub/client token (Client/Stub/Grpc/…), NOT "Service"
2591 * itself — stripping "ServiceClient" yielded `<X>` and broke cross-repo
2592 * matching against the `<X>Service` declared name (#294). Longest tokens
2593 * first so e.g. BlockingStub wins over Stub.
2594 *
2595 * A match also serves as the gRPC stub-type signal: we ONLY emit a Route
2596 * when a recognized suffix is actually present. Without this gate the
2597 * fallback turned ordinary receiver vars (`_provider.GetGroup`,
2598 * `_builder.AddSomeService`) into phantom `__grpc__provider/...` Routes
2599 * that correspond to no .proto anywhere (#294). */
2600 snprintf(service, srv_sz, "%s", s);
2601 size_t slen = strlen(service);
2602 static const char *const suffixes[] = {"BlockingStub", "FutureStub", "AsyncStub",
2603 "AsyncClient", "Servicer", "Client",
2604 "Stub", "Grpc", NULL};
2605 bool stripped = false;
2606 for (const char *const *sfx = suffixes; *sfx; sfx++) {
2607 size_t flen = strlen(*sfx);
2608 if (slen > flen && strcmp(service + slen - flen, *sfx) == 0) {
2609 service[slen - flen] = '\0';
2610 stripped = true;
2611 break;
2612 }
2613 }

Callers 2

emit_grpc_edgeFunction · 0.85
test_parallel.cFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected