AddConversionByType for time.Time and timestamppb.Timestamp
muradhajiyev opened this issue · 1 comments
muradhajiyev commented
I'm trying to convert from time.Time to timestamppb.Timestamp. This is the code I have written below but it doesn't work, and return such an error panic: reflect.Set: value of type time.Time is not assignable to type *timestamppb.Timestamp
Conversion:
srcType := reflect.TypeOf((*time.Time)(nil)).Elem()
targetType := reflect.TypeOf((**timestamppb.Timestamp)(nil)).Elem()
model.AddConversionByType(srcType, targetType, func(in reflect.Value) (reflect.Value, error) {
t := timestamppb.New(in.Interface().(time.Time))
return reflect.ValueOf(&t), nil
})
struct A {
CreatedAt timeTime
}
struct B {
CreatedAt *timestamppb.Timestamp
}
Is there anyone who can show me what I'm doing wrong here?
cc: @jeevatkm @rmohr @CodinCat
rmohr commented
@muradhajiyev I can barely remember having written this conversion registration feature, but let me see if I can help :)
According to the docs I created I think the registration should look like this:
model.AddConversionByType((*time.Time)(nil), **timestamppb.Timestamp)(nil), func(in reflect.Value) (reflect.Value, error) {
So registering e.g. (*time.Time)(nil)
instead of reflect.TypeOf((*time.Time)(nil)).Elem()
may do the trick.