@@ -15,12 +15,12 @@ import type { IsPrefixed } from "@root/utils/types";
1515import {
1616 type ChoiceFieldInstance ,
1717 type Field ,
18- type Identifier ,
1918 isComplexTypeIdentifier ,
2019 isNotChoiceDeclarationField ,
2120 isResourceIdentifier ,
22- type NestedType ,
21+ type NestedTypeSchema ,
2322 type RegularField ,
23+ type TypeIdentifier ,
2424 type TypeSchema ,
2525} from "@typeschema/types" ;
2626
@@ -35,15 +35,15 @@ export class ViewModelFactory {
3535 constructor (
3636 private readonly tsIndex : TypeSchemaIndex ,
3737 private readonly nameGenerator : NameGenerator ,
38- private readonly filterPred : ( id : Identifier ) => boolean ,
38+ private readonly filterPred : ( id : TypeIdentifier ) => boolean ,
3939 ) { }
4040
4141 public createUtility ( ) : RootViewModel < ViewModel > {
4242 return this . _createForRoot ( ) ;
4343 }
4444
4545 public createComplexType (
46- typeRef : Identifier ,
46+ typeRef : TypeIdentifier ,
4747 cache : ViewModelCache = { resourcesByUri : { } , complexTypesByUri : { } } ,
4848 ) : RootViewModel < ResolvedTypeViewModel > {
4949 const base = this . _createForComplexType ( typeRef , cache ) ;
@@ -64,7 +64,7 @@ export class ViewModelFactory {
6464 } ) ;
6565 }
6666 public createResource (
67- typeRef : Identifier ,
67+ typeRef : TypeIdentifier ,
6868 cache : ViewModelCache = { resourcesByUri : { } , complexTypesByUri : { } } ,
6969 ) : RootViewModel < ResolvedTypeViewModel > {
7070 const base = this . _createForResource ( typeRef , cache ) ;
@@ -85,7 +85,7 @@ export class ViewModelFactory {
8585 } ) ;
8686 }
8787
88- private _createFor ( typeRef : Identifier , cache : ViewModelCache , nestedIn ?: TypeSchema ) : TypeViewModel {
88+ private _createFor ( typeRef : TypeIdentifier , cache : ViewModelCache , nestedIn ?: TypeSchema ) : TypeViewModel {
8989 if ( typeRef . kind === "complex-type" ) {
9090 return this . _createForComplexType ( typeRef , cache , nestedIn ) ;
9191 }
@@ -95,7 +95,11 @@ export class ViewModelFactory {
9595 throw new Error ( `Unknown type ${ typeRef . kind } ` ) ;
9696 }
9797
98- private _createForComplexType ( typeRef : Identifier , cache : ViewModelCache , nestedIn ?: TypeSchema ) : TypeViewModel {
98+ private _createForComplexType (
99+ typeRef : TypeIdentifier ,
100+ cache : ViewModelCache ,
101+ nestedIn ?: TypeSchema ,
102+ ) : TypeViewModel {
99103 const type = this . tsIndex . resolve ( typeRef ) ;
100104 if ( ! type ) {
101105 throw new Error ( `ComplexType ${ typeRef . name } not found` ) ;
@@ -108,7 +112,7 @@ export class ViewModelFactory {
108112 return res ;
109113 }
110114
111- private _createForResource ( typeRef : Identifier , cache : ViewModelCache , nestedIn ?: TypeSchema ) : TypeViewModel {
115+ private _createForResource ( typeRef : TypeIdentifier , cache : ViewModelCache , nestedIn ?: TypeSchema ) : TypeViewModel {
112116 const type = this . tsIndex . resolve ( typeRef ) ;
113117 if ( ! type ) {
114118 throw new Error ( `Resource ${ typeRef . name } not found` ) ;
@@ -121,25 +125,25 @@ export class ViewModelFactory {
121125 return res ;
122126 }
123127
124- private _createChildrenFor ( typeRef : Identifier , cache : ViewModelCache , nestedIn ?: TypeSchema ) : TypeViewModel [ ] {
128+ private _createChildrenFor ( typeRef : TypeIdentifier , cache : ViewModelCache , nestedIn ?: TypeSchema ) : TypeViewModel [ ] {
125129 const schema = this . tsIndex . resolve ( typeRef ) ;
126130 if ( ! schema || ! ( "typeFamily" in schema ) ) return [ ] ;
127131 if ( isComplexTypeIdentifier ( typeRef ) ) {
128132 return ( schema . typeFamily ?. complexTypes ?? [ ] )
129133 . filter ( this . filterPred )
130- . map ( ( childRef : Identifier ) => this . _createFor ( childRef , cache , nestedIn ) ) ;
134+ . map ( ( childRef : TypeIdentifier ) => this . _createFor ( childRef , cache , nestedIn ) ) ;
131135 }
132136 if ( isResourceIdentifier ( typeRef ) ) {
133137 return ( schema . typeFamily ?. resources ?? [ ] )
134138 . filter ( this . filterPred )
135- . map ( ( childRef : Identifier ) => this . _createFor ( childRef , cache , nestedIn ) ) ;
139+ . map ( ( childRef : TypeIdentifier ) => this . _createFor ( childRef , cache , nestedIn ) ) ;
136140 }
137141 return [ ] ;
138142 }
139143
140- private _createParentsFor ( base : TypeSchema | NestedType , cache : ViewModelCache ) {
144+ private _createParentsFor ( base : TypeSchema | NestedTypeSchema , cache : ViewModelCache ) {
141145 const parents : TypeViewModel [ ] = [ ] ;
142- let parentRef : Identifier | undefined = "base" in base ? base . base : undefined ;
146+ let parentRef : TypeIdentifier | undefined = "base" in base ? base . base : undefined ;
143147 while ( parentRef ) {
144148 parents . push ( this . _createFor ( parentRef , cache , undefined ) ) ;
145149 const parent = this . tsIndex . resolve ( parentRef ) ;
@@ -149,7 +153,7 @@ export class ViewModelFactory {
149153 }
150154
151155 private _createForNestedType (
152- nested : NestedType ,
156+ nested : NestedTypeSchema ,
153157 cache : ViewModelCache ,
154158 nestedIn ?: TypeSchema ,
155159 ) : ResolvedTypeViewModel {
@@ -171,7 +175,7 @@ export class ViewModelFactory {
171175 }
172176
173177 private _createTypeViewModel (
174- schema : TypeSchema | NestedType ,
178+ schema : TypeSchema | NestedTypeSchema ,
175179 cache : ViewModelCache ,
176180 nestedIn ?: TypeSchema ,
177181 ) : TypeViewModel {
@@ -229,7 +233,7 @@ export class ViewModelFactory {
229233 } ;
230234 }
231235
232- private _collectDependencies ( schema : TypeSchema | NestedType ) : TypeViewModel [ "dependencies" ] {
236+ private _collectDependencies ( schema : TypeSchema | NestedTypeSchema ) : TypeViewModel [ "dependencies" ] {
233237 const dependencies : TypeViewModel [ "dependencies" ] = {
234238 resources : [ ] ,
235239 complexTypes : [ ] ,
@@ -273,35 +277,35 @@ export class ViewModelFactory {
273277 return dependencies ;
274278 }
275279
276- private _createIsResource ( typeRef : Identifier ) : Record < IsPrefixed < string > , boolean > | false {
280+ private _createIsResource ( typeRef : TypeIdentifier ) : Record < IsPrefixed < string > , boolean > | false {
277281 if ( typeRef . kind !== "resource" ) {
278282 return false ;
279283 }
280284 return Object . fromEntries (
281285 this . tsIndex
282286 . collectResources ( )
283287 . map ( ( e ) => e . identifier )
284- . map ( ( resourceRef : Identifier ) => [
288+ . map ( ( resourceRef : TypeIdentifier ) => [
285289 `is${ resourceRef . name . charAt ( 0 ) . toUpperCase ( ) + resourceRef . name . slice ( 1 ) } ` ,
286290 resourceRef . url === typeRef . url ,
287291 ] ) ,
288292 ) as Record < IsPrefixed < string > , boolean > ;
289293 }
290- private _createIsComplexType ( typeRef : Identifier ) : Record < IsPrefixed < string > , boolean > | false {
294+ private _createIsComplexType ( typeRef : TypeIdentifier ) : Record < IsPrefixed < string > , boolean > | false {
291295 if ( typeRef . kind !== "complex-type" && typeRef . kind !== "nested" ) {
292296 return false ;
293297 }
294298 return Object . fromEntries (
295299 this . tsIndex
296300 . collectComplexTypes ( )
297301 . map ( ( e ) => e . identifier )
298- . map ( ( complexTypeRef : Identifier ) => [
302+ . map ( ( complexTypeRef : TypeIdentifier ) => [
299303 `is${ complexTypeRef . name . charAt ( 0 ) . toUpperCase ( ) + complexTypeRef . name . slice ( 1 ) } ` ,
300304 complexTypeRef . url === typeRef . url ,
301305 ] ) ,
302306 ) as Record < IsPrefixed < string > , boolean > ;
303307 }
304- private _createIsPrimitiveType ( typeRef : Identifier ) : Record < IsPrefixed < string > , boolean > | false {
308+ private _createIsPrimitiveType ( typeRef : TypeIdentifier ) : Record < IsPrefixed < string > , boolean > | false {
305309 if ( typeRef . kind !== "primitive-type" ) {
306310 return false ;
307311 }
@@ -310,7 +314,10 @@ export class ViewModelFactory {
310314 ) as FieldViewModel [ "isPrimitive" ] ;
311315 }
312316
313- private _collectNestedComplex ( schema : TypeSchema | NestedType , cache : ViewModelCache ) : ResolvedTypeViewModel [ ] {
317+ private _collectNestedComplex (
318+ schema : TypeSchema | NestedTypeSchema ,
319+ cache : ViewModelCache ,
320+ ) : ResolvedTypeViewModel [ ] {
314321 const nested : ResolvedTypeViewModel [ ] = [ ] ;
315322 if ( "nested" in schema && schema . nested ) {
316323 schema . nested
@@ -347,14 +354,14 @@ export class ViewModelFactory {
347354 complexTypes : this . tsIndex
348355 . collectComplexTypes ( )
349356 . map ( ( e ) => e . identifier )
350- . map ( ( typeRef : Identifier ) => ( {
357+ . map ( ( typeRef : TypeIdentifier ) => ( {
351358 name : typeRef . name ,
352359 saveName : this . nameGenerator . generateType ( typeRef ) ,
353360 } ) ) ,
354361 resources : this . tsIndex
355362 . collectResources ( )
356363 . map ( ( e ) => e . identifier )
357- . map ( ( typeRef : Identifier ) => ( {
364+ . map ( ( typeRef : TypeIdentifier ) => ( {
358365 name : typeRef . name ,
359366 saveName : this . nameGenerator . generateType ( typeRef ) ,
360367 } ) ) ,
0 commit comments