Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
npatersontemenos opened this issue Jun 17, 2024 · 5 comments
Open

Comments

@npatersontemenos
Copy link

npatersontemenos commented Jun 17, 2024

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
Copy link

stagep commented Jun 23, 2024

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
Copy link
Author

@stagep Many thanks!

@Exzept1on
Copy link

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));
    }
}

how to make the same, but with nullable datetime? because using nullable datetime throws the same error which described in topic

@stagep
Copy link

stagep commented Jul 10, 2024

If DateTime is nullable then you have 2 options. Use a nullable Timestamp or create your own gRPC message that can handle the nullability of DateTime with a IsNull property. I use this approach.

config.ForType<Timestamp?, DateTime?>().MapWith(d => d == null ? null : d.ToDateTime());
config.ForType<DateTime?, Timestamp?>().MapWith(d => d.HasValue ? Timestamp.FromDateTime(d.Value) : null);

nullable_date_time.proto:

message NullableDateTimeMessage
{
	int32 year = 1;
	int32 month = 2;
	int32 day = 3;
	int32 hour = 4;
	int32 minute = 5;
	int32 second = 6;
	bool is_null = 7;
}

and the mapping

config.ForType<NullableDateTimeMessage, DateTime?>().MapWith(d => d.IsNull ? null : new DateTime(d.Year, d.Month, d.Day, d.Hour, d.Minute, d.Second);
config.ForType<DateTime?, NullableDateTimeMessage>().MapWith(d => d.HasValue ?
            new NullableDateTimeMessage { Year = d.Value.Year, Month = d.Value.Month, Day = d.Value.Day, Hour = d.Value.Hour, Minute = d.Value.Minute, Second = d.Value.Second } :
            new NullableDateTimeMessage { IsNull = true });

@Exzept1on
Copy link

Thanks for answer.

config.ForType<Timestamp?, DateTime?>().MapWith(d => d == null ? null : d.ToDateTime());

config.ForType<DateTime?, Timestamp?>().MapWith(d => d.HasValue ? DateTime.SpecifyKind(d.Value, DateTimeKind.Utc).ToTimestamp() : null);

This code works for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants