MapsterMapper / Mapster

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

Google.Protobuf.WellKnownTypes.Timestamp Error while compiling #718

Open npatersontemenos opened 2 weeks ago

npatersontemenos commented 2 weeks ago

Using GRPC, the proto class that maps to my POCO has one field of type google.protobuf.Timestamp, whereas, it is a datetime field in my poco. The error says "source=Google.Protobuf.WellKnownTypes.Timestamp destination=System.DateTime" and goes onto say: "Cannot convert immutable type, please consider using 'MapWith' method to create mapping". I assume this means in the configure RegisterMappings method? Can anyone steer me in the right direction to resolve this please? My code is similar to: TypeAdapterConfig<W1, W2>.NewConfig() .Map(dest => dest.dt, src => src.Dt1)

stagep commented 2 weeks ago
public class MapsterConfiguration : IRegister
{
    public void Register(TypeAdapterConfig config)
    {
        config.ForType<Timestamp, DateTime>().MapWith(d => d.ToDateTime());
        config.ForType<DateTime, Timestamp>().MapWith(d => Timestamp.FromDateTime(d));
    }
}
npatersontemenos commented 1 week ago

@stagep Many thanks!