crash if not correct type
This commit is contained in:
@@ -104,12 +104,17 @@ func NewMutation[V any, T proto.Message](handler func(*V, T) error) *RegisteredM
|
|||||||
// This avoids relying on reflect.TypeFor (which can yield unexpected results in some toolchains)
|
// This avoids relying on reflect.TypeFor (which can yield unexpected results in some toolchains)
|
||||||
// and ensures we always peel off the pointer layer for proto messages.
|
// and ensures we always peel off the pointer layer for proto messages.
|
||||||
create := func() proto.Message {
|
create := func() proto.Message {
|
||||||
m := new(T)
|
var t T
|
||||||
return *m
|
rt := reflect.TypeOf(t)
|
||||||
|
if rt != nil && rt.Kind() == reflect.Pointer {
|
||||||
|
return reflect.New(rt.Elem()).Interface().(proto.Message)
|
||||||
|
}
|
||||||
|
log.Fatalf("expected to create proto message got %+v", rt)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
instance := create()
|
instance := create()
|
||||||
rt := reflect.TypeOf(instance)
|
rt := reflect.TypeOf(instance)
|
||||||
if rt.Kind() == reflect.Ptr {
|
if rt.Kind() == reflect.Pointer {
|
||||||
rt = rt.Elem()
|
rt = rt.Elem()
|
||||||
}
|
}
|
||||||
return &RegisteredMutation[V, T]{
|
return &RegisteredMutation[V, T]{
|
||||||
@@ -222,7 +227,7 @@ func (r *ProtoMutationRegistry) Apply(ctx context.Context, grain any, msg ...pro
|
|||||||
}
|
}
|
||||||
// Typed nil: interface holds concrete proto message type whose pointer value is nil.
|
// Typed nil: interface holds concrete proto message type whose pointer value is nil.
|
||||||
rv := reflect.ValueOf(m)
|
rv := reflect.ValueOf(m)
|
||||||
if rv.Kind() == reflect.Ptr && rv.IsNil() {
|
if rv.Kind() == reflect.Pointer && rv.IsNil() {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
rt := indirectType(reflect.TypeOf(m))
|
rt := indirectType(reflect.TypeOf(m))
|
||||||
|
|||||||
Reference in New Issue
Block a user