UNPKG

@wuapi/essential

Version:
649 lines (648 loc) 20.4 kB
import { forIn, notNU } from "./util"; /** * Parent of classes that have comment. */ export class $Commentable { constructor() { /** * The comment */ this.comment = ""; /** * The configuration map of this field. */ this.config = null; } } export var $ReqMethod; (function ($ReqMethod) { // HTTP $ReqMethod[$ReqMethod["GET"] = 0] = "GET"; $ReqMethod[$ReqMethod["HEAD"] = 1] = "HEAD"; $ReqMethod[$ReqMethod["POST"] = 2] = "POST"; $ReqMethod[$ReqMethod["PUT"] = 3] = "PUT"; $ReqMethod[$ReqMethod["DELETE"] = 4] = "DELETE"; $ReqMethod[$ReqMethod["CONNECT"] = 5] = "CONNECT"; $ReqMethod[$ReqMethod["OPTIONS"] = 6] = "OPTIONS"; $ReqMethod[$ReqMethod["TRACE"] = 7] = "TRACE"; $ReqMethod[$ReqMethod["PATCH"] = 8] = "PATCH"; // SOCKET or MQTT $ReqMethod[$ReqMethod["SOCKET"] = 9] = "SOCKET"; $ReqMethod[$ReqMethod["MQTT"] = 10] = "MQTT"; })($ReqMethod || ($ReqMethod = {})); /** * A class to represent an element (entity or enumeration) path * inside a project. */ export class $ElementPath { /** * Constructor of ElementPath * @param module The module name * @param name The element name */ constructor(module, name) { this.module = module; this.name = name; } /** * Compare with another ElementPath object * @param another Another ElementPath object. * @returns true if the other is identitcal to this ElementPath, false otherwise. */ equals(another) { if (!another) return false; return another.module == this.module && another.name == this.name; } /** * Returns the entity in the project, designated by the path. * @param project The project where to find the Entity. * @returns The entity found. */ asEntityOf(project) { var _a, _b; if (!project) return null; if (this.module == null || this.name == null) return null; return (_b = (_a = project.modules[this.module]) === null || _a === void 0 ? void 0 : _a.entities[this.name]) !== null && _b !== void 0 ? _b : null; } /** * Returns the enumeration in the project, designated by the path. * @param project The project where to find the Entity. * @returns The enumeration found. */ asEnumOf(project) { var _a, _b; if (!project) return null; if (this.module == null || this.name == null) return null; return (_b = (_a = project.modules[this.module]) === null || _a === void 0 ? void 0 : _a.enums[this.name]) !== null && _b !== void 0 ? _b : null; } /** * Create $ElementPath instance from json data. * @param data Json data * @returns The $ElementPath instance */ static load(data) { var _a, _b; return new $ElementPath((_a = data.module) !== null && _a !== void 0 ? _a : null, (_b = data.name) !== null && _b !== void 0 ? _b : null); } } /** * Item of enumeration entity. */ export class $EnumItem extends $Commentable { /** * The constructor of enumeration item. * @param value The number id of the item. */ constructor(value) { super(); this.value = value; /** * The real (json) name may differ from the display name. * For example, the json name may violet forbidden key rules. */ this.realname = null; } /** * Create $EnumItem instance from json data. * @param data Json data * @returns The $EnumItem instance */ static load(data) { var _a, _b; if (!notNU(data.value)) return null; const item = new $EnumItem(data.value); item.comment = (_a = data.comment) !== null && _a !== void 0 ? _a : ""; item.config = data.config; item.realname = (_b = data.realname) !== null && _b !== void 0 ? _b : null; return item; } } /** * Enumeration entity. */ export class $Enum extends $Commentable { constructor() { super(...arguments); /** * Map of string names to enum items. */ this.enumMap = {}; } /** * Get the first item. * @returns The first item (by number value) of this Enumeration */ first() { const k = this.firstName(); return null == k ? null : this.enumMap[k]; } /** * Get the name of the first item. * @returns The name of the first (by number value) item of this Enumeration. */ firstName() { var i = null; var k = null; for (const key in this.enumMap) { const item = this.enumMap[key]; if (i == null || item.value < i) { i = item.value; k = key; } } // return k == null ? null : this.enumMap[k] return k; } /** * Flat an enumeration's items. * @returns List of name, item. Order is not defined. */ flat() { let result = []; forIn(this.enumMap, (item, name) => { result.push({ name, item, }); }); return result; } /** * Create $Enum instance from json data. * @param data Json data * @returns The $Enum instance */ static load(data) { var _a; if (!notNU(data.enumMap)) return null; const enu = new $Enum(); enu.comment = (_a = data.comment) !== null && _a !== void 0 ? _a : ""; enu.config = data.config; for (let i in data.enumMap) { const item = $EnumItem.load(data.enumMap[i]); if (notNU(item)) { enu.enumMap[i] = item; } } return enu; } } /** * The entity field type. */ export class $FieldType { /** * Constructor of FieldType * @param type The name of this type */ constructor(type) { this.type = type; } /** * Check if this type equals with another. * @param another Another type, or null * @returns true if this type is the same with another type */ equals(another) { var _a, _b, _c; if (!another) return false; if (another.type != this.type) return false; switch (this.type) { case 'TObject': { const _self = this; const _other = another; return ((_a = _self === null || _self === void 0 ? void 0 : _self.entity) === null || _a === void 0 ? void 0 : _a.equals(_other === null || _other === void 0 ? void 0 : _other.entity)) == true; } case 'TEnum': { const _self = this; const _other = another; return ((_b = _self === null || _self === void 0 ? void 0 : _self.enu) === null || _b === void 0 ? void 0 : _b.equals(_other === null || _other === void 0 ? void 0 : _other.enu)) == true; } case 'TUnknown': { const _self = this; const _other = another; return (_self === null || _self === void 0 ? void 0 : _self.unknown) == (_other === null || _other === void 0 ? void 0 : _other.unknown); } case 'TList': { const _self = this; const _other = another; return (_c = _self === null || _self === void 0 ? void 0 : _self.member) === null || _c === void 0 ? void 0 : _c.equals(_other === null || _other === void 0 ? void 0 : _other.member); } default: { return true; } } } /** * Check if this type equals with another. * If the other field type is list, check it's member. * * @param another Another type, or null * @returns true if this type is the same with another type */ equalsEvenInList(another) { if (this.type == 'TList') throw "List field shall not use quealsInList!"; if (!notNU(another)) return false; if ((another === null || another === void 0 ? void 0 : another.type) == 'TList') { return this.equalsEvenInList(another.member); } else { return this.equals(another); } } /** * Create $FieldType instance from json data. * @param data Json data * @returns The $FieldType instance */ static load(data) { if (!notNU(data.type)) return null; switch (data.type) { case 'TInteger': return new $TInteger(); case 'TLong': return new $TLong(); case 'TDouble': return new $TDouble(); case 'TID': return new $TID(); case 'TURL': return new $TURL(); case 'TDateTime': return new $TDateTime(); case 'TBoolean': return new $TBoolean(); case 'TString': return new $TString(); case 'TSSMap': return new $TSSMap(); case 'TObject': { if (!notNU(data.entity)) return null; const path = $ElementPath.load(data.entity); if (!notNU(path)) return null; return new $TObject(path); } case 'TEnum': { if (!notNU(data.enu)) return null; const path = $ElementPath.load(data.enu); if (!notNU(path)) return null; return new $TEnum(path); } case 'TList': { if (!notNU(data.member)) return null; const member = $FieldType.load(data.member); if (!notNU(member)) return null; return new $TList(member); } case 'TUnknown': { if (!notNU(data.unknown)) return null; return new $TUnknown(data.unknown); } default: return null; } } } export class $TObject extends $FieldType { constructor(entity) { super('TObject'); this.entity = entity; } } export class $TEnum extends $FieldType { constructor(enu) { super('TEnum'); this.enu = enu; } } export class $TList extends $FieldType { constructor(member) { super('TList'); this.member = member; } } export class $TUnknown extends $FieldType { constructor(unknown) { super('TUnknown'); this.unknown = unknown; } } export class $TInteger extends $FieldType { constructor() { super('TInteger'); } } export class $TLong extends $FieldType { constructor() { super('TLong'); } } export class $TDouble extends $FieldType { constructor() { super('TDouble'); } } export class $TID extends $FieldType { constructor() { super('TID'); } } export class $TURL extends $FieldType { constructor() { super('TURL'); } } export class $TDateTime extends $FieldType { constructor() { super('TDateTime'); } } export class $TBoolean extends $FieldType { constructor() { super('TBoolean'); } } export class $TString extends $FieldType { constructor() { super('TString'); } } export class $TSSMap extends $FieldType { constructor() { super('TSSMap'); } } /** * The entity field. */ export class $Field extends $Commentable { /** * Constructor of Entity Field. * @param type The type of this field */ constructor(type) { super(); this.type = type; /** * The real (json) name may differ from the display name. * For example, the json name may violet forbidden key rules. */ this.realname = null; /** * True if this field is optional */ this.isOptional = false; /** * True if this field would appear inside path. */ this.isPathParameter = false; /** * The optional fixed value of this field. */ this.fixedValue = null; } /** * Create $Field instance from json data. * @param data Json data * @returns The $Field instance */ static load(data) { var _a, _b; if (!notNU(data.type, data.isOptional, data.isPathParameter)) return null; const filedType = $FieldType.load(data.type); if (!notNU(filedType)) return null; const field = new $Field(filedType); field.comment = (_a = data.comment) !== null && _a !== void 0 ? _a : ""; field.config = data.config; field.realname = (_b = data.realname) !== null && _b !== void 0 ? _b : null; field.isOptional = data.isOptional; field.isPathParameter = data.isPathParameter; field.fixedValue = data.fixedValue; field.config = data.config; return field; } } /** * Type of entities. */ export var $EntityType; (function ($EntityType) { $EntityType[$EntityType["OBJECT"] = 0] = "OBJECT"; $EntityType[$EntityType["REQUEST"] = 1] = "REQUEST"; $EntityType[$EntityType["RESPONSE"] = 2] = "RESPONSE"; })($EntityType || ($EntityType = {})); /** * Entity */ export class $Entity extends $Commentable { /** * List of generic parameters defined inside current entity. * (no generic from parent) */ getGenericLocal() { let result = []; for (let k in this.fieldsLocal) { const field = this.fieldsLocal[k]; if (field.type.type == 'TUnknown') { result.push(field.type.unknown); } } return result; } /** * Return a list of names of generics that are unsolved until this entity. * @param project The project, from where to get generic unsolved. * @returns A list of names of unsolved generics. */ getGenericUnsolved(project) { var _a, _b, _c; if (!project) return []; let pg = (_c = (_b = (_a = this.parent) === null || _a === void 0 ? void 0 : _a.asEntityOf(project)) === null || _b === void 0 ? void 0 : _b.getGenericUnsolved(project)) !== null && _c !== void 0 ? _c : []; pg = pg.concat(this.getGenericLocal()); return pg.filter((o) => !this.genericMap[o]); } /** * Traverse from the eldest ancestor all down to this entity. * @param project The project from where to traverse. * @param block The callback function */ fromAncestorToMe(project, block) { var _a, _b; (_b = (_a = this.parent) === null || _a === void 0 ? void 0 : _a.asEntityOf(project)) === null || _b === void 0 ? void 0 : _b.fromAncestorToMe(project, block); block(this); } /** * Constructor of Entity class. * @param type The type of this entity */ constructor(type = $EntityType.OBJECT) { super(); this.type = type; /** * True if this entity is abstract (shall not instantiate) */ this.isAbstract = false; /** * The path of the parent entity, optional. */ this.parent = null; /** * The path of corresponding response entity, if this entity is a request. */ this.response = null; /** * The URL path, if this entity is a request. */ this.path = null; /** * The Request method, if this entity is a request. */ this.method = null; /** * List of fields defined inside current entity. * (no field from parent) */ this.fieldsLocal = {}; /** * The map of definitions of generic parameters, till this entity. */ this.genericMap = {}; } /** * Create $Entity instance from json data. * @param data Json data * @returns The $Entity instance */ static load(data) { var _a, _b, _c; if (!notNU(data.type, data.isAbstract, data.fieldsLocal, data.genericMap)) return null; const entity = new $Entity(data.type); // enum as number entity.comment = (_a = data.comment) !== null && _a !== void 0 ? _a : ""; entity.config = data.config; entity.isAbstract = data.isAbstract; entity.path = (_b = data.path) !== null && _b !== void 0 ? _b : null; entity.method = (_c = data.method) !== null && _c !== void 0 ? _c : null; // enum as number if (notNU(data.parent)) { entity.parent = $ElementPath.load(data.parent); } if (notNU(data.response)) { entity.response = $ElementPath.load(data.response); } for (let i in data.fieldsLocal) { const field = $Field.load(data.fieldsLocal[i]); if (notNU(field)) { entity.fieldsLocal[i] = field; } } for (let i in data.genericMap) { const field = $Field.load(data.genericMap[i]); if (notNU(field)) { entity.genericMap[i] = field; } } return entity; } } /** * The module object */ export class $Module { constructor() { /** * The map of entities inside this module. */ this.entities = {}; /** * The map of enumerations inside this module. */ this.enums = {}; } /** * Create $Module instance from json data. * @param data Json data * @returns The $Module instance */ static load(data) { if (!notNU(data.entities, data.enums)) return null; const module = new $Module(); for (let i in data.entities) { const entity = $Entity.load(data.entities[i]); if (notNU(entity)) { module.entities[i] = entity; } } for (let i in data.enums) { const enu = $Enum.load(data.enums[i]); if (notNU(enu)) { module.enums[i] = enu; } } return module; } } /** * The project object */ export class $Project { /** * The constructor of entity project. * @param name The name of the project * @param version The version of the project * @param targetPackage The package (for Java & Kotlin) into which the entities * will be generated */ constructor(name, version, targetPackage) { this.name = name; this.version = version; this.targetPackage = targetPackage; /** * The map of modules. */ this.modules = {}; } /** * Flat the project into list of entities. * @returns list of path, entity. Order is not defined! */ flatEntities() { let result = []; forIn(this.modules, (moduleInstance, module) => { forIn(moduleInstance.entities, (entityInstance, name) => { result.push({ path: new $ElementPath(module, name), entity: entityInstance }); }); }); return result; } /** * Flat the project into list of enumerations. * @returns list of path, enumerations. Order is not defined! */ flatEnums() { let result = []; forIn(this.modules, (moduleInstance, module) => { forIn(moduleInstance.enums, (enumInstance, name) => { result.push({ path: new $ElementPath(module, name), enu: enumInstance }); }); }); return result; } /** * Create $Project instance from json data. * @param data Json object * @returns Generated $Project instance. */ static load(data) { if (!notNU(data.name, data.version, data.targetPackage, data.modules)) return null; const project = new $Project(data.name, data.version, data.targetPackage); for (let i in data.modules) { const module = $Module.load(data.modules[i]); if (notNU(module)) { project.modules[i] = module; } } return project; } }