Skip to content

Commit

Permalink
chore: add UnionUnmarshaler for responses that are interfaces (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-app[bot] committed Feb 5, 2025
1 parent 2a829ce commit 6f9193d
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions internal/apijson/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 6f9193d

Please sign in to comment.