crash if not correct type
All checks were successful
Build and Publish / BuildAndDeployAmd64 (push) Successful in 43s
Build and Publish / BuildAndDeployArm64 (push) Successful in 5m52s

This commit is contained in:
matst80
2025-12-03 10:22:41 +01:00
parent c7049793e2
commit ca7d23f122

View File

@@ -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)
// and ensures we always peel off the pointer layer for proto messages.
create := func() proto.Message {
m := new(T)
return *m
var t T
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()
rt := reflect.TypeOf(instance)
if rt.Kind() == reflect.Ptr {
if rt.Kind() == reflect.Pointer {
rt = rt.Elem()
}
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.
rv := reflect.ValueOf(m)
if rv.Kind() == reflect.Ptr && rv.IsNil() {
if rv.Kind() == reflect.Pointer && rv.IsNil() {
continue
}
rt := indirectType(reflect.TypeOf(m))