Skip to content

refactor(core-types): centralize dispatch and preserve clone aliases (1/11) - #81

Closed
ofriw wants to merge 14 commits into
mainfrom
rs/58-1-type-ops
Closed

ofriw wants to merge 14 commits into
mainfrom
rs/58-1-type-ops

Conversation

@ofriw

@ofriw ofriw commented Oct 29, 2026 •

Copy link
Copy Markdown
Collaborator

Note: This PR was generated by an AI agent. If you'd like to talk with other humans, drop by our Discord!


The problem

GoatDB kept the same knowledge about CoreType in three separate switch statements: base-encoder.ts, clone.ts, and equals.ts. Each new type demanded three edits. Forget one, and that type fell through with no error: a wrong clone, a wrong equality result, or a wrong byte on the wire. Nothing caught the gap at compile time.

The change

base/core-types/type-ops.ts now holds clone, equals, encode and childrenOf for every CoreType in one frozen table. kTypeOps is declared satisfies Record<CoreType, CoreTypeOps>, so the compiler rejects a missing entry. The three switches are gone. The public helpers are thin now.

The table recurses by calling itself, never the public wrappers. This keeps the module graph acyclic. convertValue and fillRefs read the table too, so the encoder and the cyclical encoder share the same source of truth.

Grouped changes

1. One home for type knowledge — type-ops.ts adds CoreTypeOps, CoreCloneContext, CoreChildren and a shared kNoChildren constant. clone.ts drops from 139 lines to a single context allocation. equals.ts drops its 242-line switch. BaseEncoder declares convert* methods public abstract, so the table can call them while subclasses keep control of encoding.

2. A cycle removed — encoding/encoded-ref.ts is a new leaf module. It owns the EncodedRef shapes and the isEncodedRef guards. cyclical.ts re-exports them, so no caller changes. The former type-ops/cyclical cycle is gone.

3. Declared behavior changes — CHANGELOG.md records three items, so no reader must discover them: clone keeps shared references across array and set siblings; coreValueHash is removed in favor of encodableValueHash; objectFilterFields equality is now symmetric. The last item fixes a real bug: the second comparison loop handed the wrong object to the filter.

4. Known quirk kept, not fixed — Ref collection does not apply iterableFilter. Final JSON encoding does. This is the pre-existing behavior, pinned by tests and marked TODO(#58-followup). A silent change here would move every __r/__d payload.

5. Tests that hold the line — tests/core-value-serialization.test.ts adds 1094 lines of inline goldens: per-type JSON, error behavior, the cyclical ref matrix, checksums, clone aliasing, equality, and a frozen-and-exhaustive table check. If a later refactor moves a byte, the diff shows it, and a reviewer decides.

6. Verification prerequisites — Only changes this branch needed to pass its own gates. Pins for Playwright 1.64.1, esbuild 0.25.12 and the esbuild plugin 1.2.1; regenerated build assets; a node:http overload fix for the pinned Deno floor; a typed postMessage transfer list; a type-only import in the log entry; and a compile fixture that links local GoatDB before it installs app dependencies.

Impact

Type dispatch has one owner. A new CoreType is one entry plus one classifier branch, and the compiler enforces the rest. Serialized output stays where it was, because goldens say so, not because reviewers hoped so.

@ofriw ofriw closed this Nov 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant