-
Is your feature request related to a problem? Please describe.When I'm mapping between two kind of datastructures, I'm often facing the problem that they're both using equivalent enums but these encode the values as different constants or have different variant names. class UserEntity {
@AutoMap()
name: string;
@AutoMap()
favoriteColor: ColorEntity;
}
enum ColorEntity {
Red = 'r',
Green = 'g',
Blue = 'b',
Other = 'o',
} class UserDto {
@AutoMap()
name: string;
@AutoMap()
favoriteColor: ColorDto;
}
enum ColorDto {
Red = 'red',
Green = 'green',
Blue = 'blue',
Custom = 'custom',
} How can I teach AutoMapper to map Describe the solution you'd likeI'd love to see automapper handle enums like any other class, so a mapping between two enums can be defined. The variants of the enum are mapped like typescript fields:
mapper.createMap(ColorEntity, ColorDto)
.forMember(dto => dto.Custom, mapFrom(entity => entity.Other)) Describe alternatives you've consideredNo response Additional contextI barely know typescript reflection, so I can't judge whether this idea is realizable. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments
-
Thank you for showing interest and requesting this feature. However, Enums are complicated: string enums, number enums, mixed enums, and const enums... Hence, it is tricky to get some generic support for enum in this sense. What I can suggest you to do is to create a export const colorResolver: Resolver<
{color: ColorEntity},
{color: ColorDto},
ColorDto
> = {
resolve(source: TypeConverterSource): ColorDto {
switch (source.color) {
case Color.Red:
return ColorDto.Red;
case Color.Green:
return ColorDto.Green;
case Color.Blue:
return ColorDto.Blue;
}
},
}; then use it on a mapper.createMap(Source, Destination)
.forMember(d => d.color, mapFrom(colorResolver)) |
Beta Was this translation helpful? Give feedback.
-
@aki-ks can we close this issue? |
Beta Was this translation helpful? Give feedback.
-
If you want to leave it at your suggestion and don't plan to add extended support for enums to the library, then sure. |
Beta Was this translation helpful? Give feedback.
-
Thank you. I'll convert this to a discussion then :) |
Beta Was this translation helpful? Give feedback.
Thank you for showing interest and requesting this feature.
However, Enums are complicated: string enums, number enums, mixed enums, and const enums... Hence, it is tricky to get some generic support for enum in this sense.
What I can suggest you to do is to create a
Resolver
and use aswitch
statementthen use it on a
mapFrom()