Documentation
¶
Overview ¶
Package tagged provides small helpers for encoding and decoding tagged unions.
A tagged union payload includes a discriminator field (i.e. "kind") that selects the concrete payload type. This package supports both nested payloads:
{ "kind": "...", "body": { ... } }
and inlined/flattened payloads:
{ "kind": "...", ...variant-specific fields... }
Callers provide a Registry via a Provider to map discriminator values to constructors. MarshalWith, UnmarshalWith, and UnmarshalPtrWith work with any codec that satisfies Serializer, and key names/layout are controlled by a Keys type (for example, DefaultKeys or InlineKeys).
For repeated operations with the same serializer and union layout, use Codec via NewCodec to bind the serializer once and reduce call-site boilerplate.
For decoding failures, callers can use errors.As to inspect typed errors such as MissingKindError, UnknownKindError, and BadPayloadShapeError.
Index ¶
- func MarshalWith[K comparable, P Provider[K], J Keys](s Serializer, u Union[K, P]) ([]byte, error)
- func UnmarshalPtrWith[K comparable, P Provider[K], J Keys](s Serializer, b []byte, u *Union[K, P]) error
- func UnmarshalWith[K comparable, P Provider[K], J Keys](s Serializer, b []byte, u *Union[K, P]) error
- type BadPayloadShapeError
- type BadPayloadShapeReason
- type Codec
- type DefaultKeys
- type InlineKeys
- type InlinePayloadKeys
- type Keys
- type MissingKindError
- type Provider
- type Registry
- type Serializer
- type Union
- type UnknownKindError
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func MarshalWith ¶
func MarshalWith[K comparable, P Provider[K], J Keys](s Serializer, u Union[K, P]) ([]byte, error)
MarshalWith encodes Union using any Serializer and any Keys.
func UnmarshalPtrWith ¶
func UnmarshalPtrWith[K comparable, P Provider[K], J Keys](s Serializer, b []byte, u *Union[K, P]) error
UnmarshalPtrWith decodes Union like UnmarshalWith, but stores pointer payloads as pointers. This is useful for `oneof` patterns that rely on pointer receiver method sets.
func UnmarshalWith ¶
func UnmarshalWith[K comparable, P Provider[K], J Keys](s Serializer, b []byte, u *Union[K, P]) error
UnmarshalWith decodes Union using any Serializer and any Keys.
Types ¶
type BadPayloadShapeError ¶
type BadPayloadShapeError struct {
Kind any
KindKey string
Reason BadPayloadShapeReason
}
BadPayloadShapeError reports a structurally invalid payload shape for the selected encoding mode. KindKey is the discriminator key for the current layout (for example "kind" or "type").
func (*BadPayloadShapeError) Error ¶
func (e *BadPayloadShapeError) Error() string
type BadPayloadShapeReason ¶
type BadPayloadShapeReason string
BadPayloadShapeReason classifies payload shape validation failures produced by this package (currently used by inline payload encoding).
const ( // BadPayloadShapeInlineNonObject means inline mode was requested for a payload // that serialized to a non-object shape (for example a scalar or array). BadPayloadShapeInlineNonObject BadPayloadShapeReason = "inline_non_object" // BadPayloadShapeInlineReservedKey means the inline payload contained the // discriminator key, which would overwrite the tag field. BadPayloadShapeInlineReservedKey BadPayloadShapeReason = "inline_reserved_key" )
type Codec ¶ added in v1.1.0
type Codec[K comparable, P Provider[K], J Keys] struct { // contains filtered or unexported fields }
Codec binds a Serializer for a specific union configuration (K/P/J) so repeated marshal/unmarshal calls do not need to pass the serializer each time.
func NewCodec ¶ added in v1.1.0
func NewCodec[K comparable, P Provider[K], J Keys](s Serializer) Codec[K, P, J]
NewCodec constructs a bound Codec for repeated operations using the same serializer and union layout.
func (Codec[K, P, J]) Marshal ¶ added in v1.1.0
Marshal encodes Union using the Codec's bound Serializer.
type DefaultKeys ¶
type DefaultKeys struct{}
func (DefaultKeys) BodyKeyName ¶
func (DefaultKeys) BodyKeyName() string
func (DefaultKeys) KindKeyName ¶
func (DefaultKeys) KindKeyName() string
type InlineKeys ¶
type InlineKeys struct{}
InlineKeys uses the default "kind" key with inline payload fields.
func (InlineKeys) BodyKeyName ¶
func (InlineKeys) BodyKeyName() string
func (InlineKeys) InlinePayload ¶
func (InlineKeys) InlinePayload() bool
func (InlineKeys) KindKeyName ¶
func (InlineKeys) KindKeyName() string
type InlinePayloadKeys ¶
type InlinePayloadKeys interface {
InlinePayload() bool
}
InlinePayloadKeys is an optional extension for Keys. When implemented and InlinePayload() returns true, payload fields are encoded inline with the tag (for example {"kind":"foo","a":1}) instead of under a body key (for example {"kind":"foo","body":{"a":1}}).
BodyKeyName is ignored in inline mode but remains part of Keys for backwards compatibility.
type MissingKindError ¶
type MissingKindError struct {
Key string
}
MissingKindError reports that the discriminator field was not present. Use errors.As to inspect which key name was expected.
func (*MissingKindError) Error ¶
func (e *MissingKindError) Error() string
type Provider ¶
type Provider[K comparable] interface { Registry() Registry[K] }
type Registry ¶
type Registry[K comparable] map[K]func() any
type Serializer ¶
Serializer is the only thing you need from a codec (json/yaml/toml/cbor/etc).
type Union ¶
type Union[K comparable, P Provider[K]] struct { Type K Value any }
type UnknownKindError ¶
type UnknownKindError struct {
Kind any
}
UnknownKindError reports that the discriminator value was decoded but not found in the provider registry. Kind preserves the decoded value.
func (*UnknownKindError) Error ¶
func (e *UnknownKindError) Error() string