From 92f475c6880c77f2f134586af165a6496562da46 Mon Sep 17 00:00:00 2001 From: Arman Ozak Date: Tue, 8 Dec 2020 17:27:37 +0300 Subject: [PATCH] feat: update types and add Generic class --- .../packages/schematics/src/models/model.ts | 94 +++++++++++++++---- 1 file changed, 74 insertions(+), 20 deletions(-) diff --git a/npm/ng-packs/packages/schematics/src/models/model.ts b/npm/ng-packs/packages/schematics/src/models/model.ts index 6c7feb318d..048c08a1b0 100644 --- a/npm/ng-packs/packages/schematics/src/models/model.ts +++ b/npm/ng-packs/packages/schematics/src/models/model.ts @@ -1,43 +1,97 @@ import { Import } from './import'; -import { Omissible } from './util'; +import { Options } from './util'; export class Model { - imports: Import[] = []; - interfaces: Interface[] = []; - namespace: string; - path: string; + readonly imports: Import[] = []; + readonly interfaces: Interface[] = []; + readonly namespace: string; + readonly path: string; constructor(options: ModelOptions) { Object.assign(this, options); } } -export type ModelOptions = Omissible; +export type ModelOptions = Options; export class Interface { - base: string | null; - identifier: string; - namespace: string; - properties: Property[] = []; - ref: string; + readonly base: string | null; + readonly identifier: string; + readonly namespace: string; + readonly generics: Generic[] = []; + readonly properties: Property[] = []; + readonly ref: string; constructor(options: InterfaceOptions) { Object.assign(this, options); } } -export type InterfaceOptions = Omissible; +export type InterfaceOptions = Options; -export class Property { - name: string; - type: string; - default = ''; - optional: '' | '?' = ''; - refs: string[] = []; +abstract class TypeRef { + readonly refs: string[] = []; - constructor(options: PropertyOptions) { + protected _type = ''; + get type() { + return this._type; + } + set type(value: string) { + if (!value) return; + this._type = value; + } + + protected _default = ''; + get default() { + return this._default; + } + set default(value: string) { + if (!value) return; + this._default = ` = ${value}`; + } + + constructor(options: TypeRefOptions) { Object.assign(this, options); } + + setDefault(value: string) { + this.default = value; + } + + setType(value: string) { + this.type = value; + } +} + +type TypeRefOptionalKeys = 'default' | 'refs'; +type TypeRefOptions = Options; + +export class Generic extends TypeRef { + constructor(options: GenericOptions) { + super(options); + } +} + +export type GenericOptions = Options; + +export class Property extends TypeRef { + readonly name: string; + private _optional: '' | '?' = ''; + get optional() { + return this.default ? '' : this._optional; + } + + set optional(value: '' | '?') { + this._optional = value; + } + + constructor(options: PropertyOptions) { + super(options); + } + + setOptional(isOptional: boolean) { + this.optional = isOptional ? '?' : ''; + } } -export type PropertyOptions = Omissible; +export type PropertyOptions = Options;