polymorphic

package
v0.1.0-alpha.52 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 23, 2025 License: MIT Imports: 3 Imported by: 21

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ClearRegistry

func ClearRegistry()

ClearRegistry removes all registered factories. Useful in tests to reset global state.

func MarshalPolymorphicJSON

func MarshalPolymorphicJSON(obj Polymorphic) ([]byte, error)

MarshalPolymorphicJSON is a helper that marshals a Polymorphic object into the envelope format used by this package.

func Register

func Register[T Polymorphic](factory func() T)

Register registers a factory for a Polymorphic type using a factory function that returns the concrete instance. The registration uses the discriminator value returned by the instance produced by the factory.

func RegisterType

func RegisterType[T any]()

RegisterType is a convenience helper that registers a type T where *T implements Polymorphic. It will panic if *T does not implement the Polymorphic interface.

func RegisterWithDiscriminator

func RegisterWithDiscriminator(discriminator string, factory TypeFactory)

RegisterWithDiscriminator stores a factory function under the given discriminator. The factory should return a pointer to a zero-value instance of the concrete type.

Types

type Envelope

type Envelope struct {
	Discriminator string `json:"$type"`
	Content       any    `json:"-"`
}

Envelope represents a marshaled polymorphic value. The `$type` field contains the discriminator and `Content` holds the concrete value after unmarshaling.

func NewEnvelope

func NewEnvelope(obj Polymorphic) *Envelope

NewEnvelope creates an Envelope wrapping a polymorphic object. The Envelope contains the discriminator value and the content to be marshaled. The discriminator is obtained by calling obj.GetDiscriminator().

func UnmarshalPolymorphicJSON

func UnmarshalPolymorphicJSON(data []byte) (*Envelope, error)

UnmarshalPolymorphicJSON unmarshals data into an Envelope and resolves the contained polymorphic value using the registered factory for the discriminator value.

func (*Envelope) MarshalJSON

func (e *Envelope) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler for Envelope. It validates that the discriminator is registered and marshals the content into a small envelope object containing `$type` and `content`.

func (*Envelope) UnmarshalJSON

func (e *Envelope) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler for Envelope. It expects a JSON object with a `$type` discriminator and a `content` field. The content is unmarshaled into a concrete instance returned by the registered factory for that discriminator.

type Polymorphic

type Polymorphic interface {
	GetDiscriminator() string
}

Polymorphic is implemented by types that expose a discriminator string used to identify the concrete implementation when serializing or deserializing polymorphic values.

func CreateInstance

func CreateInstance(discriminator string) (Polymorphic, error)

CreateInstance creates a new instance for the given discriminator. It returns the instance as a Polymorphic interface or an error if the discriminator is not registered.

type TypeFactory

type TypeFactory = func() any

TypeFactory creates instances of registered types.

func LoadFactory

func LoadFactory(discriminator string) (TypeFactory, error)

LoadFactory returns the factory function registered for the discriminator, or an error if there is none.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL