cultcache-ts codec: unit arm is a frozen object, recursive test codec annotated

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-10-11 03:51:11 +02:00
commit f43a0d26f9
2 changed files with 10 additions and 6 deletions

View file

@ -158,9 +158,12 @@ export function slots<TValue extends object>(spec: SlotsSpec<TValue>): SlotsCode
}
// An arm with no slots. It is written [key, []], never [key, nil].
export const unit = Symbol("cultcache.unit-arm");
export interface UnitArm {
readonly unit: true;
}
export const unit: UnitArm = Object.freeze({ unit: true });
type ArmSpec<TKind extends string> = readonly [armName: TKind, body: SlotsCodec<any> | typeof unit];
type ArmSpec<TKind extends string> = readonly [armName: TKind, body: SlotsCodec<any> | UnitArm];
// A tagged union written [armKey, [armSlots...]]. The reader refuses an undeclared key, naming the union, the key and
// the declared keys; there is no fallback arm.
@ -172,14 +175,14 @@ export function union<TValue extends { kind: string }>(
if (keys.length === 0) {
throw new Error(`Union ${name} declares no arms.`);
}
const byKey = new Map<number, { armName: string; body: SlotsCodec<any> | typeof unit }>();
const byKey = new Map<number, { armName: string; body: SlotsCodec<any> | UnitArm }>();
const byName = new Map<string, number>();
for (const key of keys) {
const [armName, body] = arms[key];
if (byName.has(armName)) {
throw new Error(`Union ${name} declares the arm "${armName}" twice.`);
}
if (body !== unit && body.names.includes("kind")) {
if (body !== unit && (body as SlotsCodec<any>).names.includes("kind")) {
throw new Error(`Union ${name} arm "${armName}" declares a slot named kind, which is the union's own tag.`);
}
byKey.set(key, { armName, body });
@ -197,7 +200,7 @@ export function union<TValue extends { kind: string }>(
throw new Error(`Union ${name}: the value's kind is not one of its arms (${[...byName.keys()].join(", ")}).`);
}
const { armName, body } = byKey.get(key)!;
return [key, body === unit ? [] : at(`arm ${armName}`, () => body.encode(value))];
return [key, body === unit ? [] : at(`arm ${armName}`, () => (body as SlotsCodec<any>).encode(value))];
},
decode(raw) {
if (raw === null) {

View file

@ -8,6 +8,7 @@ import { decode, encode } from "@msgpack/msgpack";
import { z } from "zod";
import { codecs } from "../src";
import type { Codec } from "../src/codec";
import { CultCache } from "../src/cult-cache";
import { defineDocumentType } from "../src/document";
import { SingleFileMessagePackBackingStore } from "../src/single-file-messagepack-backing-store";
@ -43,7 +44,7 @@ interface Surface {
}
const composition = slots<Composition>({ 0: ["name", string], 1: ["body", lazy(() => node)] });
const node = union<Node>("Node", {
const node: Codec<Node | null> = union<Node>("Node", {
0: ["Box", slots<{ id: string; children: (Node | null)[] }>({ 0: ["id", string], 1: ["children", list(lazy(() => node))] })],
1: ["Text", slots<{ id: string; content: string }>({ 0: ["id", string], 1: ["content", string] })],
2: ["Use", slots<{ id: string; composition: Composition; slots: (Node | null)[] }>({