Skip to content

Commit e6b8f32

Browse files
committed
add package
1 parent 6043ff3 commit e6b8f32

File tree

13 files changed

+330
-157
lines changed

13 files changed

+330
-157
lines changed

projects/angular-odata/schematics/apigen/angular/api-config.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ import { strings } from '@angular-devkit/core';
22
import { Base } from './base';
33
import { url, Source } from '@angular-devkit/schematics';
44
import { Schema as ApiGenSchema } from '../schema';
5+
import { Package } from './package';
56

67
export class ApiConfig extends Base {
7-
constructor(options: ApiGenSchema) {
8-
super(options);
8+
constructor(pkg: Package, options: ApiGenSchema) {
9+
super(pkg, options);
910
}
1011
public override template(): Source {
1112
return url('./files/api-config');

projects/angular-odata/schematics/apigen/angular/base.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
} from '../metadata/csdl/csdl-function-action';
1212
import { makeRelativePath, toTypescriptType } from '../utils';
1313
import { ODataMetadata } from '../metadata';
14+
import { Package } from './package';
1415

1516
export class Callable {
1617
callables: CsdlCallable[] = [];
@@ -154,7 +155,9 @@ export class Callable {
154155
}
155156

156157
export abstract class Base {
157-
constructor(protected options: ApiGenSchema) {}
158+
constructor(
159+
protected pkg: Package,
160+
protected options: ApiGenSchema) {}
158161

159162
public abstract name(): string;
160163
public abstract fileName(): string;
@@ -218,11 +221,15 @@ export abstract class Base {
218221
public addCallables(callables: Callable[]) {
219222
callables.forEach((r) => this.addCallable(r));
220223
}
224+
225+
public getPackage() {
226+
return this.pkg;
227+
}
221228
}
222229

223230
export class Index extends Base {
224-
constructor(options: ApiGenSchema) {
225-
super(options);
231+
constructor(pkg: Package, options: ApiGenSchema) {
232+
super(pkg, options);
226233
}
227234
public override template(): Source {
228235
return url('./files/index');
@@ -249,10 +256,11 @@ export class Index extends Base {
249256

250257
export class Metadata extends Base {
251258
constructor(
259+
pkg: Package,
252260
options: ApiGenSchema,
253261
private meta: ODataMetadata,
254262
) {
255-
super(options);
263+
super(pkg, options);
256264
}
257265
public override template(): Source {
258266
return url('./files/metadata');
@@ -275,4 +283,16 @@ export class Metadata extends Base {
275283
public override importTypes(): string[] {
276284
return [];
277285
}
286+
287+
findEnumType(fullName: string) {
288+
return this.meta.findEnumType(fullName);
289+
}
290+
291+
findEntityType(fullName: string) {
292+
return this.meta.findEntityType(fullName);
293+
}
294+
295+
findComplexType(fullName: string) {
296+
return this.meta.findComplexType(fullName);
297+
}
278298
}

projects/angular-odata/schematics/apigen/angular/collection.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@ import { url, Source } from '@angular-devkit/schematics';
55
import { Schema as ApiGenSchema } from '../schema';
66
import { Model } from './model';
77
import { Entity } from './entity';
8+
import { Package } from './package';
89

910
export class Collection extends Base {
1011
constructor(
12+
pkg: Package,
1113
options: ApiGenSchema,
1214
protected edmType: CsdlEntityType | CsdlComplexType,
1315
protected entity: Entity,
1416
protected model: Model
1517
) {
16-
super(options);
18+
super(pkg, options);
1719
}
1820

1921
public entityType() {

projects/angular-odata/schematics/apigen/angular/entity.ts

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,44 @@ import { url, Source } from '@angular-devkit/schematics';
55
import { Schema as ApiGenSchema } from '../schema';
66
import { CsdlNavigationProperty, CsdlProperty } from '../metadata/csdl/csdl-structural-property';
77
import { toTypescriptType } from '../utils';
8+
import { Package } from './package';
89

910
export class EntityProperty {
10-
constructor(protected edmType: CsdlProperty | CsdlNavigationProperty) {}
11+
constructor(protected entity: Entity, protected edmType: CsdlProperty | CsdlNavigationProperty) {}
1112

1213
name() {
13-
return this.edmType.Name;
14+
const required = !(this.edmType instanceof CsdlNavigationProperty || this.edmType.Nullable);
15+
const name = this.edmType.Name;
16+
return name + (!required ? '?' : '');
1417
}
1518

1619
type() {
17-
let type = toTypescriptType(this.edmType.Type);
18-
type += this.edmType.Collection ? '[]' : '';
19-
type += this.edmType.Nullable ? ' | null' : '';
20+
const pkg = this.entity.getPackage();
21+
const enumType = pkg.findEnum(this.edmType.Type);
22+
const entityType = pkg.findEntity(this.edmType.Type);
23+
let type = "any";
24+
if (enumType !== undefined)
25+
{
26+
type = enumType.importedName!;
27+
type += this.edmType.Collection ? '[]' : '';
28+
} else if (entityType !== undefined) {
29+
type = entityType.importedName!;
30+
type += this.edmType.Collection ? '[]' : '';
31+
} else {
32+
type = toTypescriptType(this.edmType.Type);
33+
type += this.edmType.Collection ? '[]' : '';
34+
}
2035
return type;
2136
}
2237
}
2338

2439
export class Entity extends Base {
2540
constructor(
41+
pkg: Package,
2642
options: ApiGenSchema,
2743
protected edmType: CsdlEntityType | CsdlComplexType,
2844
) {
29-
super(options);
45+
super(pkg, options);
3046
}
3147

3248
public override template(): Source {
@@ -37,8 +53,8 @@ export class Entity extends Base {
3753
type: this.name() + (this.edmType instanceof CsdlEntityType ? 'EntityType' : 'ComplexType'),
3854
baseType: this.edmType.BaseType,
3955
properties: [
40-
...(this.edmType.Property ?? []).map((p) => new EntityProperty(p)),
41-
...(this.edmType.NavigationProperty ?? []).map((p) => new EntityProperty(p)),
56+
...(this.edmType.Property ?? []).map((p) => new EntityProperty(this, p)),
57+
...(this.edmType.NavigationProperty ?? []).map((p) => new EntityProperty(this, p)),
4258
],
4359
};
4460
}
@@ -63,12 +79,12 @@ export class Entity extends Base {
6379
imports.push(this.edmType.BaseType);
6480
}
6581
for (let prop of this.edmType?.Property ?? []) {
66-
if (!prop.Type.startsWith('Edm.')) {
82+
if (!prop.isEdmType()) {
6783
imports.push(prop.Type);
6884
}
6985
}
7086
for (let prop of this.edmType?.NavigationProperty ?? []) {
71-
if (!prop.Type.startsWith('Edm.')) {
87+
if (!prop.isEdmType()) {
7288
imports.push(prop.Type);
7389
}
7490
}

projects/angular-odata/schematics/apigen/angular/enum.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Base } from './base';
44
import { Import } from './import';
55
import { url, Source } from '@angular-devkit/schematics';
66
import { Schema as ApiGenSchema } from '../schema';
7+
import { Package } from './package';
78

89
export class EnumValue {
910
constructor(private edmType: CsdlMember) {}
@@ -16,10 +17,11 @@ export class EnumValue {
1617
}
1718
export class Enum extends Base {
1819
constructor(
20+
pkg: Package,
1921
options: ApiGenSchema,
2022
protected edmType: CsdlEnumType,
2123
) {
22-
super(options);
24+
super(pkg, options);
2325
}
2426
public override template(): Source {
2527
return url('./files/enum');

projects/angular-odata/schematics/apigen/angular/model.ts

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,39 @@ import { Schema as ApiGenSchema } from '../schema';
66
import { CsdlNavigationProperty, CsdlProperty } from '../metadata/csdl/csdl-structural-property';
77
import { toTypescriptType } from '../utils';
88
import { Entity } from './entity';
9+
import { Package } from './package';
910

1011
export class ModelField {
11-
constructor(protected edmType: CsdlProperty | CsdlNavigationProperty) {}
12+
constructor(protected model: Model, protected edmType: CsdlProperty | CsdlNavigationProperty) {}
1213

1314
name() {
14-
return this.edmType.Name + (this.edmType.Nullable ? '?' : '!');
15+
const required = !(this.edmType instanceof CsdlNavigationProperty || this.edmType.Nullable);
16+
const name = this.edmType.Name;
17+
return name + (!required ? '?' : '');
1518
}
1619

1720
type() {
18-
let type = toTypescriptType(this.edmType.Type);
19-
type += this.edmType.Collection ? '[]' : '';
20-
type += this.edmType.Nullable ? ' | null' : '';
21+
const pkg = this.model.getPackage();
22+
const enumType = pkg.findEnum(this.edmType.Type);
23+
const entityType = pkg.findEntity(this.edmType.Type);
24+
let type = "any";
25+
if (enumType !== undefined)
26+
{
27+
type = enumType.importedName!;
28+
type += this.edmType.Collection ? '[]' : '';
29+
} else if (entityType !== undefined) {
30+
if (this.edmType.Collection) {
31+
const collection = pkg.findCollection(this.edmType.Type);
32+
const model = pkg.findModel(this.edmType.Type);
33+
type = `${collection!.importedName}<${entityType!.importedName}, ${model!.importedName}<${entityType!.importedName}>>`;
34+
} else {
35+
const model = pkg.findModel(this.edmType.Type);
36+
type = `${model!.importedName}<${entityType!.importedName}>`;
37+
}
38+
} else {
39+
type = toTypescriptType(this.edmType.Type);
40+
type += this.edmType.Collection ? '[]' : '';
41+
}
2142
return type;
2243
}
2344

@@ -75,11 +96,12 @@ export class ModelField {
7596

7697
export class Model extends Base {
7798
constructor(
99+
pkg: Package,
78100
options: ApiGenSchema,
79101
protected edmType: CsdlEntityType | CsdlComplexType,
80102
protected entity: Entity
81103
) {
82-
super(options);
104+
super(pkg, options);
83105
}
84106

85107
public entityType() {
@@ -95,8 +117,8 @@ export class Model extends Base {
95117
baseType: this.edmType.BaseType ? this.edmType.BaseType + 'Model' : null,
96118
entity: this.entity,
97119
fields: [
98-
...(this.edmType.Property ?? []).map((p) => new ModelField(p)),
99-
...(this.edmType.NavigationProperty ?? []).map((p) => new ModelField(p)),
120+
...(this.edmType.Property ?? []).map((p) => new ModelField(this, p)),
121+
...(this.edmType.NavigationProperty ?? []).map((p) => new ModelField(this, p)),
100122
],
101123
actions: [], // To be implemented
102124
functions: [], // To be implemented

projects/angular-odata/schematics/apigen/angular/module.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ import { Base } from './base';
33
import { url, Source } from '@angular-devkit/schematics';
44
import { Schema as ApiGenSchema } from '../schema';
55
import { Service } from './service';
6+
import { Package } from './package';
67

78
export class Module extends Base {
89
services: Service[] = [];
9-
constructor(options: ApiGenSchema) {
10-
super(options);
10+
constructor(protected pkg: Package, options: ApiGenSchema) {
11+
super(pkg, options);
1112
}
1213
public override template(): Source {
1314
return url('./files/module');

0 commit comments

Comments
 (0)