MetadataStorage.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. import { TransformationType } from './enums';
  2. /**
  3. * Storage all library metadata.
  4. */
  5. var MetadataStorage = /** @class */ (function () {
  6. function MetadataStorage() {
  7. // -------------------------------------------------------------------------
  8. // Properties
  9. // -------------------------------------------------------------------------
  10. this._typeMetadatas = new Map();
  11. this._transformMetadatas = new Map();
  12. this._exposeMetadatas = new Map();
  13. this._excludeMetadatas = new Map();
  14. this._ancestorsMap = new Map();
  15. }
  16. // -------------------------------------------------------------------------
  17. // Adder Methods
  18. // -------------------------------------------------------------------------
  19. MetadataStorage.prototype.addTypeMetadata = function (metadata) {
  20. if (!this._typeMetadatas.has(metadata.target)) {
  21. this._typeMetadatas.set(metadata.target, new Map());
  22. }
  23. this._typeMetadatas.get(metadata.target).set(metadata.propertyName, metadata);
  24. };
  25. MetadataStorage.prototype.addTransformMetadata = function (metadata) {
  26. if (!this._transformMetadatas.has(metadata.target)) {
  27. this._transformMetadatas.set(metadata.target, new Map());
  28. }
  29. if (!this._transformMetadatas.get(metadata.target).has(metadata.propertyName)) {
  30. this._transformMetadatas.get(metadata.target).set(metadata.propertyName, []);
  31. }
  32. this._transformMetadatas.get(metadata.target).get(metadata.propertyName).push(metadata);
  33. };
  34. MetadataStorage.prototype.addExposeMetadata = function (metadata) {
  35. if (!this._exposeMetadatas.has(metadata.target)) {
  36. this._exposeMetadatas.set(metadata.target, new Map());
  37. }
  38. this._exposeMetadatas.get(metadata.target).set(metadata.propertyName, metadata);
  39. };
  40. MetadataStorage.prototype.addExcludeMetadata = function (metadata) {
  41. if (!this._excludeMetadatas.has(metadata.target)) {
  42. this._excludeMetadatas.set(metadata.target, new Map());
  43. }
  44. this._excludeMetadatas.get(metadata.target).set(metadata.propertyName, metadata);
  45. };
  46. // -------------------------------------------------------------------------
  47. // Public Methods
  48. // -------------------------------------------------------------------------
  49. MetadataStorage.prototype.findTransformMetadatas = function (target, propertyName, transformationType) {
  50. return this.findMetadatas(this._transformMetadatas, target, propertyName).filter(function (metadata) {
  51. if (!metadata.options)
  52. return true;
  53. if (metadata.options.toClassOnly === true && metadata.options.toPlainOnly === true)
  54. return true;
  55. if (metadata.options.toClassOnly === true) {
  56. return (transformationType === TransformationType.CLASS_TO_CLASS ||
  57. transformationType === TransformationType.PLAIN_TO_CLASS);
  58. }
  59. if (metadata.options.toPlainOnly === true) {
  60. return transformationType === TransformationType.CLASS_TO_PLAIN;
  61. }
  62. return true;
  63. });
  64. };
  65. MetadataStorage.prototype.findExcludeMetadata = function (target, propertyName) {
  66. return this.findMetadata(this._excludeMetadatas, target, propertyName);
  67. };
  68. MetadataStorage.prototype.findExposeMetadata = function (target, propertyName) {
  69. return this.findMetadata(this._exposeMetadatas, target, propertyName);
  70. };
  71. MetadataStorage.prototype.findExposeMetadataByCustomName = function (target, name) {
  72. return this.getExposedMetadatas(target).find(function (metadata) {
  73. return metadata.options && metadata.options.name === name;
  74. });
  75. };
  76. MetadataStorage.prototype.findTypeMetadata = function (target, propertyName) {
  77. return this.findMetadata(this._typeMetadatas, target, propertyName);
  78. };
  79. MetadataStorage.prototype.getStrategy = function (target) {
  80. var excludeMap = this._excludeMetadatas.get(target);
  81. var exclude = excludeMap && excludeMap.get(undefined);
  82. var exposeMap = this._exposeMetadatas.get(target);
  83. var expose = exposeMap && exposeMap.get(undefined);
  84. if ((exclude && expose) || (!exclude && !expose))
  85. return 'none';
  86. return exclude ? 'excludeAll' : 'exposeAll';
  87. };
  88. MetadataStorage.prototype.getExposedMetadatas = function (target) {
  89. return this.getMetadata(this._exposeMetadatas, target);
  90. };
  91. MetadataStorage.prototype.getExcludedMetadatas = function (target) {
  92. return this.getMetadata(this._excludeMetadatas, target);
  93. };
  94. MetadataStorage.prototype.getExposedProperties = function (target, transformationType) {
  95. return this.getExposedMetadatas(target)
  96. .filter(function (metadata) {
  97. if (!metadata.options)
  98. return true;
  99. if (metadata.options.toClassOnly === true && metadata.options.toPlainOnly === true)
  100. return true;
  101. if (metadata.options.toClassOnly === true) {
  102. return (transformationType === TransformationType.CLASS_TO_CLASS ||
  103. transformationType === TransformationType.PLAIN_TO_CLASS);
  104. }
  105. if (metadata.options.toPlainOnly === true) {
  106. return transformationType === TransformationType.CLASS_TO_PLAIN;
  107. }
  108. return true;
  109. })
  110. .map(function (metadata) { return metadata.propertyName; });
  111. };
  112. MetadataStorage.prototype.getExcludedProperties = function (target, transformationType) {
  113. return this.getExcludedMetadatas(target)
  114. .filter(function (metadata) {
  115. if (!metadata.options)
  116. return true;
  117. if (metadata.options.toClassOnly === true && metadata.options.toPlainOnly === true)
  118. return true;
  119. if (metadata.options.toClassOnly === true) {
  120. return (transformationType === TransformationType.CLASS_TO_CLASS ||
  121. transformationType === TransformationType.PLAIN_TO_CLASS);
  122. }
  123. if (metadata.options.toPlainOnly === true) {
  124. return transformationType === TransformationType.CLASS_TO_PLAIN;
  125. }
  126. return true;
  127. })
  128. .map(function (metadata) { return metadata.propertyName; });
  129. };
  130. MetadataStorage.prototype.clear = function () {
  131. this._typeMetadatas.clear();
  132. this._exposeMetadatas.clear();
  133. this._excludeMetadatas.clear();
  134. this._ancestorsMap.clear();
  135. };
  136. // -------------------------------------------------------------------------
  137. // Private Methods
  138. // -------------------------------------------------------------------------
  139. MetadataStorage.prototype.getMetadata = function (metadatas, target) {
  140. var metadataFromTargetMap = metadatas.get(target);
  141. var metadataFromTarget;
  142. if (metadataFromTargetMap) {
  143. metadataFromTarget = Array.from(metadataFromTargetMap.values()).filter(function (meta) { return meta.propertyName !== undefined; });
  144. }
  145. var metadataFromAncestors = [];
  146. for (var _i = 0, _a = this.getAncestors(target); _i < _a.length; _i++) {
  147. var ancestor = _a[_i];
  148. var ancestorMetadataMap = metadatas.get(ancestor);
  149. if (ancestorMetadataMap) {
  150. var metadataFromAncestor = Array.from(ancestorMetadataMap.values()).filter(function (meta) { return meta.propertyName !== undefined; });
  151. metadataFromAncestors.push.apply(metadataFromAncestors, metadataFromAncestor);
  152. }
  153. }
  154. return metadataFromAncestors.concat(metadataFromTarget || []);
  155. };
  156. MetadataStorage.prototype.findMetadata = function (metadatas, target, propertyName) {
  157. var metadataFromTargetMap = metadatas.get(target);
  158. if (metadataFromTargetMap) {
  159. var metadataFromTarget = metadataFromTargetMap.get(propertyName);
  160. if (metadataFromTarget) {
  161. return metadataFromTarget;
  162. }
  163. }
  164. for (var _i = 0, _a = this.getAncestors(target); _i < _a.length; _i++) {
  165. var ancestor = _a[_i];
  166. var ancestorMetadataMap = metadatas.get(ancestor);
  167. if (ancestorMetadataMap) {
  168. var ancestorResult = ancestorMetadataMap.get(propertyName);
  169. if (ancestorResult) {
  170. return ancestorResult;
  171. }
  172. }
  173. }
  174. return undefined;
  175. };
  176. MetadataStorage.prototype.findMetadatas = function (metadatas, target, propertyName) {
  177. var metadataFromTargetMap = metadatas.get(target);
  178. var metadataFromTarget;
  179. if (metadataFromTargetMap) {
  180. metadataFromTarget = metadataFromTargetMap.get(propertyName);
  181. }
  182. var metadataFromAncestorsTarget = [];
  183. for (var _i = 0, _a = this.getAncestors(target); _i < _a.length; _i++) {
  184. var ancestor = _a[_i];
  185. var ancestorMetadataMap = metadatas.get(ancestor);
  186. if (ancestorMetadataMap) {
  187. if (ancestorMetadataMap.has(propertyName)) {
  188. metadataFromAncestorsTarget.push.apply(metadataFromAncestorsTarget, ancestorMetadataMap.get(propertyName));
  189. }
  190. }
  191. }
  192. return metadataFromAncestorsTarget
  193. .slice()
  194. .reverse()
  195. .concat((metadataFromTarget || []).slice().reverse());
  196. };
  197. MetadataStorage.prototype.getAncestors = function (target) {
  198. if (!target)
  199. return [];
  200. if (!this._ancestorsMap.has(target)) {
  201. var ancestors = [];
  202. for (var baseClass = Object.getPrototypeOf(target.prototype.constructor); typeof baseClass.prototype !== 'undefined'; baseClass = Object.getPrototypeOf(baseClass.prototype.constructor)) {
  203. ancestors.push(baseClass);
  204. }
  205. this._ancestorsMap.set(target, ancestors);
  206. }
  207. return this._ancestorsMap.get(target);
  208. };
  209. return MetadataStorage;
  210. }());
  211. export { MetadataStorage };
  212. //# sourceMappingURL=MetadataStorage.js.map