|
| 1 | +// The NEUTRAL, structural codegen template data dict (SP-1 §3.2). Distinct from |
| 2 | +// the Markdown-flavored EntityDocData — this carries raw structural facts only, |
| 3 | +// so a consumer's Mustache template can emit any language's code from it. The |
| 4 | +// field names here are a byte-gated cross-port contract; change them only via the |
| 5 | +// spec (and the conformance corpus). |
| 6 | +import type { MetaObject, MetaRoot, MetaField } from "@metaobjectsdev/metadata"; |
| 7 | +import { FIELD_ATTR_VALUES, FIELD_SUBTYPE_ENUM } from "@metaobjectsdev/metadata"; |
| 8 | + |
| 9 | +export interface FieldTemplateData { |
| 10 | + name: string; |
| 11 | + /** Neutral field subtype, e.g. "string" | "int" | "currency" | "enum". |
| 12 | + * Arrayness is carried by `isArray`, NOT appended here. */ |
| 13 | + type: string; |
| 14 | + required: boolean; |
| 15 | + isArray: boolean; |
| 16 | + maxLength?: number; |
| 17 | + enumValues?: string[]; |
| 18 | +} |
| 19 | +export interface IdentityTemplateData { kind: string; fields: string[]; } |
| 20 | +export interface RelationshipTemplateData { name: string; cardinality: string; targetRef: string; } |
| 21 | +export interface EntityTemplateData { |
| 22 | + name: string; |
| 23 | + package: string; |
| 24 | + fields: FieldTemplateData[]; |
| 25 | + identities: IdentityTemplateData[]; |
| 26 | + relationships: RelationshipTemplateData[]; |
| 27 | +} |
| 28 | +export interface PackageTemplateData { package: string; entities: EntityTemplateData[]; } |
| 29 | +export interface ModelTemplateData { packages: PackageTemplateData[]; } |
| 30 | + |
| 31 | +function fieldData(field: MetaField): FieldTemplateData { |
| 32 | + const d: FieldTemplateData = { |
| 33 | + name: field.name, |
| 34 | + type: field.subType, |
| 35 | + required: field.isRequired, |
| 36 | + isArray: field.isArray === true, |
| 37 | + }; |
| 38 | + if (typeof field.maxLength === "number") d.maxLength = field.maxLength; |
| 39 | + if (field.subType === FIELD_SUBTYPE_ENUM) { |
| 40 | + const vals = field.attr(FIELD_ATTR_VALUES); |
| 41 | + if (Array.isArray(vals)) d.enumValues = vals.map((v) => String(v)); |
| 42 | + } |
| 43 | + return d; |
| 44 | +} |
| 45 | + |
| 46 | +export function buildEntityTemplateData(entity: MetaObject): EntityTemplateData { |
| 47 | + return { |
| 48 | + name: entity.name, |
| 49 | + package: entity.package ?? "", |
| 50 | + fields: entity.fields().map(fieldData), |
| 51 | + identities: entity.identities().map((i) => ({ kind: i.subType, fields: [...i.fields] })), |
| 52 | + relationships: entity.relationships().map((r) => ({ |
| 53 | + name: r.name, |
| 54 | + cardinality: r.cardinality ?? "", |
| 55 | + targetRef: r.objectRef ?? "", |
| 56 | + })), |
| 57 | + }; |
| 58 | +} |
| 59 | + |
| 60 | +export function buildPackageTemplateData(pkg: string, entities: MetaObject[]): PackageTemplateData { |
| 61 | + return { package: pkg, entities: entities.map(buildEntityTemplateData) }; |
| 62 | +} |
| 63 | + |
| 64 | +/** Groups concrete (non-abstract) objects by package — packages ascending, |
| 65 | + * entities in `root.objects()` order. Abstract objects never emit instance |
| 66 | + * artifacts, so they are excluded. */ |
| 67 | +export function buildModelTemplateData(root: MetaRoot): ModelTemplateData { |
| 68 | + const concrete = root.objects().filter((o) => o.isAbstract !== true); |
| 69 | + const byPkg = new Map<string, MetaObject[]>(); |
| 70 | + for (const o of concrete) { |
| 71 | + const pkg = o.package ?? ""; |
| 72 | + let bucket = byPkg.get(pkg); |
| 73 | + if (bucket === undefined) { bucket = []; byPkg.set(pkg, bucket); } |
| 74 | + bucket.push(o); |
| 75 | + } |
| 76 | + return { |
| 77 | + packages: [...byPkg.keys()].sort().map((pkg) => buildPackageTemplateData(pkg, byPkg.get(pkg)!)), |
| 78 | + }; |
| 79 | +} |
0 commit comments