viewstore

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2026 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package viewstore binds evt.ViewRepository to JSON values, an entity type, and optional decode factories. Codec accepts keys per call; Single binds a primary-key function.

List methods skip invalid payloads and report them to an optional DecodeErrorHandler. Use evt.ListJSONViewsByPK or evt.ListJSONViewsByEntityType for strict decoding.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Codec

type Codec[T any] struct {
	// contains filtered or unexported fields
}

Codec binds an evt.ViewRepository to a JSON-encoded value type T plus an EntityType. Callers supply pk/sk/entityID per call.

func New

func New[T any](repo evt.ViewRepository, entityType evt.EntityType) *Codec[T]

New constructs a Codec for value-type T. Use NewWithFactory when T is a pointer-backed entity that needs constructor-injected dependencies.

func NewWithFactory

func NewWithFactory[T any](repo evt.ViewRepository, entityType evt.EntityType, factory func() T) *Codec[T]

NewWithFactory constructs a Codec that uses factory to produce decode destinations. This is required when T is a pointer to an aggregate that needs projector wiring before json.Unmarshal runs.

func (*Codec[T]) EntityType

func (c *Codec[T]) EntityType() evt.EntityType

EntityType returns the entity type bound to this codec.

func (*Codec[T]) Get

func (c *Codec[T]) Get(ctx context.Context, pk string) (T, bool, error)

Get retrieves and decodes the view at pk. Returns (zero, false, nil) when no view exists.

func (*Codec[T]) ListAll

func (c *Codec[T]) ListAll(ctx context.Context, onDecodeError DecodeErrorHandler) ([]T, error)

ListAll returns all valid views for the bound entity type, skipping payloads that fail to decode. onDecodeError, if non-nil, is invoked for each skipped payload.

func (*Codec[T]) ListByPK

func (c *Codec[T]) ListByPK(ctx context.Context, pk string, onDecodeError DecodeErrorHandler) ([]T, error)

ListByPK returns valid views matching pk, skipping payloads that fail to decode. onDecodeError, if non-nil, is invoked for each skipped payload.

func (*Codec[T]) Put

func (c *Codec[T]) Put(ctx context.Context, pk, sk string, entityID evt.EntityID, value T) error

Put writes value at the given pk/sk with the supplied entity ID under the bound EntityType.

func (*Codec[T]) PutAt

func (c *Codec[T]) PutAt(ctx context.Context, pk string, entityID evt.EntityID, value T) error

PutAt writes value at the given pk with an empty sort key.

func (*Codec[T]) Repo

func (c *Codec[T]) Repo() evt.ViewRepository

Repo returns the underlying view repository for callers that need raw access (e.g., existence probes that should not pay decode cost).

type DecodeErrorHandler

type DecodeErrorHandler func(view *evt.SerializedView, err error)

DecodeErrorHandler is invoked for each view skipped during a List call. view may be nil if the view itself was nil; err is the underlying decode error (typically *evt.JSONViewDecodeError).

type Single

type Single[K any, T any] struct {
	// contains filtered or unexported fields
}

Single is a Codec[T] augmented with caller-supplied pkFor / entityIDOf functions, enabling concise Get(id) / Put(value) helpers for the common "one PK per entity ID" view shape.

func NewSingle

func NewSingle[K any, T any](
	repo evt.ViewRepository,
	entityType evt.EntityType,
	pkFor func(K) string,
	entityIDOf func(T) evt.EntityID,
) *Single[K, T]

NewSingle constructs a Single store. pkFor must be non-nil; entityIDOf is optional and only required by the value-driven Put method.

func NewSingleWithFactory

func NewSingleWithFactory[K any, T any](
	repo evt.ViewRepository,
	entityType evt.EntityType,
	pkFor func(K) string,
	entityIDOf func(T) evt.EntityID,
	factory func() T,
) *Single[K, T]

NewSingleWithFactory is NewSingle plus a factory for pointer-backed T.

func (*Single[K, T]) Codec

func (s *Single[K, T]) Codec() *Codec[T]

Codec exposes the underlying Codec for callers that need to issue lookups outside the bound pkFor (e.g., legacy-key fallback reads).

func (*Single[K, T]) Get

func (s *Single[K, T]) Get(ctx context.Context, id K) (T, bool, error)

Get retrieves the view keyed by id.

func (*Single[K, T]) ListAll

func (s *Single[K, T]) ListAll(ctx context.Context, onDecodeError DecodeErrorHandler) ([]T, error)

ListAll returns all valid views for the bound entity type.

func (*Single[K, T]) Put

func (s *Single[K, T]) Put(ctx context.Context, id K, value T) error

Put writes value, deriving pk from entityIDOf(value) (which must be set). Use PutAt when the pk derives from inputs other than the value.

Jump to

Keyboard shortcuts

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