diff --git a/protocodec/backend_getter_v2.go b/protocodec/backend_getter_v2.go index 4c309660..0d4cf108 100644 --- a/protocodec/backend_getter_v2.go +++ b/protocodec/backend_getter_v2.go @@ -30,19 +30,22 @@ func (b backendGetterV2[C, T]) Get(ctx context.Context, key string, dest galaxyc if bgErr != nil { return bgErr } - switch d := dest.(type) { - case *CodecV2[C, T]: + if d, ok := dest.(*CodecV2[C, T]); ok { d.Set(out) - default: - vs, mErr := proto.Marshal(out) - if mErr != nil { - return fmt.Errorf("failed to marshal value as bytes: %w", mErr) - } - - if uErr := dest.UnmarshalBinary(vs); uErr != nil { - return fmt.Errorf("destination codec (type %T) Unmarshal failed: %w", dest, uErr) - } + return nil } + return b.setSlow(out, dest) +} + +func (b backendGetterV2[C, T]) setSlow(out T, dest galaxycache.Codec) error { + vs, mErr := proto.Marshal(out) + if mErr != nil { + return fmt.Errorf("failed to marshal value as bytes: %w", mErr) + } + + if uErr := dest.UnmarshalBinary(vs); uErr != nil { + return fmt.Errorf("destination codec (type %T) Unmarshal failed: %w", dest, uErr) + } return nil } diff --git a/protocodec/galaxywrap_v2.go b/protocodec/galaxywrap_v2.go index 5529845c..ff24574b 100644 --- a/protocodec/galaxywrap_v2.go +++ b/protocodec/galaxywrap_v2.go @@ -10,11 +10,11 @@ import ( // GalaxyGet is a simple wrapper around a Galaxy.Get method-call that takes // care of constructing the protocodec.CodecV2, etc. (making the interface more idiomatic for Go) -func GalaxyGet[C any, T pointerMessage[C]](ctx context.Context, g *galaxycache.Galaxy, key string) (T, error) { - pc := NewV2[C, T]() - getErr := g.Get(ctx, key, &pc) +func GalaxyGet[C any, T pointerMessage[C]](ctx context.Context, g *galaxycache.Galaxy, key string) (m T, getErr error) { + pc := CodecV2[C, T]{} + getErr = g.Get(ctx, key, &pc) if getErr != nil { - return nil, getErr + return // use named return values to bring the inlining cost down } - return pc.Get(), nil + return pc.msg, nil }