| 2362 | static const char *json_extract_prop(const char *json, const char *key, char *buf, size_t buf_sz); |
| 2363 | |
| 2364 | static const char *node_prop(const cbm_node_t *n, const char *prop, cbm_store_t *store) { |
| 2365 | if (!n || !prop) { |
| 2366 | return ""; |
| 2367 | } |
| 2368 | const char *str = node_string_field(n, prop); |
| 2369 | if (str && str[0]) { |
| 2370 | return str; |
| 2371 | } |
| 2372 | /* Computed and JSON-derived values live in rotating thread-local buffers: |
| 2373 | * a single row (or an ORDER-BY comparison) reads several of these before any |
| 2374 | * of them is copied out, so returning one shared static buffer would alias |
| 2375 | * every column to the last value read. Mirrors edge_prop's rotation. */ |
| 2376 | static _Thread_local char bufs[CYP_BUF_8][CBM_SZ_512]; |
| 2377 | static _Thread_local int buf_idx = 0; |
| 2378 | char *out = bufs[buf_idx]; |
| 2379 | buf_idx = (buf_idx + SKIP_ONE) % CYP_BUF_8; |
| 2380 | |
| 2381 | if (strcmp(prop, "start_line") == 0) { |
| 2382 | snprintf(out, CBM_SZ_512, "%d", n->start_line); |
| 2383 | return out; |
| 2384 | } |
| 2385 | if (strcmp(prop, "end_line") == 0) { |
| 2386 | snprintf(out, CBM_SZ_512, "%d", n->end_line); |
| 2387 | return out; |
| 2388 | } |
| 2389 | /* Virtual computed properties: in_degree/out_degree via CALLS edges. |
| 2390 | * Enables Cypher dead-code detection: WHERE n.in_degree = '0'. */ |
| 2391 | if (store && (strcmp(prop, "in_degree") == 0 || strcmp(prop, "out_degree") == 0)) { |
| 2392 | int in_deg = 0; |
| 2393 | int out_deg = 0; |
| 2394 | cbm_store_node_degree(store, n->id, &in_deg, &out_deg); |
| 2395 | int val = (strcmp(prop, "in_degree") == 0) ? in_deg : out_deg; |
| 2396 | snprintf(out, CBM_SZ_512, "%d", val); |
| 2397 | return out; |
| 2398 | } |
| 2399 | /* Fall back to any value stored in the node's properties JSON — exposes the |
| 2400 | * extraction metrics (complexity, cognitive, loop_count, loop_depth, |
| 2401 | * transitive_loop_depth, recursive) and any other persisted property to |
| 2402 | * WHERE/RETURN, e.g. WHERE n.loop_depth >= 2. */ |
| 2403 | if (n->properties_json && n->properties_json[0] == '{') { |
| 2404 | const char *v = json_extract_prop(n->properties_json, prop, out, CBM_SZ_512); |
| 2405 | if (v && v[0]) { |
| 2406 | return v; |
| 2407 | } |
| 2408 | } |
| 2409 | return ""; |
| 2410 | } |
| 2411 | |
| 2412 | /* Extract a string value from JSON properties_json by key. |
| 2413 | * Writes result to buf (up to buf_sz). Returns buf if found, "" otherwise. |
no test coverage detected