Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions changelog.d/typescript-reference-facet-reader.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
- **The TypeScript loader reads and enforces the reference facet** (`ref` on a `type: "attr"`
entry of a provider definition, `spec/metamodel-definitions.md`). A reference attribute can now
say in data what it may name (target type, admitted subtypes, same system), and the loader
checks it with no code per provider: the lookup is typed by the target, a dotted value resolves
`System.child` and `pkg::System.child`, a bare value is looked for in the referrer's own
containers first, and a value that names nothing (`ERR_REF_UNRESOLVED`), the wrong kind of node
(`ERR_REF_WRONG_KIND`) or a node of another system (the facet's own code) is refused. The
registry manifest writes `ref` only on an attribute that has one. The two default codes are in
`fixtures/conformance/ERROR-CODES.json` and in the TypeScript, Python, Java and C# error-code
catalogs. No definition file uses the facet yet, so no registry, manifest or generated output
changes. A provider that sets `TypeDefinition.references` in code: the descriptor's
`targetSubType` is now `targetSubTypes` (a list), `dottedFieldPath` is now `fieldPath`, and
`errorCode` is optional.
7 changes: 4 additions & 3 deletions docs/features/extending-with-providers.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,10 @@ file format) is covered by core's package version (`docs/compatibility-policy.md
`spec/metamodel/*.json` format, and register it with `applyProviderDefinition(registry, definition,
factories, validators?)` from `@metaobjectsdev/metadata`. `factories` maps each
`"<type>.<subType>"` to the function that constructs its node; the optional `validators` map gives
a type a `NodeValidator`, for a rule the data cannot state (a reference to a nested node, say).
The bundled packs register this way, and so do the independent packs: `packs/systems/` is a
worked example.
a type a `NodeValidator`, for a rule the data cannot state. What a reference attribute may name,
a nested node included, is data: see [the reference facet](../../spec/metamodel-definitions.md#the-reference-facet)
and, there, which ports read it. The bundled packs register this way, and so do the independent
packs: `packs/systems/` is a worked example.

## Two registration modes

Expand Down
2 changes: 2 additions & 0 deletions fixtures/conformance/ERROR-CODES.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
"ERR_STORAGE_WITHOUT_OBJECT_REF": "@storage was set on a field that has no @objectRef.",
"ERR_OBJECT_FIELD_WITHOUT_OBJECT_REF": "ADR-0013: a field.object declares no @objectRef. A field.object models a typed nested value and REQUIRES @objectRef. The message states the whole ladder rather than only the escape hatch: a known key set is an object.value the field @objectRefs; dynamic keys over a known value type are a field.map (@objectRef for a value object, @valueType for a scalar); only a bag no reader pins a key in is @dbColumnType: jsonb on a field.string. Naming the escape hatch alone is what taught adopters to reach for the bag by default.",
"ERR_UNRESOLVED_OBJECT_REF": "ADR-0042: a field.object / field.map @objectRef does not resolve to any object in the loaded tree (a dangling target). The ref resolves package-locally when bare (referrer's package, else root-level) and exactly when FQN — a bare cross-package ref no longer binds elsewhere. The error names same-short-name objects in other packages so the author can qualify it.",
"ERR_REF_UNRESOLVED": "spec/metamodel-definitions.md: an attribute whose definition carries the reference facet (`ref`) names a node that resolves to nothing, by the typed lookup or by the untyped lookup the loader runs to choose its error. The default code when the facet names no `errorCode`.",
"ERR_REF_WRONG_KIND": "spec/metamodel-definitions.md: an attribute whose definition carries the reference facet (`ref`) names a node of another type than the facet's `targetType`, or a subtype outside its `targetSubTypes`. The default code when the facet names no `errorCode`.",
"ERR_RESERVED_ATTR": "An @-prefixed reserved structural keyword (e.g. @name, @isArray, @children) was used as an inline attribute.",
"ERR_SCOPE_PATTERN_INVALID": "Phase-1 metadata-source-resolution: a scope include/exclude package pattern is malformed (empty pattern or empty :: segment).",
"ERR_SOURCE_NO_PRIMARY": "An object declares source nodes but none has role=primary.",
Expand Down
8 changes: 8 additions & 0 deletions fixtures/registry-conformance/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,14 @@ still fails everywhere.
`@object` (ADR-0001) and `@objectAdapter` (ADR-0005) are `["java", "kotlin"]`, the JVM runtime.
They used to be carved out as `PRESENTATION_ONLY` / `NATIVE_BINDING`; both reasons are retired.

### The reference facet (`ref`)

An attr whose definition entry carries the reference facet (`spec/metamodel-definitions.md`)
carries `"ref"` in the manifest, after any `ports` and before `description`: `targetType`, then
`targetSubTypes` (sorted), `fieldPath` and `sameSystem` (only when `true`), `errorCode` and
`wrongSystemCode` (only when present). An attr with no facet writes no `ref`, so the canonical
does not change until a definition file uses one.

### EXCLUDED from v1 — deferred follow-ons (documented, not silent)

These were evaluated against the three "open boundary calls" in the design spec
Expand Down
3 changes: 2 additions & 1 deletion packs/systems/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ Apps and resources are children of a system, so they are addressed the way the l
any nested node (ADR-0059): `System.child` (`Shop.db`), or `pkg::System.child` from another
package. A bare name means a node of the referrer's own system (of the kind the attribute wants,
when an app and a resource share the name), then a top-level node of that name in the
referrer's package. This bare-name rule is the systems packs' convention, not the loader's.
referrer's package. The systems packs apply this rule in their own validators, not through the
loader's [reference facet](../../spec/metamodel-definitions.md#the-reference-facet).

- `@needs` must name `resource.*` nodes, and `@calls` must name `app.*` or `system.external` nodes.
A name that resolves to nothing, or to the wrong kind of node, fails the load with
Expand Down
15 changes: 8 additions & 7 deletions packs/systems/typescript/src/refs.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// Resolving a reference to a node inside a system.
//
// Apps and resources are children of a system.internal, so they are not top-level nodes
// and the loader's reference descriptors (which bind top-level nodes only) cannot reach
// them. They are addressed the house way instead (ADR-0059): `System.child` binds the
// system among the top-level nodes and walks to its child, and `pkg::System.child`
// does so from another package. A bare name is a convenience for the common case, a
// node in the referrer's own system, then a top-level node of that name (an outside
// system declared at the document root).
// Apps and resources are children of a system.internal, so they are not top-level nodes.
// The pack resolves them in its own validator: its supported core range (>=1.3.0) includes
// releases whose loader reference descriptors bind top-level nodes only, and @calls admits
// two target types, which one reference facet cannot name. They are addressed the house
// way (ADR-0059): `System.child` binds the system among the top-level nodes and walks to
// its child, and `pkg::System.child` does so from another package. A bare name is a
// convenience for the common case, a node in the referrer's own system, then a top-level
// node of that name (an outside system declared at the document root).

import {
CHILD_REF_SEPARATOR,
Expand Down
14 changes: 14 additions & 0 deletions server/java/metadata/src/main/java/com/metaobjects/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,20 @@ public enum ErrorCode {
*/
ERR_UNRESOLVED_OBJECT_REF,

/**
* spec/metamodel-definitions.md: an attribute carrying the reference facet ({@code ref})
* names a node that resolves to nothing. The facet's default code when it names no
* {@code errorCode}.
*/
ERR_REF_UNRESOLVED,

/**
* spec/metamodel-definitions.md: an attribute carrying the reference facet ({@code ref})
* names a node of the wrong type or subtype. The facet's default code when it names no
* {@code errorCode}.
*/
ERR_REF_WRONG_KIND,

/** An object declares source nodes but none has role=primary. */
ERR_SOURCE_NO_PRIMARY,

Expand Down
4 changes: 4 additions & 0 deletions server/python/src/metaobjects/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,10 @@ class ErrorCode(str, Enum):
# retired ADR-0041 ERR_AMBIGUOUS_REF (bare = package-local ⇒ ambiguity is
# unreachable, so every unresolved ref fails closed with its per-attr code).
ERR_UNRESOLVED_OBJECT_REF = "ERR_UNRESOLVED_OBJECT_REF"
# spec/metamodel-definitions.md — the reference facet's default codes: the value
# resolves to nothing, or to a node of the wrong type or subtype.
ERR_REF_UNRESOLVED = "ERR_REF_UNRESOLVED"
ERR_REF_WRONG_KIND = "ERR_REF_WRONG_KIND"
# FR-033 — strict structural-placement vocabulary. ERR_CHILD_NOT_ALLOWED: a
# structural child (field/identity/source/validator/… — not an attr) is placed
# under a parent whose registered childRules do not admit it (the structural
Expand Down
2 changes: 2 additions & 0 deletions server/rust/metaobjects/data/ERROR-CODES.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
"ERR_STORAGE_WITHOUT_OBJECT_REF": "@storage was set on a field that has no @objectRef.",
"ERR_OBJECT_FIELD_WITHOUT_OBJECT_REF": "ADR-0013: a field.object declares no @objectRef. A field.object models a typed nested value and REQUIRES @objectRef. The message states the whole ladder rather than only the escape hatch: a known key set is an object.value the field @objectRefs; dynamic keys over a known value type are a field.map (@objectRef for a value object, @valueType for a scalar); only a bag no reader pins a key in is @dbColumnType: jsonb on a field.string. Naming the escape hatch alone is what taught adopters to reach for the bag by default.",
"ERR_UNRESOLVED_OBJECT_REF": "ADR-0042: a field.object / field.map @objectRef does not resolve to any object in the loaded tree (a dangling target). The ref resolves package-locally when bare (referrer's package, else root-level) and exactly when FQN — a bare cross-package ref no longer binds elsewhere. The error names same-short-name objects in other packages so the author can qualify it.",
"ERR_REF_UNRESOLVED": "spec/metamodel-definitions.md: an attribute whose definition carries the reference facet (`ref`) names a node that resolves to nothing, by the typed lookup or by the untyped lookup the loader runs to choose its error. The default code when the facet names no `errorCode`.",
"ERR_REF_WRONG_KIND": "spec/metamodel-definitions.md: an attribute whose definition carries the reference facet (`ref`) names a node of another type than the facet's `targetType`, or a subtype outside its `targetSubTypes`. The default code when the facet names no `errorCode`.",
"ERR_RESERVED_ATTR": "An @-prefixed reserved structural keyword (e.g. @name, @isArray, @children) was used as an inline attribute.",
"ERR_SCOPE_PATTERN_INVALID": "Phase-1 metadata-source-resolution: a scope include/exclude package pattern is malformed (empty pattern or empty :: segment).",
"ERR_SOURCE_NO_PRIMARY": "An object declares source nodes but none has role=primary.",
Expand Down
2 changes: 1 addition & 1 deletion server/typescript/packages/metadata/src/core-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ function registerCoreTypeDefs(registry: TypeRegistry): void {
{
attr: IDENTITY_REFERENCE_ATTR_REFERENCES,
targetType: TYPE_OBJECT,
dottedFieldPath: true,
fieldPath: true,
errorCode: "ERR_INVALID_REFERENCE",
},
];
Expand Down
4 changes: 4 additions & 0 deletions server/typescript/packages/metadata/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ export const ERROR_CODES = [
// package-locally (referrer's package, else root-level); FQN refs resolve
// exactly — a bare cross-package ref no longer binds elsewhere.
"ERR_UNRESOLVED_OBJECT_REF",
// spec/metamodel-definitions.md — the reference facet's default codes: the value
// resolves to nothing, or to a node of the wrong type or subtype.
"ERR_REF_UNRESOLVED",
"ERR_REF_WRONG_KIND",
// Source v2 (ADR-0007) error codes — enforcement added during the source-v2 rollout.
"ERR_RESERVED_ATTR",
"ERR_SOURCE_NO_PRIMARY",
Expand Down
13 changes: 11 additions & 2 deletions server/typescript/packages/metadata/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ export {
METAMODEL_TYPE_PAGE_REF,
} from "./metamodel-docs/index.js";
export type { MetamodelDocsPage } from "./metamodel-docs/index.js";
export type { RegistryManifest, ManifestType, ManifestAttr, ManifestChild } from "./registry-manifest.js";
export type { RegistryManifest, ManifestType, ManifestAttr, ManifestChild, ManifestRef } from "./registry-manifest.js";

// Registry coverage (SP-G Unit 5) — untested-vocabulary report (manifest vs
// fixture corpora). NODE-ONLY: registry-coverage.ts statically imports node:fs
Expand All @@ -341,7 +341,16 @@ export type { MetaDataTypeProvider } from "./provider.js";
export { applyProviderDefinition } from "./provider-data.js";
export type { ProviderDefinition, TypeDef, ChildDef, ExtendDef, FactoryMap, ValidatorMap } from "./provider-data.js";
// The validation hook a provider's TypeDefinition may carry (`validate`).
export type { NodeValidator, ValidationContext, SymbolTable, LoaderCode } from "./validation-types.js";
export type {
NodeValidator,
ValidationContext,
SymbolTable,
LoaderCode,
ReferenceFacet,
ReferenceDescriptor,
ReferenceMatch,
} from "./validation-types.js";
export { REF_UNRESOLVED_CODE, REF_WRONG_KIND_CODE } from "./validation-types.js";

// DataType classification
export {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,7 @@ export class MetaDataLoader {

// ADR-0059 R5 — after the last validation pass, before freeze: resolve every
// reference once, record its target and rewrite it to its canonical spelling.
resolveReferences(root);
resolveReferences(root, this._registry);
}

// If nothing parsed successfully, synthesize an empty root so callers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ import {
} from "../naming-refs.js";
import { resolveSuperRef } from "../super-resolve.js";
import { dimensionViaTerminal } from "./reporting-validation.js";
import type { TypeRegistry } from "../registry.js";
import type { ReferenceFacet } from "../validation-types.js";
import { CHILD_REF_SEPARATOR } from "../shared/structural.js";
import { SymbolTableImpl } from "./symbol-table.js";

/** One resolved reference: its owner object and the canonical spelling. */
interface Resolved {
Expand Down Expand Up @@ -73,12 +77,47 @@ function referrerPackage(root: MetaData, node: MetaData, attr: string, declPkg:
return declPkg;
}

/** Resolves one reference value of `attr` on `node`, or undefined when it names nothing. */
type ResolveValue = (value: string) => Resolved | undefined;

/** A facet reference (spec/metamodel-definitions.md, rule 7): the target the typed
* lookup finds, spelled as the top-level owner it was reached through plus the child
* names walked. A `fieldPath` value keeps its field segment. A spelling that would not
* look up the same target again keeps the value as written. */
function facetResolver(
symbols: SymbolTableImpl,
node: MetaData,
facet: ReferenceFacet,
declPkg: string,
): ResolveValue {
return (value) => {
const path = symbols.lookup(node, value, facet, declPkg).path;
if (path === undefined) return undefined;
const target = path[path.length - 1]!;
const tail =
facet.fieldPath === true
? splitChildTail(value).tail
: path
.slice(1)
.map((n) => `${CHILD_REF_SEPARATOR}${n.name}`)
.join("");
const canonical = spellRef(path[0]!, declPkg, tail);
const again = symbols.lookup(node, canonical, facet, declPkg).path;
return { target, canonical: again?.[again.length - 1] === target ? canonical : value };
};
}

function resolveAttr(root: MetaData, node: MetaData, attr: MetaAttr, declPkg: string): void {
const value = attr.value;
const refPkg = referrerPackage(root, node, attr.name, declPkg);
bindAttr(node, attr, (v) => resolveOne(root, v, refPkg, declPkg));
}

/** Record each resolved value's target on `attr` and rewrite it to its canonical spelling. */
function bindAttr(node: MetaData, attr: MetaAttr, resolveValue: ResolveValue): void {
const value = attr.value;
if (typeof value === "string") {
if (value === "") return;
const r = resolveOne(root, value, refPkg, declPkg);
const r = resolveValue(value);
if (r === undefined) return;
attr.resolvedTarget = r.target;
if (r.canonical !== value) {
Expand All @@ -89,7 +128,7 @@ function resolveAttr(root: MetaData, node: MetaData, attr: MetaAttr, declPkg: st
}
if (Array.isArray(value) && value.every((v) => typeof v === "string")) {
const items = value as readonly string[];
const resolved = items.map((v) => (v === "" ? undefined : resolveOne(root, v, refPkg, declPkg)));
const resolved = items.map((v) => (v === "" ? undefined : resolveValue(v)));
if (resolved.every((r) => r === undefined)) return;
attr.resolvedTarget = resolved.map((r) => r?.target);
const canonical: AttrValue = items.map((v, i) => resolved[i]?.canonical ?? v);
Expand Down Expand Up @@ -122,9 +161,19 @@ function resolveExtends(root: MetaData, node: MetaData): void {
/**
* Resolve every own reference attribute (ADR-0059 R4) and every `extends` in the
* tree, record the targets, and rewrite resolved values to their canonical spelling.
* Must run after the last validation pass and before `freeze()`.
* With `registry`, an attribute whose schema carries the reference facet is resolved
* too, by the facet's typed lookup. Must run after the last validation pass and before
* `freeze()`.
*/
export function resolveReferences(root: MetaData): void {
export function resolveReferences(root: MetaData, registry?: TypeRegistry): void {
let symbols: SymbolTableImpl | undefined;
const facetOf = (node: MetaData, name: string): ReferenceFacet | undefined => {
if (registry === undefined) return undefined;
const schema =
registry.find(node.type, node.subType)?.attributes.find((a) => a.name === name) ??
registry.getCommonAttrs().find((a) => a.name === name);
return schema?.ref;
};
const visit = (node: MetaData): void => {
if (node !== root) {
resolveExtends(root, node);
Expand All @@ -134,7 +183,13 @@ export function resolveReferences(root: MetaData): void {
// ADR-0039: own — this pass rewrites the declared-here layer; an inherited
// attribute is resolved once, on the node that declares it.
for (const attr of node.ownMetaAttrs()) {
if (isRefBearingAttr(node, attr.name)) resolveAttr(root, node, attr, declPkg);
const facet = facetOf(node, attr.name);
if (facet !== undefined) {
symbols ??= SymbolTableImpl.build(root);
bindAttr(node, attr, facetResolver(symbols, node, facet, declPkg));
} else if (isRefBearingAttr(node, attr.name)) {
resolveAttr(root, node, attr, declPkg);
}
}
}
// ADR-0039: own — structural walk over the physical declaration tree.
Expand Down
Loading
Loading