|
| 1 | +import { |
| 2 | + BaseMetadata, |
| 3 | + EntityPropertyMetadata, |
| 4 | + FakerPropertyMetadata, |
| 5 | + TargetMap, |
| 6 | + MetadataTarget |
| 7 | +} from './utils/types-helper' |
| 8 | + |
| 9 | +export class MetadataUtils<Meta extends BaseMetadata> { |
| 10 | + private _ancestorsMap = new Map<MetadataTarget, Function[]>() |
| 11 | + |
| 12 | + /** |
| 13 | + * clear constructors map |
| 14 | + */ |
| 15 | + public clear(): void { |
| 16 | + this._ancestorsMap.clear() |
| 17 | + } |
| 18 | + |
| 19 | + /** |
| 20 | + * save metadata to target map's property map |
| 21 | + * @param constructMap constructor map Map<constructor, Map<propertyName, metadata>> |
| 22 | + * @param metadata metadata |
| 23 | + */ |
| 24 | + public setMetadata<T extends Meta>(constructMap: TargetMap<T>, metadata: T): void { |
| 25 | + const {target, propertyName} = metadata |
| 26 | + |
| 27 | + if (!constructMap.has(target)) { |
| 28 | + constructMap.set(target, new Map<string, T>()) |
| 29 | + } |
| 30 | + constructMap.get(target)!.set(propertyName, metadata) |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * save metadatas to target map's property map |
| 35 | + * @param constructMap constructor map Map<constructor, Map<propertyName, metadata[]>> |
| 36 | + * @param metadata metadata |
| 37 | + */ |
| 38 | + public setMetadatas<T extends Meta>(constructMap: TargetMap<T[]>, metadata: T): void { |
| 39 | + const {target, propertyName} = metadata |
| 40 | + |
| 41 | + if (!constructMap.has(target)) { |
| 42 | + constructMap.set(target, new Map<string, T[]>()) |
| 43 | + } |
| 44 | + const propertyMap = constructMap.get(target)! |
| 45 | + const metadatas = propertyMap.get(propertyName) ?? [] |
| 46 | + propertyMap.set(propertyName, [...metadatas, metadata]) |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Returns all the metadatas of the given target.(filter by propertyName if not undefined) |
| 51 | + * @param constructMap constructor map Map<constructor, Map<propertyName, metadata>> |
| 52 | + * @param target target constructor |
| 53 | + * @returns metadatas |
| 54 | + */ |
| 55 | + public getMetadatas<T extends Meta>(constructMap: TargetMap<T>, target: MetadataTarget): T[] { |
| 56 | + const ancestorsMetas: T[] = [] |
| 57 | + const ancestors = [...this.getAncestors(target), target] |
| 58 | + |
| 59 | + for (const ancestor of ancestors) { |
| 60 | + const ancestorMetas = Array.from(constructMap.get(ancestor)?.values() ?? []).filter( |
| 61 | + meta => meta.propertyName !== undefined |
| 62 | + ) |
| 63 | + ancestorsMetas.push(...ancestorMetas) |
| 64 | + } |
| 65 | + return ancestorsMetas |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * . |
| 70 | + * @param constructMap constructor map Map<constructor, Map<propertyName, metadata>> |
| 71 | + * @param target target constructor |
| 72 | + * @param propertyName property name |
| 73 | + * @returns metadata or undefined |
| 74 | + */ |
| 75 | + public findMetadata<T extends Meta>( |
| 76 | + constructMap: TargetMap<T>, |
| 77 | + target: MetadataTarget, |
| 78 | + propertyName: string |
| 79 | + ): T | undefined { |
| 80 | + const ancestors = [target, ...this.getAncestors(target)] |
| 81 | + |
| 82 | + for (const ancestor of ancestors) { |
| 83 | + const ancestorPropMeta: T | undefined = constructMap.get(ancestor)?.get(propertyName) |
| 84 | + if (ancestorPropMeta) return ancestorPropMeta |
| 85 | + } |
| 86 | + return undefined |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * find metadatas from target and its ancestors |
| 91 | + * @param constructMap constructor map Map<constructor, Map<propertyName, metadata[]>> |
| 92 | + * @param target target constructor |
| 93 | + * @param propertyName property name |
| 94 | + * @returns metadatas |
| 95 | + */ |
| 96 | + public findMetadatas<T extends Meta>( |
| 97 | + constructMap: TargetMap<T[]>, |
| 98 | + target: MetadataTarget, |
| 99 | + propertyName: string |
| 100 | + ): T[] { |
| 101 | + const targetPropMetas: T[] = constructMap.get(target)?.get(propertyName) ?? [] |
| 102 | + const ancestorsPropMetas: T[] = [] |
| 103 | + const ancestors = this.getAncestors(target) |
| 104 | + |
| 105 | + for (const ancestor of ancestors) { |
| 106 | + const ancestorPropMetas = constructMap.get(ancestor)?.get(propertyName) ?? [] |
| 107 | + ancestorsPropMetas.push(...ancestorPropMetas) |
| 108 | + } |
| 109 | + |
| 110 | + return [...ancestorsPropMetas.reverse(), ...targetPropMetas.reverse()] |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Returns all the parent's class constructor of target class |
| 115 | + * @param target target class |
| 116 | + * @returns all parent's constructor of target |
| 117 | + */ |
| 118 | + private getAncestors(target: MetadataTarget): Function[] { |
| 119 | + if (!target) return [] |
| 120 | + if (!this._ancestorsMap.has(target)) { |
| 121 | + const ancestors: Function[] = [] |
| 122 | + |
| 123 | + for ( |
| 124 | + let baseClass = Object.getPrototypeOf(target.prototype.constructor); |
| 125 | + typeof baseClass.prototype !== 'undefined'; |
| 126 | + baseClass = Object.getPrototypeOf(baseClass.prototype.constructor) |
| 127 | + ) { |
| 128 | + ancestors.push(baseClass) |
| 129 | + } |
| 130 | + this._ancestorsMap.set(target, ancestors) |
| 131 | + } |
| 132 | + return this._ancestorsMap.get(target) ?? [] |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +/** |
| 137 | + * Storage all library metadata. |
| 138 | + */ |
| 139 | +export class MetadataStorage { |
| 140 | + static _instance: MetadataStorage |
| 141 | + static get instance(): MetadataStorage { |
| 142 | + if (!MetadataStorage._instance) { |
| 143 | + MetadataStorage._instance = new MetadataStorage() |
| 144 | + } |
| 145 | + return MetadataStorage._instance |
| 146 | + } |
| 147 | + |
| 148 | + private _fakerMockConstructMap: TargetMap<FakerPropertyMetadata> = new Map() |
| 149 | + private _entityMockConstructMap: TargetMap<EntityPropertyMetadata> = new Map() |
| 150 | + private _metadataUtils = new MetadataUtils() |
| 151 | + |
| 152 | + addFakerMetadata(metadata: FakerPropertyMetadata): void { |
| 153 | + this._metadataUtils.setMetadata(this._fakerMockConstructMap, metadata) |
| 154 | + } |
| 155 | + |
| 156 | + addEntityMetadata(metadata: EntityPropertyMetadata): void { |
| 157 | + this._metadataUtils.setMetadata(this._entityMockConstructMap, metadata) |
| 158 | + } |
| 159 | + |
| 160 | + findFakerMetadata(target: MetadataTarget, propertyName: string): FakerPropertyMetadata | undefined { |
| 161 | + return this._metadataUtils.findMetadata(this._fakerMockConstructMap, target, propertyName) |
| 162 | + } |
| 163 | + |
| 164 | + findEntityMetadata(target: MetadataTarget, propertyName: string): EntityPropertyMetadata | undefined { |
| 165 | + return this._metadataUtils.findMetadata(this._entityMockConstructMap, target, propertyName) |
| 166 | + } |
| 167 | + |
| 168 | + getClassMetadatas(target: Function): Array<FakerPropertyMetadata | EntityPropertyMetadata> { |
| 169 | + const fakerMetas = this._metadataUtils.getMetadatas(this._fakerMockConstructMap, target) |
| 170 | + const entityMetas = this._metadataUtils.getMetadatas(this._entityMockConstructMap, target) |
| 171 | + |
| 172 | + return [...fakerMetas, ...entityMetas] |
| 173 | + } |
| 174 | + |
| 175 | + clear(): void { |
| 176 | + this._fakerMockConstructMap.clear() |
| 177 | + this._entityMockConstructMap.clear() |
| 178 | + this._metadataUtils.clear() |
| 179 | + } |
| 180 | +} |
0 commit comments