MapsterMapper / Mapster

A fast, fun and stimulating object to object Mapper
MIT License
4.23k stars 325 forks source link

How can I map enums #661

Closed misho-29 closed 5 months ago

misho-29 commented 7 months ago

I am using Mapster 7.4.0.

I have two enums BaggageFilter and FlightSearchBaggageFilter. Following code does not work for these enums but MapWith works for string types. Does mapster have a bug? Is there any other way to do mapping between enums? (Sorry for my english)

Expression<Func<BaggageFilter, FlightSearchBaggageFilter>> expression = x => FlightSearchBaggageFilter.Included; config.NewConfig<BaggageFilter, FlightSearchBaggageFilter>().MapWith(expression);

stagep commented 7 months ago

Can you provide the enum definitions for BaggageFilter and FlightSearchBaggageFilter.

misho-29 commented 6 months ago

Can you provide the enum definitions for BaggageFilter and FlightSearchBaggageFilter.

Sorry for the late reply. I need to map these enums manually in config

public enum BaggageFilter
{
    Ignore,
    Included,
}

public enum BaggageFilter
{
    INCLUDED,
    IGNORED,
}
stagep commented 6 months ago

I assumed that the second enum is FlightSearchBaggageFilter. You just need to point the MapWith to a method that accepts a BaggageFilter enum and returns a FlightSearchBaggageFilter enum.

public enum BaggageFilter
{
    Ignore,
    Included,
}

public enum FlightSearchBaggageFilter
{
    INCLUDED,
    IGNORED,
}

public class MapsterConfiguration : IRegister
{
    public void Register(TypeAdapterConfig config)
    {
        config.NewConfig<BaggageFilter, FlightSearchBaggageFilter>().MapWith(x => ToFlightSearchBaggageFilter(x));
    }

    FlightSearchBaggageFilter ToFlightSearchBaggageFilter(BaggageFilter baggageFilter)
    {
        return baggageFilter switch
        {
            BaggageFilter.Ignore => FlightSearchBaggageFilter.IGNORED,
            BaggageFilter.Included => FlightSearchBaggageFilter.INCLUDED,
            _ => throw new ArgumentOutOfRangeException(nameof(baggageFilter), baggageFilter, "Out of range...")
        };
    }
}
misho-29 commented 5 months ago

I assumed that the second enum is FlightSearchBaggageFilter. You just need to point the MapWith to a method that accepts a BaggageFilter enum and returns a FlightSearchBaggageFilter enum.

public enum BaggageFilter
{
    Ignore,
    Included,
}

public enum FlightSearchBaggageFilter
{
    INCLUDED,
    IGNORED,
}

public class MapsterConfiguration : IRegister
{
    public void Register(TypeAdapterConfig config)
    {
        config.NewConfig<BaggageFilter, FlightSearchBaggageFilter>().MapWith(x => ToFlightSearchBaggageFilter(x));
    }

    FlightSearchBaggageFilter ToFlightSearchBaggageFilter(BaggageFilter baggageFilter)
    {
        return baggageFilter switch
        {
            BaggageFilter.Ignore => FlightSearchBaggageFilter.IGNORED,
            BaggageFilter.Included => FlightSearchBaggageFilter.INCLUDED,
            _ => throw new ArgumentOutOfRangeException(nameof(baggageFilter), baggageFilter, "Out of range...")
        };
    }
}

It works, Thanks 👍