81 — Writing a lens
GuideRendered from 81-lens-authoring-guide.md in the public repository. Edits belong there.
This page lists the steps for adding a lens and states how the workbench chooses one.
A lens converts a datum's stored value into the value the workbench shows, and converts what an operator types back into the stored value. It does not change how the value is stored. Example: the fiat_cents lens shows a stored 450 as $4.50, and stores $4.50 typed into a price cell as 450.
The contract
Lens in micyte/state_machine/lens/base.py is the contract every lens meets:
| Member | Direction | Returns |
|---|---|---|
lens_id | — | Unique id, lower case. |
decode(canonical_value) | stored → shown | The display value. |
encode(display_value) | typed → stored | The canonical value. |
validate_display(display_value) | typed | A tuple of issue codes; () means valid. |
A lens holds no state and does no I/O.
A lens is a declaration
There is no class per lens. Every lens is an entry in micyte/state_machine/lens/lenses.json:
{
"lens_id": "fiat_cents",
"label": "Price (cents)",
"description": "Renders a fiat magnitude as currency: 450 shows as $4.50.",
"presents": "number",
"decode": ["text", "cents_to_currency"],
"encode": ["text", "currency_to_cents"],
"validate": {"steps": ["text"], "checks": [
{"check": "required", "issue": "price_required"},
{"check": "currency", "issue": "price_invalid"}
]},
"bindings": {"families": ["fiat_babelette"], "overlays": ["fiat_babelette"], "value_kinds": ["fiat_cents"]}
}decodeandencodeare lists of steps, run in order. An empty list is the passthrough: the value comes back unchanged, whatever its type.validateruns itsstepson the typed value, then itschecksin order. The first check that fails gives the one issue code returned.bindingssay which datums the registry chooses the lens for. One binding belongs to one lens.presentssays what the lens's display reads as:number(a value: a count, a price, a node address),nominal(a name or words), oras_read, where the value decides (42reads as a number,lawn_careas a name). Omitted, it isas_read. The shell draws a number in its own container and figures (.mc-number), so a value is never taken for a name that looks like one. Numbers are not told apart from each other, only from names.always_on: truekeeps a lens out of the per-instance toggles.fallback: truemarks the lens chosen when nothing binds; exactly one lens carries it.
DeclaredLens (declared.py) runs a declaration. lenses_of refuses, naming the problem: a step or check outside the closed set, a parameter a step lacks or does not take, a lens declared twice, a binding two lenses claim, or a set without exactly one fallback.
The closed set
micyte/core/datum_ops/lens_transforms.py holds every step a declaration can name.
| Step | Does |
|---|---|
text | None → ""; anything else → str of it, stripped. Begins every pipeline but the passthrough's. |
collapse_spaces | Every run of whitespace → one space. |
upper, lower | Case. |
["default", "<shown>"] | <shown> in place of an empty value. |
cents_to_currency | 450 → $4.50; a value that is not whole cents is kept. |
currency_to_cents | $4.50 → 450, through the write path's parse_cents; a value that is not a price is kept. |
bits_to_ascii | A bit string → its text, 8 bits a character; · for a byte outside printable ASCII; <N> bits when nothing decodes. |
words | lawn_care → Lawn Care: split at underscores, each word's first letter raised. |
| Check | Passes when |
|---|---|
required | The value is not empty. |
ascii | Every character is ASCII. |
email_shape | An @ that neither starts nor ends the value. |
{"check": "excludes", "value": "<word>"} | <word> (lower case) appears nowhere, in any case. |
digit_groups | ASCII digits in groups joined by single hyphens. |
currency | parse_cents accepts it, in ASCII digits. |
bits | Only 0 and 1. |
Digits are ASCII digits throughout. A node address or a price in another script's digits is not one this system stores.
Declared lenses
lens_id | Behaviour |
|---|---|
identity | Returns the value unchanged. The fallback; always on. |
trimmed_string | Strips surrounding whitespace. |
samras_title | Collapses spaces and upper-cases a title on save; refuses a title that is not ASCII. |
email_address | Lower-cases an email address; checks the @. |
secret_reference | Shows a secret reference (not_configured when empty), never a secret; refuses one containing "password". |
numeric_hyphen | Checks a SAMRAS/HOPS node address is digit groups: a-b-c. |
fiat_cents | Integer cents ↔ $d.cc. Symmetric. |
binary_text | Bit string → ASCII text. Display only: encode keeps the bit string. |
label | A snake_case label as words. Always on; the channel viewer's node labels. |
The browser runs the same declarations
micyte/ui/static/v2_portal_lens.js implements the same steps and carries lenses.json in a generated block, so MyCiteLens.decode(id, v), .encode(id, v), .validate(id, v) and .readsAs(id, shown) answer as Python does. .html(id, v) is the display as escaped HTML, a number wrapped in .mc-number. The datum grid marks a value cell that reads as a number the same way, in every mode but raw. The shell loads it before every renderer; micyte.com loads it before the channel viewer (js/lens.js).
How a lens is chosen
DatumLensRegistry.resolve in micyte/state_machine/lens/registry.py checks, in order:
flag_lens_id— a lens named by a hyphae flag (matched_on="hyphae_flag"). No caller passes it; see Not built.recognized_familyin a lens'sbindings.families— for examplefiat_babelette→fiat_cents,samras→numeric_hyphen,title_babelette→binary_text.overlay_kindin a lens'sbindings.overlays.primary_value_kindin a lens'sbindings.value_kinds— for examplefiat_cents,binary_string,literal_text.- Otherwise the lens declared
"fallback": true,identity(matched_on="fallback").
Keys are lower-cased before lookup.
If the instance has disabled the chosen lens, resolve returns identity with matched_on="lens_disabled". A lens declared "always_on": true (identity, label) is never disabled and is not offered as a toggle. The disabled set is kept per instance by fnd_app/instances/_shared/runtime/portal_lens_runtime.py, read with GET /portal/api/lenses and changed with POST /portal/api/lenses/toggle.
The workbench (micyte/tools/workbench_ui/service.py) resolves one lens for each row and one for each reference binding in a record row. A binding's lens is chosen from its own expected_value_kind, so a price inside a record row is shown with fiat_cents even when the row's first value is a node address. Each row carries resolved_lens and resolved_lens_match.
Steps
- Declare it. Add an entry to
lenses.json:lens_id,label,description, and its steps. If it needs a step the closed set lacks, add the step tolens_transforms.pyAND tov2_portal_lens.js, with vectors that hold the two to one answer. - Decide whether it is symmetric. A symmetric lens stores what
encodereturns (fiat_cents,email_address). A display-only lens leaves the stored value as typed (binary_text). For a symmetric lens,decode(encode(d))must returndapart from deliberate normalization. - Bind it in its own
bindings: by family when every datum of a recognized family needs it; by value kind when it follows the value's shape. Nothing else changes: the registry, the toggle catalog andlens_by_idread the declarations. - Regenerate the browser's copy:
python -m micyte.ui.lenses --write. The public suite fails while the script's block and the file differ. - Return issue codes, not prose.
StagingArea.stage_with_lens(micyte/state_machine/nimm/staging.py) callsvalidate_displaybeforeencodeand stores the codes on the staged value. Use short snake_case codes, for exampleprice_required,price_invalid,binary_text_invalid.
Tests
| Test | What it checks |
|---|---|
micyte/tests/unit/test_a_lens_is_a_declaration.py | Every declared lens against the answers recorded from the classes it replaced (data/lens_golden.json), the resolution grid, the catalog, refusals, the script's block. |
fnd_app/tests/e2e/test_the_script_runs_the_same_lenses.py | The browser's runner against Python, every lens over every vector, in Chromium. |
micyte/tests/unit/test_state_machine_lens_registry.py | Resolution order, fallback, the catalog. |
micyte/tests/unit/test_fiat_lens.py | fiat_cents round trip and refusals. |
fnd_app/tests/unit/test_lens_management.py | The per-instance disabled set and the toggle API. |
Add a case that resolves your binding to your lens_id and one that round-trips a value.
Not built
- Binding a lens to a hyphae value.
resolveacceptsflag_lens_id, but the hyphae flag registry is empty and no caller passes the argument. See 60. - A store's own lenses. The declarations ship with the package. A lens kept as a datum document in an instance's store would have no reader until hyphae flags bind lenses.
- A surface for the toggles. The toggle API works; the panel module for it is mounted by no surface.