Skip to content

Commit 2c4c784

Browse files
authored
Merge pull request DeusData#2497 from DeusData/fix/issue-686-fiber-group-v2
fix(extract): preserve Go router group prefixes
2 parents 79b29fc + 5e596c7 commit 2c4c784

2 files changed

Lines changed: 313 additions & 39 deletions

File tree

‎internal/cbm/extract_calls.c‎

Lines changed: 232 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1763,6 +1763,48 @@ static const char *php_chain_prefix_arg(CBMArena *a, TSNode call_node, const cha
17631763
return NULL;
17641764
}
17651765

1766+
/* Compose route-group prefix segments into one path. parts[] is inner-first;
1767+
* the result is outer-first with exactly one '/' between segments and a
1768+
* leading '/'. Returns NULL when no segment contributes or the result would
1769+
* be oversized (the route then stays un-prefixed). Shared by the Laravel
1770+
* (#952) and Go router-group (#686) prefix walks. */
1771+
static const char *route_prefix_compose(CBMArena *a, const char *const *parts, int part_count) {
1772+
char buf[CBM_SZ_256];
1773+
size_t pos = 0;
1774+
for (int i = part_count - 1; i >= 0; i--) {
1775+
const char *seg = parts[i];
1776+
while (*seg == '/') {
1777+
seg++;
1778+
}
1779+
size_t sl = strlen(seg);
1780+
if (sl == 0) {
1781+
continue;
1782+
}
1783+
if (pos + sl + 2 >= sizeof(buf)) {
1784+
return NULL; /* oversized — leave path un-prefixed */
1785+
}
1786+
buf[pos++] = '/';
1787+
memcpy(buf + pos, seg, sl);
1788+
pos += sl;
1789+
while (pos > 1 && buf[pos - 1] == '/') {
1790+
pos--; /* strip trailing slash per segment */
1791+
}
1792+
}
1793+
buf[pos] = '\0';
1794+
return pos ? cbm_arena_strndup(a, buf, pos) : NULL;
1795+
}
1796+
1797+
/* Prepend a composed group prefix to a route path ("/x" + "/users" ->
1798+
* "/x/users"; a bare "/" route becomes the prefix itself). */
1799+
static const char *route_path_with_prefix(CBMArena *a, const char *prefix, const char *path) {
1800+
const char *rel = path;
1801+
while (*rel == '/') {
1802+
rel++;
1803+
}
1804+
return rel[0] ? cbm_arena_sprintf(a, "%s/%s", prefix, rel)
1805+
: cbm_arena_strndup(a, prefix, strlen(prefix));
1806+
}
1807+
17661808
/* The value of a plain PHP string literal ('...' or "..." with no
17671809
* interpolation or escapes), "" for an empty one; NULL for anything else. */
17681810
static const char *php_plain_string_value(CBMArena *a, TSNode node, const char *source) {
@@ -1817,7 +1859,7 @@ static const char *php_group_array_prefix(CBMArena *a, TSNode group_call, const
18171859
}
18181860

18191861
static const char *php_group_prefix_for_call(CBMArena *a, TSNode node, const char *source) {
1820-
const char *parts[PHP_PREFIX_PARTS_MAX];
1862+
const char *parts[PHP_PREFIX_PARTS_MAX] = {NULL};
18211863
int part_count = 0;
18221864
TSNode cur = ts_node_parent(node);
18231865
for (int depth = 0; depth < PHP_GROUP_WALK_MAX && !ts_node_is_null(cur); depth++) {
@@ -1873,34 +1915,200 @@ static const char *php_group_prefix_for_call(CBMArena *a, TSNode node, const cha
18731915
}
18741916
cur = ts_node_parent(cur);
18751917
}
1876-
if (part_count == 0) {
1918+
return route_prefix_compose(a, parts, part_count);
1919+
}
1920+
1921+
/* #686: compose Go router-group prefixes (Fiber, Gin, Echo: `g := app.Group("/admin")`
1922+
* then `g.Post("/x")`). Starting at the route call's receiver, follow:
1923+
* - an identifier to the latest binding before the use (`:=`, `=`, `var`)
1924+
* among the preceding statements of the enclosing blocks of the same
1925+
* function (a re-bound variable uses the binding in effect at the call),
1926+
* - a `recv.Group("<literal>", ...)` call to its literal prefix and receiver,
1927+
* until the root router (`fiber.New()`, `gin.Default()`, a parameter ...).
1928+
* A non-literal group prefix returns NULL: the route stays un-prefixed rather
1929+
* than carrying a guessed path. Groups passed across functions are not
1930+
* followed (parameters are roots). */
1931+
enum { GO_GROUP_HOPS_MAX = 32 };
1932+
1933+
static const char *go_string_literal_value(CBMArena *a, TSNode n, const char *source) {
1934+
const char *k = ts_node_type(n);
1935+
if (strcmp(k, "interpreted_string_literal") != 0 && strcmp(k, "raw_string_literal") != 0) {
18771936
return NULL;
18781937
}
1879-
/* parts[] is inner-first; compose outer-first. Ensure exactly one '/'
1880-
* between segments and a leading '/'. */
1881-
char buf[CBM_SZ_256];
1882-
size_t pos = 0;
1883-
for (int i = part_count - 1; i >= 0; i--) {
1884-
const char *seg = parts[i];
1885-
while (*seg == '/') {
1886-
seg++;
1938+
char *t = cbm_node_text(a, n, source);
1939+
size_t len = t ? strlen(t) : 0;
1940+
if (len < 2) {
1941+
return NULL;
1942+
}
1943+
t[len - 1] = '\0';
1944+
return t + 1;
1945+
}
1946+
1947+
/* 1 = `recv.Group("<lit>")` (receiver + literal written out), 0 = not a Group
1948+
* call, -1 = a Group call whose prefix is not a string literal. */
1949+
static int go_group_call(CBMArena *a, TSNode expr, const char *source, TSNode *recv_out,
1950+
const char **prefix_out) {
1951+
if (strcmp(ts_node_type(expr), "call_expression") != 0) {
1952+
return 0;
1953+
}
1954+
TSNode fn = ts_node_child_by_field_name(expr, TS_FIELD("function"));
1955+
if (ts_node_is_null(fn) || strcmp(ts_node_type(fn), "selector_expression") != 0) {
1956+
return 0;
1957+
}
1958+
TSNode field = ts_node_child_by_field_name(fn, TS_FIELD("field"));
1959+
const char *ft = ts_node_is_null(field) ? NULL : cbm_node_text(a, field, source);
1960+
if (!ft || strcmp(ft, "Group") != 0) {
1961+
return 0;
1962+
}
1963+
*recv_out = ts_node_child_by_field_name(fn, TS_FIELD("operand"));
1964+
TSNode args = ts_node_child_by_field_name(expr, TS_FIELD("arguments"));
1965+
*prefix_out = NULL;
1966+
if (!ts_node_is_null(args) && ts_node_named_child_count(args) > 0) {
1967+
*prefix_out = go_string_literal_value(a, ts_node_named_child(args, 0), source);
1968+
}
1969+
return *prefix_out ? 1 : -1;
1970+
}
1971+
1972+
/* Value bound to `name` by one var_spec (`var a, b = x, y`), or a null node. */
1973+
static TSNode go_var_spec_binding(CBMArena *a, TSNode spec, const char *name, const char *source) {
1974+
TSNode none = {0};
1975+
TSNode value = ts_node_child_by_field_name(spec, TS_FIELD("value"));
1976+
if (ts_node_is_null(value)) {
1977+
return none;
1978+
}
1979+
uint32_t idx = 0;
1980+
uint32_t nc = ts_node_named_child_count(spec);
1981+
for (uint32_t i = 0; i < nc; i++) {
1982+
TSNode c = ts_node_named_child(spec, i);
1983+
if (strcmp(ts_node_type(c), "identifier") != 0) {
1984+
continue;
18871985
}
1888-
size_t sl = strlen(seg);
1889-
if (sl == 0) {
1986+
const char *t = cbm_node_text(a, c, source);
1987+
if (t && strcmp(t, name) == 0) {
1988+
return idx < ts_node_named_child_count(value) ? ts_node_named_child(value, idx) : none;
1989+
}
1990+
idx++;
1991+
}
1992+
return none;
1993+
}
1994+
1995+
/* Value bound to `name` by statement `stmt`, or a null node. */
1996+
static TSNode go_stmt_binding(CBMArena *a, TSNode stmt, const char *name, const char *source) {
1997+
TSNode none = {0};
1998+
const char *k = ts_node_type(stmt);
1999+
if (strcmp(k, "var_declaration") == 0 || strcmp(k, "var_spec_list") == 0) {
2000+
for (uint32_t i = ts_node_named_child_count(stmt); i > 0; i--) {
2001+
TSNode c = ts_node_named_child(stmt, i - 1);
2002+
TSNode v = strcmp(ts_node_type(c), "var_spec") == 0
2003+
? go_var_spec_binding(a, c, name, source)
2004+
: go_stmt_binding(a, c, name, source);
2005+
if (!ts_node_is_null(v)) {
2006+
return v;
2007+
}
2008+
}
2009+
return none;
2010+
}
2011+
if (strcmp(k, "short_var_declaration") != 0 && strcmp(k, "assignment_statement") != 0) {
2012+
return none;
2013+
}
2014+
TSNode left = ts_node_child_by_field_name(stmt, TS_FIELD("left"));
2015+
TSNode right = ts_node_child_by_field_name(stmt, TS_FIELD("right"));
2016+
if (ts_node_is_null(left) || ts_node_is_null(right)) {
2017+
return none;
2018+
}
2019+
uint32_t nl = ts_node_named_child_count(left);
2020+
for (uint32_t i = 0; i < nl; i++) {
2021+
const char *t = cbm_node_text(a, ts_node_named_child(left, i), source);
2022+
if (t && strcmp(t, name) == 0) {
2023+
return i < ts_node_named_child_count(right) ? ts_node_named_child(right, i) : none;
2024+
}
2025+
}
2026+
return none;
2027+
}
2028+
2029+
static bool go_is_function_boundary(const char *k) {
2030+
return strcmp(k, "function_declaration") == 0 || strcmp(k, "method_declaration") == 0 ||
2031+
strcmp(k, "func_literal") == 0 || strcmp(k, "source_file") == 0;
2032+
}
2033+
2034+
/* Latest binding of `name` among the statements preceding `use` in its
2035+
* enclosing blocks, innermost block first; a null node when none. */
2036+
static TSNode go_find_binding(CBMArena *a, TSNode use, const char *name, const char *source) {
2037+
TSNode child = use;
2038+
TSNode cur = ts_node_parent(use);
2039+
while (!ts_node_is_null(cur) && !go_is_function_boundary(ts_node_type(cur))) {
2040+
const char *k = ts_node_type(cur);
2041+
if (strcmp(k, "block") == 0 || strcmp(k, "statement_list") == 0) {
2042+
for (TSNode s = ts_node_prev_named_sibling(child); !ts_node_is_null(s);
2043+
s = ts_node_prev_named_sibling(s)) {
2044+
TSNode v = go_stmt_binding(a, s, name, source);
2045+
if (!ts_node_is_null(v)) {
2046+
return v;
2047+
}
2048+
}
2049+
}
2050+
child = cur;
2051+
cur = ts_node_parent(cur);
2052+
}
2053+
TSNode none = {0};
2054+
return none;
2055+
}
2056+
2057+
static const char *go_group_prefix_for_call(CBMArena *a, TSNode call, const char *source) {
2058+
TSNode fn = ts_node_child_by_field_name(call, TS_FIELD("function"));
2059+
if (ts_node_is_null(fn) || strcmp(ts_node_type(fn), "selector_expression") != 0) {
2060+
return NULL;
2061+
}
2062+
const char *parts[GO_GROUP_HOPS_MAX] = {NULL};
2063+
int part_count = 0;
2064+
TSNode recv = ts_node_child_by_field_name(fn, TS_FIELD("operand"));
2065+
for (int hop = 0; !ts_node_is_null(recv); hop++) {
2066+
if (hop >= GO_GROUP_HOPS_MAX) {
2067+
return NULL; /* chain too deep to trust: never a partial prefix */
2068+
}
2069+
const char *k = ts_node_type(recv);
2070+
if (strcmp(k, "parenthesized_expression") == 0 && ts_node_named_child_count(recv) > 0) {
2071+
recv = ts_node_named_child(recv, 0);
18902072
continue;
18912073
}
1892-
if (pos + sl + 2 >= sizeof(buf)) {
1893-
return NULL; /* oversized — leave path un-prefixed */
2074+
if (strcmp(k, "identifier") == 0) {
2075+
const char *name = cbm_node_text(a, recv, source);
2076+
recv = name ? go_find_binding(a, recv, name, source) : (TSNode){0};
2077+
continue; /* unbound (parameter, package var): root reached */
18942078
}
1895-
buf[pos++] = '/';
1896-
memcpy(buf + pos, seg, sl);
1897-
pos += sl;
1898-
while (pos > 1 && buf[pos - 1] == '/') {
1899-
pos--; /* strip trailing slash per segment */
2079+
TSNode inner = {0};
2080+
const char *prefix = NULL;
2081+
int g = go_group_call(a, recv, source, &inner, &prefix);
2082+
if (g < 0) {
2083+
return NULL; /* dynamic prefix: do not guess */
2084+
}
2085+
if (g == 0) {
2086+
break; /* root router constructor or other expression */
19002087
}
2088+
parts[part_count++] = prefix; /* inner-first */
2089+
recv = inner;
19012090
}
1902-
buf[pos] = '\0';
1903-
return pos ? cbm_arena_strndup(a, buf, pos) : NULL;
2091+
return route_prefix_compose(a, parts, part_count);
2092+
}
2093+
2094+
/* Routes registered inside router groups must carry the composed path — the
2095+
* resolve passes only see the flat CBMCall, so the enclosing group chain can
2096+
* only be read here where the AST still exists. Laravel `prefix()->group()`
2097+
* closures (#952) and Go `x.Group("/p")` receivers (#686). Returns the route
2098+
* path to store (unchanged when no group prefix applies). */
2099+
static const char *route_path_with_group_prefix(CBMExtractCtx *ctx, TSNode node,
2100+
const char *callee_name, const char *path) {
2101+
if (!path || path[0] != '/' || !callee_name ||
2102+
cbm_service_pattern_route_method(callee_name) == NULL) {
2103+
return path;
2104+
}
2105+
const char *gp = NULL;
2106+
if (ctx->language == CBM_LANG_PHP) {
2107+
gp = php_group_prefix_for_call(ctx->arena, node, ctx->source);
2108+
} else if (ctx->language == CBM_LANG_GO) {
2109+
gp = go_group_prefix_for_call(ctx->arena, node, ctx->source);
2110+
}
2111+
return (gp && gp[0]) ? route_path_with_prefix(ctx->arena, gp, path) : path;
19042112
}
19052113

19062114
/* A Laravel route registrar receiver (#1146): the Route facade — also at the
@@ -4210,24 +4418,9 @@ CBMInvocationDescriptor handle_calls(CBMExtractCtx *ctx, TSNode node, const CBML
42104418
call.first_string_arg = laravel_uri;
42114419
}
42124420
}
4213-
/* #952: routes registered inside Laravel `prefix()->group()`
4214-
* closures must carry the composed path — the resolve passes
4215-
* only see the flat CBMCall, so the enclosing chain can only
4216-
* be read here where the AST still exists. */
4217-
if (ctx->language == CBM_LANG_PHP && call.first_string_arg &&
4218-
call.first_string_arg[0] == '/' && call.callee_name &&
4219-
cbm_service_pattern_route_method(call.callee_name) != NULL) {
4220-
const char *gp = php_group_prefix_for_call(ctx->arena, node, ctx->source);
4221-
if (gp && gp[0]) {
4222-
const char *rel = call.first_string_arg;
4223-
while (*rel == '/') {
4224-
rel++;
4225-
}
4226-
call.first_string_arg =
4227-
rel[0] ? cbm_arena_sprintf(ctx->arena, "%s/%s", gp, rel)
4228-
: cbm_arena_strndup(ctx->arena, gp, strlen(gp));
4229-
}
4230-
}
4421+
/* Laravel prefix()->group() (#952) and Go x.Group("/p") (#686). */
4422+
call.first_string_arg = route_path_with_group_prefix(ctx, node, call.callee_name,
4423+
call.first_string_arg);
42314424
/* A slashless registration's string action would only be
42324425
* guessed at (#1146): mint its Route without a handler. */
42334426
if (call.first_string_arg && call.first_string_arg[0] == '/' &&

0 commit comments

Comments
 (0)