Skip to content

Commit d6fff80

Browse files
chore: add UnionUnmarshaler for responses that are interfaces (#6)
1 parent d05b1fd commit d6fff80

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

Diff for: internal/apijson/registry.go

+10
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,13 @@ func RegisterUnion(typ reflect.Type, discriminator string, variants ...UnionVari
2929
unionVariants[variant.Type] = typ
3030
}
3131
}
32+
33+
// Useful to wrap a union type to force it to use [apijson.UnmarshalJSON] since you cannot define an
34+
// UnmarshalJSON function on the interface itself.
35+
type UnionUnmarshaler[T any] struct {
36+
Value T
37+
}
38+
39+
func (c *UnionUnmarshaler[T]) UnmarshalJSON(buf []byte) error {
40+
return UnmarshalRoot(buf, &c.Value)
41+
}

0 commit comments

Comments
 (0)