Skip to content

Commit 5f0f648

Browse files
committed
wip
1 parent 6382608 commit 5f0f648

5 files changed

Lines changed: 51 additions & 66 deletions

File tree

src/api/mustache/generator/SchemaLoaderFacade.ts

Lines changed: 29 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,14 @@
1-
import type { FilterType } from "@mustache/types";
1+
import type { MustacheFilter } from "@mustache/types";
22
import type { TypeSchemaIndex } from "@root/typeschema/utils";
3-
import type { Identifier, RegularTypeSchema, TypeSchema } from "@typeschema/types";
3+
import { type Identifier, isComplexTypeIdentifier, isResourceIdentifier, type TypeSchema } from "@typeschema/types";
44

55
export class SchemaLoaderFacade {
6-
private readonly complexTypesByUri: Record<string, TypeSchema> = {};
7-
private readonly resourcesByUri: Record<string, TypeSchema> = {};
6+
private readonly tsIndex: TypeSchemaIndex;
7+
private readonly filters: MustacheFilter;
88

9-
private readonly childResourceUrisByParentUri: Record<string, string[]> = {};
10-
private readonly childComplexTypeUrisByParentUri: Record<string, string[]> = {};
11-
12-
private complexTypeRefs: Identifier[] | undefined = undefined;
13-
private resourceRefs: Identifier[] | undefined = undefined;
14-
15-
constructor(
16-
private readonly tsIndex: TypeSchemaIndex,
17-
private readonly filters: { resource?: FilterType; complexType?: FilterType },
18-
) {
19-
this.tsIndex.collectComplexTypes().forEach((schema: RegularTypeSchema) => {
20-
this.complexTypesByUri[schema.identifier.url] = schema;
21-
if (schema.base) {
22-
if (!this.childComplexTypeUrisByParentUri[schema.base.url]) {
23-
this.childComplexTypeUrisByParentUri[schema.base.url] = [];
24-
}
25-
this.childComplexTypeUrisByParentUri[schema.base.url]?.push(schema.identifier.url);
26-
}
27-
});
28-
this.tsIndex.collectResources().forEach((schema: RegularTypeSchema) => {
29-
this.resourcesByUri[schema.identifier.url] = schema;
30-
if (schema.base) {
31-
if (!this.childResourceUrisByParentUri[schema.base.url]) {
32-
this.childResourceUrisByParentUri[schema.base.url] = [];
33-
}
34-
this.childResourceUrisByParentUri[schema.base.url]?.push(schema.identifier.url);
35-
}
36-
});
9+
constructor(tsIndex: TypeSchemaIndex, filters: MustacheFilter) {
10+
this.tsIndex = tsIndex;
11+
this.filters = filters;
3712
}
3813

3914
public get(typeRef: Identifier) {
@@ -47,46 +22,40 @@ export class SchemaLoaderFacade {
4722
}
4823

4924
public getComplexType(typeRef: Identifier): TypeSchema | undefined {
50-
return this.complexTypesByUri[typeRef.url];
25+
return this.tsIndex.resolve(typeRef);
5126
}
5227
public getResource(typeRef: Identifier): TypeSchema | undefined {
53-
return this.resourcesByUri[typeRef.url];
28+
return this.tsIndex.resolve(typeRef);
5429
}
5530

5631
public getChildComplexTypes(parentTypeRef: Identifier): Identifier[] {
57-
return (this.childComplexTypeUrisByParentUri[parentTypeRef.url] ?? [])
58-
.map((uri) => this.complexTypesByUri[uri])
59-
.map((t: any) => t.identifier)
60-
.filter((t: any) => this._checkFilter(t))
61-
.sort((a: any, b: any) => a.url.localeCompare(b.url));
32+
return this.tsIndex
33+
.resourceChildren(parentTypeRef)
34+
.filter(isComplexTypeIdentifier)
35+
.filter((i) => this._checkFilter(i))
36+
.sort((a, b) => a.url.localeCompare(b.url));
6237
}
6338
public getChildResources(parentTypeRef: Identifier): Identifier[] {
64-
return (this.childResourceUrisByParentUri[parentTypeRef.url] ?? [])
65-
.map((uri) => this.resourcesByUri[uri])
66-
.map((t: any) => t.identifier)
67-
.filter((t: any) => this._checkFilter(t))
68-
.sort((a: any, b: any) => a.url.localeCompare(b.url));
39+
return this.tsIndex
40+
.resourceChildren(parentTypeRef)
41+
.filter(isResourceIdentifier)
42+
.filter((i) => this._checkFilter(i))
43+
.sort((a, b) => a.url.localeCompare(b.url));
6944
}
7045

7146
public getComplexTypes(): Identifier[] {
72-
if (this.complexTypeRefs === undefined) {
73-
this.complexTypeRefs = this.tsIndex
74-
.collectComplexTypes()
75-
.map((t: any) => t.identifier)
76-
.filter((t: any) => this._checkFilter(t))
77-
.sort((a: any, b: any) => a.url.localeCompare(b.url));
78-
}
79-
return this.complexTypeRefs;
47+
return this.tsIndex
48+
.collectComplexTypes()
49+
.map((i) => i.identifier)
50+
.filter((i) => this._checkFilter(i))
51+
.sort((a, b) => a.url.localeCompare(b.url));
8052
}
8153
public getResources(): Identifier[] {
82-
if (this.resourceRefs === undefined) {
83-
this.resourceRefs = this.tsIndex
84-
.collectResources()
85-
.map((t: any) => t.identifier)
86-
.filter((t: any) => this._checkFilter(t))
87-
.sort((a: any, b: any) => a.url.localeCompare(b.url));
88-
}
89-
return this.resourceRefs;
54+
return this.tsIndex
55+
.collectResources()
56+
.map((i) => i.identifier)
57+
.filter((i) => this._checkFilter(i))
58+
.sort((a, b) => a.url.localeCompare(b.url));
9059
}
9160

9261
private _checkFilter(type: Identifier): boolean {

src/api/mustache/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ export type FieldViewModel = {
4040
isReference: boolean;
4141
};
4242

43+
export type MustacheFilter = {
44+
resource?: FilterType;
45+
complexType?: FilterType;
46+
};
47+
4348
export type FilterType = {
4449
whitelist?: (string | RegExp)[];
4550
blacklist?: (string | RegExp)[];

src/api/writer-generator/mustache.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import { TemplateFileCache } from "@mustache/generator/TemplateFileCache";
1414
import type { ViewModelCache } from "@mustache/generator/ViewModelFactory";
1515
import { ViewModelFactory } from "@mustache/generator/ViewModelFactory";
1616
import type {
17-
FilterType,
1817
HookType,
18+
MustacheFilter,
1919
NamedViewModel,
2020
PrimitiveType,
2121
Rendering,
@@ -37,10 +37,7 @@ export type FileBasedMustacheGeneratorOptions = {
3737
};
3838
keywords: string[];
3939
typeMap: Partial<Record<PrimitiveType, string>>;
40-
filters: {
41-
resource?: FilterType;
42-
complexType?: FilterType;
43-
};
40+
filters: MustacheFilter;
4441
meta: {
4542
timestamp?: string;
4643
generator?: string;

src/typeschema/types.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,18 @@ export type Identifier =
8787
| ProfileIdentifier
8888
| LogicalIdentifier;
8989

90+
export const isResourceIdentifier = (id: Identifier | undefined): id is ResourceIdentifier => {
91+
return id?.kind === "resource";
92+
};
93+
94+
export const isLogicalIdentifier = (id: Identifier | undefined): id is LogicalIdentifier => {
95+
return id?.kind === "logical";
96+
};
97+
98+
export const isComplexTypeIdentifier = (id: Identifier | undefined): id is ComplexTypeIdentifier => {
99+
return id?.kind === "complex-type";
100+
};
101+
90102
export const isPrimitiveIdentifier = (id: Identifier | undefined): id is PrimitiveIdentifier => {
91103
return id?.kind === "primitive-type";
92104
};

src/typeschema/utils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,9 @@ interface TypeRelation {
119119
}
120120

121121
const resourceRelatives = (schemas: TypeSchema[]): TypeRelation[] => {
122-
const regularSchemas = schemas.filter((e) => isResourceTypeSchema(e) || isLogicalTypeSchema(e));
122+
const regularSchemas = schemas.filter(
123+
(e) => isResourceTypeSchema(e) || isLogicalTypeSchema(e) || isComplexTypeTypeSchema(e),
124+
);
123125
const directPairs: TypeRelation[] = [];
124126

125127
for (const schema of regularSchemas) {

0 commit comments

Comments
 (0)