MiCyte

← The wiki

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:

MemberDirectionReturns
lens_id—Unique id, lower case.
decode(canonical_value)stored → shownThe display value.
encode(display_value)typed → storedThe canonical value.
validate_display(display_value)typedA 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"]}
}
  • decode and encode are lists of steps, run in order. An empty list is the passthrough: the value comes back unchanged, whatever its type.
  • validate runs its steps on the typed value, then its checks in order. The first check that fails gives the one issue code returned.
  • bindings say which datums the registry chooses the lens for. One binding belongs to one lens.
  • presents says what the lens's display reads as: number (a value: a count, a price, a node address), nominal (a name or words), or as_read, where the value decides (42 reads as a number, lawn_care as a name). Omitted, it is as_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: true keeps a lens out of the per-instance toggles. fallback: true marks 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.

StepDoes
textNone → ""; anything else → str of it, stripped. Begins every pipeline but the passthrough's.
collapse_spacesEvery run of whitespace → one space.
upper, lowerCase.
["default", "<shown>"]<shown> in place of an empty value.
cents_to_currency450 → $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_asciiA bit string → its text, 8 bits a character; · for a byte outside printable ASCII; <N> bits when nothing decodes.
wordslawn_care → Lawn Care: split at underscores, each word's first letter raised.
CheckPasses when
requiredThe value is not empty.
asciiEvery character is ASCII.
email_shapeAn @ that neither starts nor ends the value.
{"check": "excludes", "value": "<word>"}<word> (lower case) appears nowhere, in any case.
digit_groupsASCII digits in groups joined by single hyphens.
currencyparse_cents accepts it, in ASCII digits.
bitsOnly 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_idBehaviour
identityReturns the value unchanged. The fallback; always on.
trimmed_stringStrips surrounding whitespace.
samras_titleCollapses spaces and upper-cases a title on save; refuses a title that is not ASCII.
email_addressLower-cases an email address; checks the @.
secret_referenceShows a secret reference (not_configured when empty), never a secret; refuses one containing "password".
numeric_hyphenChecks a SAMRAS/HOPS node address is digit groups: a-b-c.
fiat_centsInteger cents ↔ $d.cc. Symmetric.
binary_textBit string → ASCII text. Display only: encode keeps the bit string.
labelA 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:

  1. flag_lens_id — a lens named by a hyphae flag (matched_on="hyphae_flag"). No caller passes it; see Not built.
  2. recognized_family in a lens's bindings.families — for example fiat_babelette → fiat_cents, samras → numeric_hyphen, title_babelette → binary_text.
  3. overlay_kind in a lens's bindings.overlays.
  4. primary_value_kind in a lens's bindings.value_kinds — for example fiat_cents, binary_string, literal_text.
  5. 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

  1. 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 to lens_transforms.py AND to v2_portal_lens.js, with vectors that hold the two to one answer.
  2. Decide whether it is symmetric. A symmetric lens stores what encode returns (fiat_cents, email_address). A display-only lens leaves the stored value as typed (binary_text). For a symmetric lens, decode(encode(d)) must return d apart from deliberate normalization.
  3. 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 and lens_by_id read the declarations.
  4. Regenerate the browser's copy: python -m micyte.ui.lenses --write. The public suite fails while the script's block and the file differ.
  5. Return issue codes, not prose. StagingArea.stage_with_lens (micyte/state_machine/nimm/staging.py) calls validate_display before encode and stores the codes on the staged value. Use short snake_case codes, for example price_required, price_invalid, binary_text_invalid.

Tests

TestWhat it checks
micyte/tests/unit/test_a_lens_is_a_declaration.pyEvery 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.pyThe browser's runner against Python, every lens over every vector, in Chromium.
micyte/tests/unit/test_state_machine_lens_registry.pyResolution order, fallback, the catalog.
micyte/tests/unit/test_fiat_lens.pyfiat_cents round trip and refusals.
fnd_app/tests/unit/test_lens_management.pyThe 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. resolve accepts flag_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.