-
-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): add autoMap mapping configuration (#538)
Avoid using the @AutoMap decorator by replacing it with calls to autoMap inside the profile.
- Loading branch information
1 parent
25b5e7a
commit 5906add
Showing
6 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import type { MappingConfiguration } from '../types'; | ||
import { mapFrom } from '../member-map-functions/map-from'; | ||
|
||
import { forMember } from './for-member'; | ||
|
||
export function autoMap< | ||
TSource extends { [key in TKey]: TValue }, | ||
TDestination extends { [key in TKey]: TValue }, | ||
TKey extends keyof TSource & keyof TDestination, | ||
TValue extends TSource[TKey] & TDestination[TKey] | ||
>(prop: TKey): MappingConfiguration<TSource, TDestination> { | ||
return forMember( | ||
(dest) => dest[prop] as TValue, | ||
mapFrom((src) => src[prop]) | ||
); | ||
} |
5 changes: 5 additions & 0 deletions
5
packages/integration-test/src/classes/dtos/decoratorless-user.dto.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export class DecoratorlessUserDto { | ||
firstName!: string; | ||
lastName!: string; | ||
fullName!: string; | ||
} |
43 changes: 43 additions & 0 deletions
43
packages/integration-test/src/classes/map-without-decorators.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { classes } from '@automapper/classes'; | ||
import { addProfile, createMapper } from '@automapper/core'; | ||
|
||
import { DecoratorlessUserDto } from './dtos/decoratorless-user.dto'; | ||
import { DecoratorlessUser } from './models/decoratorless-user'; | ||
import { decoratorlessUserProfileFactory } from './profiles/decoratorless-user.profile'; | ||
|
||
describe('Map Without Decorators', () => { | ||
const mapper = createMapper({ strategyInitializer: classes() }); | ||
|
||
beforeAll(() => { | ||
addProfile(mapper, decoratorlessUserProfileFactory()); | ||
}); | ||
|
||
it('should map model to DTO', async () => { | ||
const dto = await mapper.mapAsync( | ||
new DecoratorlessUser('Gabriel', 'Kim'), | ||
DecoratorlessUser, | ||
DecoratorlessUserDto | ||
); | ||
|
||
expect(dto.firstName).toEqual('Gabriel'); | ||
expect(dto.lastName).toEqual('Kim'); | ||
expect(dto.fullName).toEqual('Gabriel Kim'); | ||
}); | ||
|
||
it('should map DTO to model', async () => { | ||
const dto = new DecoratorlessUserDto(); | ||
dto.firstName = 'Gabriel'; | ||
dto.lastName = 'Kim'; | ||
dto.fullName = 'Gabriel Kim'; | ||
|
||
const model = await mapper.mapAsync( | ||
dto, | ||
DecoratorlessUserDto, | ||
DecoratorlessUser | ||
); | ||
|
||
expect(model.firstName).toEqual('Gabriel'); | ||
expect(model.lastName).toEqual('Kim'); | ||
expect(model).not.toHaveProperty('fullName'); | ||
}); | ||
}); |
9 changes: 9 additions & 0 deletions
9
packages/integration-test/src/classes/models/decoratorless-user.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export class DecoratorlessUser { | ||
firstName!: string; | ||
lastName!: string; | ||
|
||
constructor(firstName: string, lastName: string) { | ||
this.firstName = firstName; | ||
this.lastName = lastName; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
packages/integration-test/src/classes/profiles/decoratorless-user.profile.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { | ||
autoMap, | ||
createMap, | ||
forMember, | ||
mapFrom, | ||
MappingProfile, | ||
} from '@automapper/core'; | ||
import { DecoratorlessUserDto } from '../dtos/decoratorless-user.dto'; | ||
import { DecoratorlessUser } from '../models/decoratorless-user'; | ||
|
||
export function decoratorlessUserProfileFactory(): MappingProfile { | ||
return (mapper) => { | ||
createMap( | ||
mapper, | ||
DecoratorlessUser, | ||
DecoratorlessUserDto, | ||
autoMap('firstName'), | ||
autoMap('lastName'), | ||
forMember( | ||
(d) => d.fullName, | ||
mapFrom((s) => s.firstName + ' ' + s.lastName) | ||
) | ||
); | ||
|
||
createMap( | ||
mapper, | ||
DecoratorlessUserDto, | ||
DecoratorlessUser, | ||
autoMap('firstName'), | ||
autoMap('lastName') | ||
); | ||
}; | ||
} |