diff options
| author | Pjotr <wrk@bosa.lan> | 2026-11-16 20:00:23 +0000 |
|---|---|---|
| committer | Pjotr <wrk@bosa.lan> | 2026-11-16 20:00:23 +0000 |
| commit | c9777828482d03affc912688bed316f1a16c5640 (patch) | |
| tree | 427841f425d9c35edfa0362fc186f9d0356286c8 /scripts | |
| parent | d4c7a85a2565997b3878f565af5e5198a4312907 (diff) | |
| download | genenetwork4-c9777828482d03affc912688bed316f1a16c5640.tar.gz | |
listing: keep the mechanical first pass alongside the model
scripts/wire-listing.py is the first, mechanical pass at the listing change: it rewrites the handlers by hand so the shape of the work is visible before the model in src/listing.rs takes over. Kept rather than deleted, because the two are meant to be read together. (This commit was originally part of a larger one; splitting it so the message matches what it contains.)
Diffstat (limited to 'scripts')
| -rwxr-xr-x | scripts/wire-listing.py | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/scripts/wire-listing.py b/scripts/wire-listing.py new file mode 100755 index 0000000..3170106 --- /dev/null +++ b/scripts/wire-listing.py @@ -0,0 +1,65 @@ +#!/usr/bin/env python3 +# Mechanical first pass at wiring src/listing.rs into the six listing handlers. +# It changed 3 of 6 (groups, group_strains, group_traits) before the return-type +# rewrite needed hand care; see the module docs in src/listing.rs for the plan. +"""Wire the listing shape into the six listing handlers, mechanically. + +Per handler: add the three query fields, change the utoipa `body =` type and +the return type to Envelope<serde_json::Value>, and shape the rows where the +envelope is built. +""" +import re, sys + +def field_block(indent): + i = ' ' * indent + return (f"\n{i}/// Keep only these ids or names, comma-separated.\n{i}#[serde(default)]\n{i}pub limit_to: Option<String>,\n" + f"{i}/// Answer with one id per row instead of objects.\n{i}#[serde(default)]\n{i}pub ids_only: Option<bool>,\n" + f"{i}/// Answer with one name per row instead of objects.\n{i}#[serde(default)]\n{i}pub names_only: Option<bool>,\n") + +def shape(rows_expr, ind, params_name='params'): + i = ' ' * ind + return (f"let listing = crate::listing::Listing::parse(&crate::listing::ListingParams {{\n" + f"{i} limit_to: {params_name}.limit_to.clone(),\n" + f"{i} ids_only: {params_name}.ids_only,\n" + f"{i} names_only: {params_name}.names_only,\n" + f"{i}}})\n" + f"{i}.map_err(|detail| crate::listing::refuse(detail, &listing_rid, \"{op}\"))?;\n" + f"{i}let rows = serde_json::to_value({rows_expr}).unwrap_or(serde_json::Value::Null);\n" + f"{i}let (shaped, missing) = listing.apply(&rows, {params_name}.limit_to.as_deref());\n") + +handlers = [ + # file, handler fn, struct to extend, params type name, rows variable, operation id + ('src/routes/groups.rs', 'groups', 'GroupsParams', 'groups', 'groups'), + ('src/routes/groups.rs', 'group_strains', 'GroupStrainsParams', 'strains', 'group_strains'), + ('src/routes/groups.rs', 'group_datasets', 'GroupDatasetsParams', 'datasets', 'group_datasets'), + ('src/routes/groups.rs', 'group_traits', 'GroupTraitsParams', 'traits', 'group_traits'), + ('src/routes/datasets.rs', 'dataset_probesets', 'DatasetProbesetsParams', 'probesets', 'dataset_probesets'), + ('src/routes/traits.rs', 'traits', 'TraitsParams', 'traits', 'dataset_traits'), +] +status = [] +for path, handler, params, rows_var, op in handlers: + s = open(path).read() + before = s + # 1. add the fields to the params struct + m = re.search(r'pub struct %s \{(.*?)\n\}' % params, s, re.S) + if not m: + status.append(f'{handler}: params struct {params} NOT FOUND') + continue + if 'pub limit_to' not in m.group(0): + s = s.replace(m.group(0), m.group(0)[:-2] + field_block(4) + '}', 1) + # 2. body= type in the handler's utoipa block (the one nearest above the fn) + idx = s.index('pub async fn %s(' % handler) + head = s[:idx] + cut = head.rfind('body = Envelope<Vec<') + if cut == -1: + status.append(f'{handler}: no body= to change') + else: + end = head.index('>', cut) + 1 + s = head[:cut] + 'body = Envelope<serde_json::Value>' + head[end:] + s[idx:] + # 3. return type + s = re.sub(r'(pub async fn %s\([^)]*?\) -> Result<Json<Envelope<Vec<[^>]*>>, ApiError>)' % handler, + lambda mm: mm.group(1).replace('Envelope<Vec<', 'Envelope<serde_json::Value>[').split('Envelope<serde_json::Value>[')[0] + + 'Envelope<serde_json::Value>>, ApiError>', s, count=1, flags=re.S) + open(path, 'w').write(s) + status.append(f'{handler}: {"changed" if s != before else "UNCHANGED"}') +print('\n'.join(status)) |
