Skip to content

Commit

Permalink
refactor(pojos): clean up typings
Browse files Browse the repository at this point in the history
  • Loading branch information
Chau Tran authored and Chau Tran committed Aug 16, 2021
1 parent 45edfda commit f43705a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 30 deletions.
35 changes: 5 additions & 30 deletions packages/pojos/src/lib/pojos.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,22 @@
import { createInitialMapping } from '@automapper/core';
import type {
CreateMapOptions,
Dictionary,
MapPluginInitializer,
} from '@automapper/types';
import type { MapPluginInitializer } from '@automapper/types';
import { MappingClassId } from '@automapper/types';
import {
PojosMappingStorage,
PojosMetadataStorage,
pojosSymbolStorage,
} from './storages';
import { instantiate } from './utils';
import { exploreMetadata, instantiate } from './utils';

export const pojos: MapPluginInitializer<string> = (errorHandler) => {
const metadataStorage = new PojosMetadataStorage();
const mappingStorage = new PojosMappingStorage();

return {
instantiate<TModel extends Dictionary<TModel> = any>(
model: string,
obj?: TModel
) {
instantiate(model, obj?) {
return instantiate(metadataStorage, model, obj);
},
initializeMapping(
source: string,
destination: string,
options?: CreateMapOptions
) {
initializeMapping(source, destination, options?) {
if (mappingStorage.has(source, destination)) {
errorHandler.handle(
`Mapping for source ${source} and destination ${destination} already exists`
Expand Down Expand Up @@ -57,7 +46,7 @@ export const pojos: MapPluginInitializer<string> = (errorHandler) => {
}
);
},
getMapping(source: string, destination: string) {
getMapping(source, destination) {
const mapping = mappingStorage.get(source, destination);
if (!mapping) {
errorHandler.handle(
Expand All @@ -79,17 +68,3 @@ export const pojos: MapPluginInitializer<string> = (errorHandler) => {
},
};
};

function exploreMetadata(
metadataStorage: PojosMetadataStorage,
...keys: string[]
) {
keys.forEach((key) => {
if (!metadataStorage.has(key)) {
const metadataList = pojosSymbolStorage.get(Symbol.for(key));
for (const [propertyKey, metadata] of metadataList) {
metadataStorage.addMetadata(key, [[propertyKey], () => metadata]);
}
}
});
}
15 changes: 15 additions & 0 deletions packages/pojos/src/lib/utils/explore-metadata.util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { PojosMetadataStorage, pojosSymbolStorage } from '../storages';

export function exploreMetadata(
metadataStorage: PojosMetadataStorage,
...keys: string[]
) {
keys.forEach((key) => {
if (!metadataStorage.has(key)) {
const metadataList = pojosSymbolStorage.get(Symbol.for(key));
for (const [propertyKey, metadata] of metadataList) {
metadataStorage.addMetadata(key, [[propertyKey], () => metadata]);
}
}
});
}
1 change: 1 addition & 0 deletions packages/pojos/src/lib/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './instantiate.util';
export * from './explore-metadata.util';

0 comments on commit f43705a

Please sign in to comment.