Skip to content

Commit

Permalink
feat(core): add custom properties to isolate forMember configuration
Browse files Browse the repository at this point in the history
ISSUE CLOSED: #497
  • Loading branch information
nartc committed Jul 27, 2022
1 parent 5f7080b commit e5b7028
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/lib/mapping-configurations/for-member.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export function forMember<
];
}

mapping[MappingClassId.properties].push([
mapping[MappingClassId.customProperties].push([
memberPath,
mappingProperty,
nestedMappingPair,
Expand Down
13 changes: 10 additions & 3 deletions packages/core/src/lib/mappings/create-initial-mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export function createInitialMapping<
[source, destination],
[sourceObject as TSource, destinationObject as TDestination],
[],
[],
mapper,
destinationConstructor,
];
Expand All @@ -89,7 +90,8 @@ export function createInitialMapping<
const destinationPaths = getPathRecursive(destinationObject);

const mappingProperties = mapping[MappingClassId.properties];
const hasCustomMappingProperties = mappingProperties.length > 0;
const customMappingProperties = mapping[MappingClassId.customProperties];
const hasCustomMappingProperties = customMappingProperties.length > 0;

const namingConventions = mapping[MappingClassId.namingConventions];

Expand All @@ -104,7 +106,7 @@ export function createInitialMapping<
// for this destination path, skip it
if (
hasCustomMappingProperties &&
mappingProperties.some((property) =>
customMappingProperties.some((property) =>
isPrimitiveArrayEqual(
property[MappingPropertiesClassId.path],
destinationPath
Expand Down Expand Up @@ -213,12 +215,17 @@ export function createInitialMapping<
}
}

mappingProperties.unshift([
mappingProperties.push([
destinationPath,
[destinationPath, transformation],
nestedMappingPair,
]);
}

// consolidate mapping properties
for (const customMappingProperty of customMappingProperties) {
mappingProperties.push(customMappingProperty);
}

return mapping;
}
1 change: 1 addition & 0 deletions packages/core/src/lib/mappings/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ export function map<
[sourceIdentifier, destinationIdentifier],
[, destinationWithMetadata],
propsToMap,
,
mapper,
destinationConstructor,
,
Expand Down
15 changes: 15 additions & 0 deletions packages/core/src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,7 @@ export const enum MappingClassId {
identifiers,
identifierMetadata,
properties,
customProperties,
mapper,
destinationConstructor,
typeConverters,
Expand Down Expand Up @@ -527,6 +528,20 @@ export type Mapping<
]
]
>,
customProperties: Array<
[
path: string[],
mappingProperty: MappingProperty<
TSource,
TDestination,
SelectorReturn<TDestination>
>,
nestedMappingPair?: [
destination: MetadataIdentifier | Primitive | Date,
source: MetadataIdentifier | Primitive | Date
]
]
>,
mapper: Mapper,
destinationConstructor: DestinationConstructor<TSource, TDestination>,
typeConverters?: Map<
Expand Down
51 changes: 51 additions & 0 deletions packages/integration-test/src/classes/issues/497/497.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { AutoMap, classes } from '@automapper/classes';
import {
CamelCaseNamingConvention,
createMap,
createMapper,
} from '@automapper/core';

export class UserEntity {
@AutoMap()
id!: number;

@AutoMap()
name!: string;

@AutoMap()
sharedInfo!: string;

password!: string;
}

export class UserResponse {
@AutoMap()
id!: number;

@AutoMap()
name!: string;

@AutoMap()
sharedInfo!: string;
}

describe('Issue 497', () => {
const mapper = createMapper({
strategyInitializer: classes(),
namingConventions: new CamelCaseNamingConvention(),
});

it('should map properly', () => {
createMap(mapper, UserEntity, UserResponse);

const user = new UserEntity();
user.id = 123;
user.name = 'Chau';
user.sharedInfo = 'info';

const responses = mapper.mapArray([user], UserEntity, UserResponse);
expect(responses).toEqual([
{ id: 123, name: 'Chau', sharedInfo: 'info' },
]);
});
});

0 comments on commit e5b7028

Please sign in to comment.