From 6f9193db34ad3858fbfe2e857adfce4648b1aa8e Mon Sep 17 00:00:00 2001 From: "stainless-app[bot]" <142633134+stainless-app[bot]@users.noreply.github.com> Date: Wed, 5 Feb 2025 18:03:43 +0000 Subject: [PATCH] chore: add UnionUnmarshaler for responses that are interfaces (#93) --- internal/apijson/registry.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/internal/apijson/registry.go b/internal/apijson/registry.go index 2ea00ae..119cc5f 100644 --- a/internal/apijson/registry.go +++ b/internal/apijson/registry.go @@ -29,3 +29,13 @@ func RegisterUnion(typ reflect.Type, discriminator string, variants ...UnionVari unionVariants[variant.Type] = typ } } + +// Useful to wrap a union type to force it to use [apijson.UnmarshalJSON] since you cannot define an +// UnmarshalJSON function on the interface itself. +type UnionUnmarshaler[T any] struct { + Value T +} + +func (c *UnionUnmarshaler[T]) UnmarshalJSON(buf []byte) error { + return UnmarshalRoot(buf, &c.Value) +}