/** * RFC-0041 §5: the values a reader sees. Pure: callers pass the translation group's original when * the entry is a translation; nothing here loads nodes or throws. * * - localized: the entry's own value, else the original's (`fromOriginal`); * - non-localized: the original's value on a translation, the entry's own otherwise; * - a language edition (an original in another site) owns all its values; * - during a field operation the `migration` marker is read through: a missing key falls back to * `fromKey`; a value still in `fromType` that does not fit the new type is converted by RFC-0041 * §6's closed table (`conversions.ts`), and resolves to `undefined` with an issue only when the * table cannot convert it; a field being removed is already gone; * - RFC-0045: a reference resolves to its group ids and a repeater to its items, each sub-value * checked as a field is (loading targets is the renderer's job); a definition of a type this * version does not know is skipped, so a newer writer's type never breaks an older reader. */ import type { ContentEntry, FieldDefinition } from "@cloudhal/core-contracts"; import { type ConvertContext, convertFieldValue } from "./conversions.ts"; import { convertContext, isBeingRemoved, rewriteFields } from "./migration.ts"; import { rewriteItems } from "./migration-sub.ts"; import { textReferenceOutcome } from "./text-reference.ts"; import { type FieldIssue, fieldDefinitions, isKnownFieldType, isRecord, renamedFrom, type SiteFields, } from "./types.ts"; import { valueIssues } from "./value-checks.ts"; export interface ResolvedField { def: FieldDefinition; /** `undefined` when there is no value, or the stored one does not fit `def`. */ value: unknown; /** The value came from the translation group's original. */ fromOriginal: boolean; /** Why a stored value resolved to `undefined`. */ issue?: FieldIssue; } /** Every defined field by key, in definition order. */ export type ResolvedFields = Record; export type FieldsEntry = Pick & { translation?: ContentEntry["translation"] | undefined; }; export type FieldsOriginal = Pick & { translation?: Pick, "group"> | undefined; }; /** * True when the entry is a translation whose original is in the same site, and that original * still has its own id as its group (RFC-0045 §2 "Membership", OQ 14: a relinked node inherits * to nobody, so it never shows another group's values). */ function readsOriginal(entry: FieldsEntry, original: FieldsOriginal | undefined): boolean { const t = entry.translation; if (!t || t.group === entry.id || t.source?.siteId !== undefined) return false; if (original === undefined || original.id !== t.group) return false; return (original.translation?.group ?? original.id) === original.id; } function read(values: unknown, def: FieldDefinition): unknown { if (!isRecord(values)) return undefined; if (Object.hasOwn(values, def.key)) return values[def.key]; const from = renamedFrom(def); return from && Object.hasOwn(values, from) ? values[from] : undefined; } function pick( def: FieldDefinition, entry: FieldsEntry, original: FieldsOriginal | undefined, ): { value: unknown; fromOriginal: boolean } { const fromOriginal = readsOriginal(entry, original); if (!def.localized) return fromOriginal ? { value: read(original?.fields, def), fromOriginal: true } : { value: read(entry.fields, def), fromOriginal: false }; const own = read(entry.fields, def); if (own !== undefined || !fromOriginal) return { value: own, fromOriginal: false }; const theirs = read(original?.fields, def); return { value: theirs, fromOriginal: theirs !== undefined }; } /** * The entry's typed values under its collection's definitions (`{}` when it has none). `convert` * adds to the site's conversion context: RFC-0045's prefetched entry maps, which readers build * only while a text ↔ reference conversion runs. */ export function resolveFields( site: SiteFields, entry: FieldsEntry, original?: FieldsOriginal, convert?: Pick, ): ResolvedFields { const out: ResolvedFields = {}; // RFC-0045 §7: text → reference never picks the entry's own group. const context = { ...convertContext(site), group: entry.translation?.group ?? entry.id, ...convert, }; for (const def of fieldDefinitions(site, entry.collection)) { if (isBeingRemoved(def) || !isKnownFieldType(def.type)) continue; const picked = pick(def, entry, original); const { fromOriginal } = picked; const value = throughSub(def, picked.value, context); if (value === undefined) { out[def.key] = { def, value: undefined, fromOriginal: false }; continue; } // A sub-field marker converted the items already (`throughSub`). const converting = def.migration?.sub === undefined ? def.migration?.fromType : undefined; // RFC-0045 §7: text ↔ reference values are decided by the one shared rule. const decided = converting ? textReferenceOutcome({ from: converting, def, value, convert: context, written: false, }) : undefined; const issues = valueIssues(def, value, `fields/${def.key}`); const first = issues[0]; if (decided?.kind === "keep" || decided?.kind === "check" || (!decided && !first)) { out[def.key] = { def, value, fromOriginal }; continue; } const converted = converting && decided?.kind !== "fail" ? convertFieldValue(converting, def, value, context) : undefined; if (converted?.ok) { out[def.key] = { def, value: converted.value, fromOriginal: converted.value !== undefined && fromOriginal, }; continue; } out[def.key] = { def, value: undefined, fromOriginal, issue: { path: first?.path ?? `fields/${def.key}`, code: "field_value_unreadable", message: converting ? `a ${converting} value that does not convert to ${def.type}` : (first?.message ?? "unreadable"), }, }; } return out; } /** * RFC-0045 §7 `sub`: a repeater's items read through its sub-field marker. A sub-value the table * cannot convert reads as none (an item left empty is dropped), as a top-level value does. */ function throughSub(def: FieldDefinition, value: unknown, convert: ConvertContext): unknown { if (def.type !== "repeater" || def.migration?.sub === undefined || value === undefined) return value; const r = rewriteItems(def, value, { convert, pristine: false, onInvalid: "clear", rewrite: rewriteFields, }); return r.next === "unchanged" ? value : r.next; } /** "Has a value" for `requiredToPublish`: not absent, not an empty string, not an empty list. */ export function hasFieldValue(value: unknown): boolean { if (value === undefined || value === null) return false; if (typeof value === "string") return value.trim() !== ""; if (Array.isArray(value)) return value.length > 0; return true; }