Replace inter-service _handlers dicts with declarative sx defquery/defaction
The inter-service data layer (fetch_data/call_action) was the least structured part of the codebase — Python _handlers dicts with ad-hoc param extraction scattered across 16 route files. This replaces them with declarative .sx query/action definitions that make the entire inter-service protocol self-describing and greppable. Infrastructure: - defquery/defaction special forms in the sx evaluator - Query/action registry with load, lookup, and schema introspection - Query executor using async_eval with I/O primitives - Blueprint factories (create_data_blueprint/create_action_blueprint) with sx-first dispatch and Python fallback - /internal/schema endpoint on every service - parse-datetime and split-ids primitives for type coercion Service extractions: - LikesService (toggle, is_liked, liked_slugs, liked_ids) - PageConfigService (ensure, get_by_container, get_by_id, get_batch, update) - RelationsService (wraps module-level functions) - AccountDataService (user_by_email, newsletters) - CartItemsService, MarketDataService (raw SQLAlchemy lookups) 50 of 54 handlers converted to sx, 4 Python fallbacks remain (ghost-sync/push-member, clear-cart-for-order, create-order). Net: -1,383 lines Python, +251 lines modified. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -553,6 +553,75 @@ def _sf_defhandler(expr: list, env: dict) -> HandlerDef:
|
||||
return handler
|
||||
|
||||
|
||||
def _parse_key_params(params_expr: list) -> list[str]:
|
||||
"""Parse ``(&key param1 param2 ...)`` into a list of param name strings."""
|
||||
params: list[str] = []
|
||||
in_key = False
|
||||
for p in params_expr:
|
||||
if isinstance(p, Symbol):
|
||||
if p.name == "&key":
|
||||
in_key = True
|
||||
continue
|
||||
if in_key:
|
||||
params.append(p.name)
|
||||
elif isinstance(p, str):
|
||||
params.append(p)
|
||||
return params
|
||||
|
||||
|
||||
def _sf_defquery(expr: list, env: dict):
|
||||
"""``(defquery name (&key param...) "docstring" body)``"""
|
||||
from .types import QueryDef
|
||||
if len(expr) < 4:
|
||||
raise EvalError("defquery requires name, params, and body")
|
||||
name_sym = expr[1]
|
||||
if not isinstance(name_sym, Symbol):
|
||||
raise EvalError(f"defquery name must be symbol, got {type(name_sym).__name__}")
|
||||
params_expr = expr[2]
|
||||
if not isinstance(params_expr, list):
|
||||
raise EvalError("defquery params must be a list")
|
||||
params = _parse_key_params(params_expr)
|
||||
# Optional docstring before body
|
||||
if len(expr) >= 5 and isinstance(expr[3], str):
|
||||
doc = expr[3]
|
||||
body = expr[4]
|
||||
else:
|
||||
doc = ""
|
||||
body = expr[3]
|
||||
qdef = QueryDef(
|
||||
name=name_sym.name, params=params, doc=doc,
|
||||
body=body, closure=dict(env),
|
||||
)
|
||||
env[f"query:{name_sym.name}"] = qdef
|
||||
return qdef
|
||||
|
||||
|
||||
def _sf_defaction(expr: list, env: dict):
|
||||
"""``(defaction name (&key param...) "docstring" body)``"""
|
||||
from .types import ActionDef
|
||||
if len(expr) < 4:
|
||||
raise EvalError("defaction requires name, params, and body")
|
||||
name_sym = expr[1]
|
||||
if not isinstance(name_sym, Symbol):
|
||||
raise EvalError(f"defaction name must be symbol, got {type(name_sym).__name__}")
|
||||
params_expr = expr[2]
|
||||
if not isinstance(params_expr, list):
|
||||
raise EvalError("defaction params must be a list")
|
||||
params = _parse_key_params(params_expr)
|
||||
if len(expr) >= 5 and isinstance(expr[3], str):
|
||||
doc = expr[3]
|
||||
body = expr[4]
|
||||
else:
|
||||
doc = ""
|
||||
body = expr[3]
|
||||
adef = ActionDef(
|
||||
name=name_sym.name, params=params, doc=doc,
|
||||
body=body, closure=dict(env),
|
||||
)
|
||||
env[f"action:{name_sym.name}"] = adef
|
||||
return adef
|
||||
|
||||
|
||||
def _sf_set_bang(expr: list, env: dict) -> Any:
|
||||
"""``(set! name value)`` — mutate existing binding."""
|
||||
if len(expr) != 3:
|
||||
@@ -737,6 +806,8 @@ _SPECIAL_FORMS: dict[str, Any] = {
|
||||
"quasiquote": _sf_quasiquote,
|
||||
"defhandler": _sf_defhandler,
|
||||
"defpage": _sf_defpage,
|
||||
"defquery": _sf_defquery,
|
||||
"defaction": _sf_defaction,
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user