wire.py

clew/wire.py

ONE serializer from an R2 dataclass to JSON.

_absent

bool clew.wire._absent(str name, Any value)

True when a value carries nothing a consumer could read.

clew/wire.py:71

Source · 12 lines
def _absent(name: str, value: Any) -> bool:
    """`False` and `0` are NOT absent — `crosses_thread: false` is a measurement.

    @brief Report whether a value is absent rather than merely falsy.
    @return True when the value carries no information.
    @version 2
    """
    if value is None or (isinstance(value, tuple | list | str) and not value):
        return True
    return value == "unknown" and name in _UNKNOWN_MEANS_ABSENT

Activity · 3 steps
lines 78–78◇ if value is None or (isinstance(value, tuple | list | str) and not value)lines 79–79return Truelines 80–80return value == "unknown" and name in _UNKNOWN_MEANS_ABSENT

Flows

_listed

dict[str, Any] clew.wire._listed(list[tuple[str, Any]] pairs)

asdict dict_factory that turns tuple-valued fields into lists.

clew/wire.py:88

Show on the map →

Called by none

Calls none

Source · 8 lines
def _listed(pairs: list[tuple[str, Any]]) -> dict[str, Any]:
    """@brief Convert tuple fields to lists while serializing a dataclass.
    @return JSON-safe dict for one dataclass.
    @version 1
    """
    return {name: list(value) if isinstance(value, tuple) else value for name, value in pairs}

Activity · 1 steps
lines 93–93return {name: list(value) if isinstance(value, tuple) else value for name, value in pairs}

Flows

_prune_rows

Any clew.wire._prune_rows(Any obj)

Prune absent fields from ROWS (dicts inside a list), leaving the envelope whole.

clew/wire.py:139

Source · 15 lines
def _prune_rows(obj: Any) -> Any:
    """Row elision throughout, plus internal-only keys dropped at EVERY level — a `rowid` is
    equally misleading on an envelope and inside a row, so unlike elision it is not row-scoped.

    @brief Apply row-level elision and strip internal-only keys.
    @return Structure with rows pruned, internal ids gone, and the envelope intact.
    @version 3
    """
    if isinstance(obj, dict):
        return {k: _prune_rows(v) for k, v in obj.items() if k not in _INTERNAL_ONLY}
    if isinstance(obj, list):
        return [_pruned_row(v) if isinstance(v, dict) else _prune_rows(v) for v in obj]
    return obj

Activity · 5 steps
lines 147–147◇ if isinstance(obj, dict)lines 148–148return {k: _prune_rows(v) for k, v in obj.items() if k not in _INTERNAL_ONLY}lines 149–149◇ if isinstance(obj, list)lines 150–150return [_pruned_row(v) if isinstance(v, dict) else _prune_rows(v) for v in obj]lines 151–151return obj

Flows

_pruned_row

dict[str, Any] clew.wire._pruned_row(dict[str, Any] row)

One row with its absent fields removed, recursing into nested rows.

clew/wire.py:101

Source · 13 lines
def _pruned_row(row: dict[str, Any]) -> dict[str, Any]:
    """Split out from `_prune_rows` to name the rule: this is what "a row" means on
    the wire.

    @brief Remove absent fields and internal-only keys from a single row.
    @return Pruned row.
    @version 4
    """
    return {
        k: _prune_rows(v) for k, v in row.items() if k not in _INTERNAL_ONLY and not _absent(k, v)
    }

Activity · 1 steps
lines 109–111return { k: _prune_rows(v) for k, v in row.items() if k not in _INTERNAL_ONLY and not _absent(k, v) }

Flows

one

dict[str, Any] | None clew.wire.one(Any obj)

Serialize one R2 dataclass (an ENVELOPE) to its JSON wire form.

clew/wire.py:159

Source · 11 lines
def one(obj: Any) -> dict[str, Any] | None:
    """The object itself is treated as an envelope — every one of its own keys
    survives — while rows nested inside it are pruned.

    @brief Wire form of a single dataclass result.
    @return Serialized dict, or None.
    @version 1
    """
    return None if obj is None else _prune_rows(asdict(obj, dict_factory=_listed))

Activity · 1 steps
lines 167–167return None if obj is None else _prune_rows(asdict(obj, dict_factory=_listed))

Requirements

REQ-DDB-QUERY-001

Flows

prune_absent_keys

dict[str, Any] | None clew.wire.prune_absent_keys(dict[str, Any]|None payload, tuple[str,...] keys)

Drop named ENVELOPE keys that carry nothing, in place.

clew/wire.py:176

Source · 28 lines
def prune_absent_keys(
    payload: dict[str, Any] | None, keys: tuple[str, ...]
) -> dict[str, Any] | None:
    """`one()` deliberately keeps every key of the envelope itself, because a consumer
    reading a known shape should not have to ask whether a field it always saw is
    missing this time. That guarantee is worth keeping for the fields that had it — so
    elision is OPT-IN and NAMED, never blanket.

    Applied to fields added later, whose absence is the common case and whose presence
    is the news: a dossier for a function that opens no critical section should not
    spend a key saying so on every call.

    Uses the same `_absent` rule as row pruning, so `false` and `0` survive — a
    `truncated: false` inside a body excerpt is a measurement, and an added field whose
    value is `0` is an answer.

    @brief Elide the named absent keys from one envelope.
    @return The payload, mutated.
    @version 1
    """
    if payload is None:
        return None
    for key in keys:
        if key in payload and _absent(key, payload[key]):
            del payload[key]
    return payload

Activity · 6 steps
lines 196–196◇ if payload is Nonelines 197–197return Nonelines 198–198◇ for key in keyslines 199–199◇ if key in payload and _absent(key, payload[key])lines 200–200del payload[key]lines 201–201return payload

Requirements

REQ-DDB-QUERY-001

Flows

rows

list[dict[str, Any]] clew.wire.rows(Any items)

Serialize an iterable of R2 dataclasses as ROWS.

clew/wire.py:209

Source · 10 lines
def rows(items: Any) -> list[dict[str, Any]]:
    """These ARE rows — they are about to sit in a list — so each one is pruned.
    Calling `one` per element instead would treat each as an envelope and prune
    nothing, which is the mistake the parity test caught.

    @brief Wire form of a list of dataclass results.
    @return List of row dicts.
    @version 1
    """
    return [_pruned_row(asdict(i, dict_factory=_listed)) for i in items]

1 more lines, not shown

Activity · 1 steps
lines 218–218return [_pruned_row(asdict(i, dict_factory=_listed)) for i in items]

Requirements

REQ-DDB-QUERY-001