Strict functional requirements for the tech-agnostic CRUD view contract: the exact public surface, the asynchronous callback semantics, the exact error messages, and the exact wire shapes the transport adapter ships. A renderer, a domain module, or a conformance double is correct when it matches these tables — every table below is a test assertion.
You reach this document when you need the contract exactly: which method
takes which callback, what error a validation produces word for word, or what
bytes cross the router.Caller. For how to use the library read
README.md; for the rules a contributor must not violate read
AGENTS.md. This page is the single, permanent home of the
contract — the working plan documents that drove past changes are replaced or
deleted every cycle, so nothing here depends on them.
package view
import (
"webtyp.com/model"
"webtyp.com/router"
)
// Screen is one entry a module offers to the app's menu, declared as data. The renderer
// draws the same outline around every screen and puts the content in cards it draws:
// one (Main) or two (Main and Aside); each card scrolls inside itself. A module never
// draws a frame, a card or a scroll area.
// A Screen is either a leaf or a group (it has Children):
//
// - list leaf: ID, Label, Icon, Permission and Presenter are required. The renderer
// makes its cards (form as Main, search/list/actions as Aside). Its help text is
// the Presenter's Description() (view.WithDescription), never a second copy here.
// - card leaf: ID, Label, Icon, Permission, Main and Help are required; Aside
// (Controls, List, Footer — List required) and Focus (*CardFocus: which card a
// phone shows) are optional. For a screen that is not a list.
// - group: ID, Label, Icon, Help and Children are required; it has no
// Presenter, cards or Permission of its own — the menu shows it only when
// the user may see at least one child, and opening it opens the first one.
// Record is a record a list can show: a model whose Item() ormc generates from the
// fields marked Item:… (model.ItemLabel, …).
type Record interface {
model.Model
model.Itemizer
}
// Lister is what a view needs from the application: the records to show.
type Lister interface {
List(args ListArgs, done func(rows []Record, err error))
Page() Page
}
type Action struct {
Op string
Label lang.Text
Confirm lang.Text
Args func(records []model.Model) model.Encodable
}
type Actioner interface {
Actions() []Action
Run(op string, done func(error))
}
type ActionRunner interface {
Actions() []Action
RunAction(op string, args model.Encodable, done func(error))
}
// Presenter is the UI-agnostic core behind any CRUD view.
type Presenter interface {
Title() string
SearchPlaceholder() string
Record() Record
Items() []model.Item
Search(term string, done func(error))
HasMore() bool
More(done func(error))
Reload(done func(error))
Selected() string
Select(id string) Record
Deselect()
OnChange(fn func())
}
// Capabilities, discovered by type assertion. A Presenter carries a method
// set that mirrors the lister's (see §4).
type Saver interface {
Save(recs []model.Model, done func(error))
}
type Updater interface {
Update(ids []string, rec model.Model, fields []string, done func(error))
}
type Deleter interface {
Delete(ids []string, done func(error))
}
type Ops struct {
Module string
List string
Save string
Update string
Delete string
Actions []Action
}
func NewCallerLister(c router.Caller, ops Ops, newList func() model.ModelSlice) Lister
func New(l Lister, record model.Model, opts ...Option) Presenter
type Option func(*config) // unexported body
func WithTitle(title string) Option
func WithSearchPlaceholder(placeholder string) OptionEverything else is unexported: core, indexEntry, lookup, rowName, the
capability wrappers, and the wire envelopes saveArgs / updateArgs /
deleteArgs.
Applies to List, Save, Update, Delete and Reload alike.
doneis the last parameter of every call.- The method returns nothing. The result, or the error, is delivered to
doneexactly once. doneis always non-nil for the implementer.viewnormalizes a nil consumer callback to a no-op once at entry; an implementer may assumedone != nil.- The implementer must not block and must not assume the caller has the
result when the call returns. A transport invokes
donefrom its own callback (a later event-loop turn); an in-process store may invoke it immediately.viewmust behave identically either way — see theno_blocking_in_listclause (§8). - One channel of error. Programming errors validated by
view(an empty batch, an unknown id) and transport errors both arrive throughdone. There is no error return value to ignore. - A consumer may pass
nilfordoneto fire-and-forget (e.g.p.Reload(nil)).
- Normalizes
done(§2.3), then callsp.lister.List(cb). - Inside
cb:err != nil→done(err); the currentorderis left unchanged.- otherwise resets
orderand records the ID of each row, inserting them intoview.Records.
- On success
done(nil). Items()returns the projected list; before the first successful Reload it is empty.
The module declares Page{First, Size, More} (see page.go): First rows on open (0 = wait for a search, All =
every row up to MaxPage), Size rows per search and per scroll step, More = the scroll asks for the rows after
the last one shown until a page comes back shorter than Size. The search runs on the server (ListArgs.Search):
with pages the browser never holds every row, so a local filter would miss records that exist. A list operation
decodes crud.ReadArgs and caps its limit with crud.Cap (MaxPage).
| Call | Result |
|---|---|
Select(id) with id in the index |
sets selected = id, returns the record |
Select(id) with unknown id |
returns nil, selected unchanged |
Deselect() |
selected = "" |
Selected() |
the current selection ("" if none) |
lookup scans index linearly — a projected list holds tens to low hundreds
of rows, so the scan is microseconds; do not reintroduce a map.
view.New returns a concrete type whose method set mirrors the lister's. A
renderer discovers capabilities by type assertion; the assertion is true on the
presenter iff the lister implements the matching interface.
| Lister implements | view.New returns |
Presenter satisfies |
|---|---|---|
Lister only |
*core |
Presenter |
+ Saver |
*saveable |
Presenter, Saver |
+ Updater |
*updatable |
Presenter, Updater |
+ Deleter |
*deletable |
Presenter, Deleter |
+ Saver, Updater |
*saveableUpdatable |
Presenter, Saver, Updater |
+ Saver, Deleter |
*saveableDeletable |
Presenter, Saver, Deleter |
+ Updater, Deleter |
*updatableDeletable |
Presenter, Updater, Deleter |
+ Saver, Updater, Deleter |
*crud |
Presenter, Saver, Updater, Deleter |
The wrappers are one-line delegations to core; no logic lives in them. There
are 2^n-1 of them for n optional capabilities. A fourth capability is a
design review trigger, not another eight structs.
Compared word for word by tests. Do not reword.
| Call | Condition | done receives |
|---|---|---|
Save(recs, done) |
len(recs) == 0 |
view: Save requires at least one record |
Save(recs, done) |
any record model.IsNil |
view: Save payload is nil |
Save(recs, done) |
lister is not a view.Saver |
view: Save: lister does not implement view.Saver |
Update(ids, rec, fields, done) |
len(ids) == 0 |
view: Update requires at least one id |
Update(ids, rec, fields, done) |
len(fields) == 0 |
view: Update requires at least one field |
Update(ids, rec, fields, done) |
model.IsNil(rec) |
view: Update record is nil |
Update(ids, rec, fields, done) |
lister is not a view.Updater |
view: Update: lister does not implement view.Updater |
Delete(ids, done) |
len(ids) == 0 |
view: Delete requires at least one id |
Delete(ids, done) |
any id not in the index | view: Delete: unknown id "<id>" — nothing is sent |
Delete(ids, done) |
lister is not a view.Deleter |
view: Delete: lister does not implement view.Deleter |
list(done) (callerLister) |
a row is not a model.Model |
view: Reload: row type <name> does not implement model.Model |
list(done) (callerLister) |
newList() is not a model.Decodable |
view: list returned by newList does not implement model.Decodable |
In every case done is invoked exactly once and the underlying lister /
caller is not reached.
Panics are programmer wiring bugs, detected at startup; they are not routed
through done.
| Constructor | Condition | Panic |
|---|---|---|
New |
l == nil |
view: New: lister is required |
New |
model.IsNil(record) |
view: New: record is required |
NewCallerLister |
c == nil |
view: NewCallerLister: caller is required |
NewCallerLister |
ops.Module == "" |
view: NewCallerLister: Ops.Module is required — it must match the server module's ModelName() |
NewCallerLister |
ops.List == "" |
view: NewCallerLister: Ops.List is required |
NewCallerLister |
newList == nil |
view: NewCallerLister: newList is required |
New does not change the lister's signature; NewCallerLister neither changes
its signature nor its panics.
callerLister is the single place a typed call becomes an operation name plus
a wire envelope.
- The returned
ListerimplementsSaver/Updater/Deleteriff the matchingops.*name is non-empty — the same mirroringview.Newapplies, so a remote backend's capabilities are honest by construction. listbuildsnewList(), requires it to be amodel.Decodable, then callsc.caller.Call(ops.Module + "." + ops.List, nil, dec, cb).save/update/deletecompose the identical qualified name fromops.Moduleplus the bare op name — no caller ever writes "module.op" as a hand-assembled literal. Insidecbit convertsLen()/At(i)rows to[]model.Modeland delivers them viadone.save/update/deleteare a singlec.caller.Call(op, envelope, nil, done)— theCaller's callback already has thefunc(error)shape.
Wire envelopes (model.Encodable):
| Envelope | Fields written |
|---|---|
saveArgs |
records array, one object per record |
updateArgs |
ids array of strings, fields array of strings, record object |
deleteArgs |
ids array of strings |
A renderer is correct only if every clause passes. The clause names are the contract:
mount_triggers_list_load list_renders_item_labels
select_fills_form save_ships_form_values
unchanged_save_does_not_ship revert_edit_is_not_dirty
delete_ships_selected_record no_save_capability_without_saver
deselect_clears_selection select_unknown_id_returns_nil
filter_matches_label_and_description
delete_unknown_id_errors new_focuses_first_field
edit_focuses_first_field cancel_clears_focus
no_delete_capability_without_deleter
no_update_capability_without_updater
saver_capability_mirrors_backend plural_save_delete_and_update
no_blocking_in_list
no_blocking_in_list is the invariant the callback contract exists to
guarantee: a Lister whose done fires in a later event-loop turn must work
identically — nothing may be assumed available when List returns. Clauses are
never weakened or deleted; if one stops compiling, rewrite it with callbacks.
The package-level tests also pin two behaviours directly:
TestListDoneRunsLater—Items()is empty before a deferreddoneruns, then correctly projected.TestValidationErrorsTravelThroughDone— every §5 failure arrives throughdonewith the exact message, and no return value exists to ignore.
What it is. An action is a command an operator runs on the list as a whole — "Apply the pending changes", "Resend", "Recalculate". It is not an edit of a record: it has no form, it is usually not undoable, and its arguments come from the data on screen (e.g. the fingerprint of the plan the operator is looking at), never typed by the operator.
Why it is not a fourth capability (§4). §4 says a fourth capability is a design review trigger. The review's outcome: a capability is a kind of write a backend may or may not support; an action is a named, declared command — a backend offers a list of them, possibly empty. So actions are data on the presenter, not a type in the mirroring table: no new wrapper structs.
type Action struct {
Op string // bare op name; qualified with Ops.Module exactly like List/Save
Label lang.Text // button text (dictionary key)
Confirm lang.Text // question asked before running; "" = runs on click
// Args builds the op's arguments from the records of the last Reload.
// nil = the op takes no arguments.
Args func(records []model.Model) model.Encodable
}
// Actioner is implemented by EVERY Presenter view.New returns.
type Actioner interface {
Actions() []Action // the backend's actions; empty when it offers none
Run(op string, done func(error)) // see behaviour below
}
// ActionRunner is the Lister-side half, implemented by the Lister NewCallerLister
// returns (and by any hand-written Lister that offers actions).
type ActionRunner interface {
Actions() []Action
RunAction(op string, args model.Encodable, done func(error))
}Ops gains Actions []Action.
Actions()returns the lister's actions when it implementsActionRunner, else an empty slice.Run(op, done):donenil → replaced by a no-op (same rule asReload).- unknown
op→done(error)with messageview: Run: unknown action <op>. Argsnon-nil → called with the records indexed by the lastReload, in list order.RunAction(op, args, cb); an error is delivered throughdoneand no reload happens.- On success,
Reload(done): the list shows the state after the action.
callerLister.RunAction=c.caller.Call(<Module>.<Op>, args, nil, done)— the qualified name is composed byOps.qualified, never by a consumer.- Constructing
NewCallerListerwith an action whoseOporLabelis empty panics:view: NewCallerLister: Action.Op is required/… Action.Label is required.
- Draw one control per action, labelled with its
Label. - Disable every action control while the presenter has no items (an action runs over the list) and while that action is running (no double submit).
- When
Confirmis non-empty, ask it and run only on explicit confirmation.