Skip to content

Commit 56d2616

Browse files
committed
ref: Split resolve into resolve(Identifier) and resolveType(TypeIdentifier)
- resolve(id: Identifier) returns TypeSchema only, no nested - resolveType(id: TypeIdentifier) returns TypeSchema | NestedTypeSchema - nestedIndex properly typed as Record<..., NestedTypeSchema> - Callers passing TypeIdentifier migrated to resolveType
1 parent b895e29 commit 56d2616

6 files changed

Lines changed: 32 additions & 11 deletions

File tree

src/api/mustache/generator/ViewModelFactory.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ export class ViewModelFactory {
100100
cache: ViewModelCache,
101101
nestedIn?: TypeSchema,
102102
): TypeViewModel {
103-
const type = this.tsIndex.resolve(typeRef);
103+
const type = this.tsIndex.resolveType(typeRef);
104104
if (!type) {
105105
throw new Error(`ComplexType ${typeRef.name} not found`);
106106
}
@@ -113,7 +113,7 @@ export class ViewModelFactory {
113113
}
114114

115115
private _createForResource(typeRef: TypeIdentifier, cache: ViewModelCache, nestedIn?: TypeSchema): TypeViewModel {
116-
const type = this.tsIndex.resolve(typeRef);
116+
const type = this.tsIndex.resolveType(typeRef);
117117
if (!type) {
118118
throw new Error(`Resource ${typeRef.name} not found`);
119119
}
@@ -126,7 +126,7 @@ export class ViewModelFactory {
126126
}
127127

128128
private _createChildrenFor(typeRef: TypeIdentifier, cache: ViewModelCache, nestedIn?: TypeSchema): TypeViewModel[] {
129-
const schema = this.tsIndex.resolve(typeRef);
129+
const schema = this.tsIndex.resolveType(typeRef);
130130
if (!schema) return [];
131131
if (isComplexTypeTypeSchema(schema)) {
132132
return (schema.typeFamily?.complexTypes ?? [])
@@ -146,7 +146,7 @@ export class ViewModelFactory {
146146
let parentRef: TypeIdentifier | undefined = "base" in base ? base.base : undefined;
147147
while (parentRef) {
148148
parents.push(this._createFor(parentRef, cache, undefined));
149-
const parent = this.tsIndex.resolve(parentRef);
149+
const parent = this.tsIndex.resolveType(parentRef);
150150
parentRef = parent && "base" in parent ? parent.base : undefined;
151151
}
152152
return parents;

src/api/writer-generator/typescript/profile-slices.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import type { TypeScript } from "./writer";
2222
/** Collect choice declaration field names from a base type schema */
2323
const collectChoiceBaseNames = (tsIndex: TypeSchemaIndex, typeId: TypeIdentifier): Set<string> => {
2424
const names = new Set<string>();
25-
const schema = tsIndex.resolve(typeId);
25+
const schema = tsIndex.resolveType(typeId);
2626
if (schema && "fields" in schema && schema.fields) {
2727
for (const [name, f] of Object.entries(schema.fields)) {
2828
if (isChoiceDeclarationField(f)) names.add(name);

src/api/writer-generator/typescript/profile.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ const collectBaseRequiredParams = (
168168
coveredNames: string[],
169169
) => {
170170
const covered = new Set(coveredNames);
171-
const baseSchema = tsIndex.resolve(flatProfile.base);
171+
const baseSchema = tsIndex.resolveType(flatProfile.base);
172172
if (!baseSchema || !("fields" in baseSchema) || !baseSchema.fields) return;
173173
for (const [name, field] of Object.entries(baseSchema.fields)) {
174174
if (covered.has(name)) continue;

src/api/writer-generator/typescript/writer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ export class TypeScript extends Writer<TypeScriptOptions> {
213213
const typeFamilyFields: { fieldName: string; familyTypeName: string }[] = [];
214214
for (const [fieldName, field] of Object.entries(schema.fields ?? {})) {
215215
if (isChoiceDeclarationField(field) || !field.type) continue;
216-
const fieldTypeSchema = tsIndex.resolve(field.type);
216+
const fieldTypeSchema = tsIndex.resolveType(field.type);
217217
if (
218218
isSpecializationTypeSchema(fieldTypeSchema) &&
219219
(fieldTypeSchema.typeFamily?.resources?.length ?? 0) > 0

src/typeschema/ir/tree-shake.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ export const treeShake = (tsIndex: TypeSchemaIndex, treeShake: TreeShakeConf): T
259259
if (isSpecializationTypeSchema(schema) || isProfileTypeSchema(schema)) {
260260
if (!schema.dependencies) continue;
261261
schema.dependencies.forEach((dep) => {
262+
if (isNestedIdentifier(dep)) return;
262263
const depSchema = tsIndex.resolve(dep);
263264
if (!depSchema)
264265
throw new Error(

src/typeschema/utils.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ import {
1010
type ComplexTypeTypeSchema,
1111
type ConstrainedChoiceInfo,
1212
type Field,
13+
type Identifier,
1314
isChoiceDeclarationField,
1415
isChoiceInstanceField,
1516
isComplexTypeIdentifier,
1617
isComplexTypeTypeSchema,
1718
isLogicalTypeSchema,
19+
isNestedIdentifier,
1820
isProfileTypeSchema,
1921
isResourceIdentifier,
2022
isResourceTypeSchema,
@@ -159,7 +161,8 @@ export type TypeSchemaIndex = {
159161
collectResources: () => ResourceTypeSchema[];
160162
collectLogicalModels: () => LogicalTypeSchema[];
161163
collectProfiles: () => ProfileTypeSchema[];
162-
resolve: (id: TypeIdentifier) => TypeSchema | undefined;
164+
resolve: (id: Identifier) => TypeSchema | undefined;
165+
resolveType: (id: TypeIdentifier) => TypeSchema | NestedTypeSchema | undefined;
163166
resolveByUrl: (pkgName: PkgName, url: CanonicalUrl) => TypeSchema | undefined;
164167
tryHierarchy: (schema: TypeSchema) => TypeSchema[] | undefined;
165168
hierarchy: (schema: TypeSchema) => TypeSchema[];
@@ -223,11 +226,14 @@ export const mkTypeSchemaIndex = (
223226
}
224227
populateTypeFamily(schemas);
225228

226-
const resolve = (id: TypeIdentifier): TypeSchema | undefined => {
227-
if (id.kind === "nested") return nestedIndex[id.url]?.[id.package] as unknown as TypeSchema | undefined;
229+
const resolve = (id: Identifier): TypeSchema | undefined => {
228230
return index[id.url]?.[id.package];
229231
};
230-
const resolveByUrl = (pkgName: PkgName, url: CanonicalUrl) => {
232+
const resolveType = (id: TypeIdentifier): TypeSchema | NestedTypeSchema | undefined => {
233+
if (isNestedIdentifier(id)) return nestedIndex[id.url]?.[id.package];
234+
return index[id.url]?.[id.package];
235+
};
236+
const resolveByUrl = (pkgName: PkgName, url: CanonicalUrl): TypeSchema | undefined => {
231237
if (register) {
232238
const resolutionTree = register.resolutionTree();
233239
const resolution = resolutionTree[pkgName]?.[url]?.[0];
@@ -247,6 +253,13 @@ export const mkTypeSchemaIndex = (
247253
return index[url]?.[anyPkg];
248254
}
249255
}
256+
if (nestedIndex[url]) {
257+
const anyPkg = Object.keys(nestedIndex[url])[0];
258+
if (anyPkg) {
259+
logger?.dryWarn(`Type '${url}' fallback to package ${anyPkg}`);
260+
return nestedIndex[url]?.[anyPkg] as unknown as TypeSchema;
261+
}
262+
}
250263
return undefined;
251264
};
252265

@@ -257,6 +270,7 @@ export const mkTypeSchemaIndex = (
257270
res.push(cur);
258271
const base = (cur as SpecializationTypeSchema).base;
259272
if (base === undefined) break;
273+
if (isNestedIdentifier(base)) break;
260274
const resolved = resolve(base);
261275
if (!resolved) {
262276
logger?.warn(
@@ -287,6 +301,11 @@ export const mkTypeSchemaIndex = (
287301
};
288302

289303
const findLastSpecializationByIdentifier = (id: TypeIdentifier): TypeIdentifier => {
304+
if (isNestedIdentifier(id)) {
305+
const resolved = resolveType(id);
306+
if (!resolved || !("base" in resolved) || !resolved.base) return id;
307+
return findLastSpecializationByIdentifier(resolved.base);
308+
}
290309
const schema = resolve(id);
291310
if (!schema) return id;
292311
return findLastSpecialization(schema).identifier;
@@ -461,6 +480,7 @@ export const mkTypeSchemaIndex = (
461480
collectLogicalModels: () => schemas.filter(isLogicalTypeSchema),
462481
collectProfiles: () => schemas.filter(isProfileTypeSchema),
463482
resolve,
483+
resolveType,
464484
resolveByUrl,
465485
tryHierarchy,
466486
hierarchy,

0 commit comments

Comments
 (0)