batch

package
v0.0.0-...-dd0b383 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2024 License: BSD-3-Clause Imports: 5 Imported by: 0

Documentation

Overview

package batch simplifies data loading for one-to-one and one-to-many relationships.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrKeyNotFound         = errors.New("batch: key not found")
	ErrMultipleValuesFound = errors.New("batch: multiple values found")
	ErrClosed              = errors.New("batch: already closed")
	ErrNilReference        = errors.New("batch: nil reference is passed in")
)

Functions

func Copier

func Copier[V any](v []V) ([]V, error)

Types

type Loader

type Loader[K comparable, V any] struct {
	// contains filtered or unexported fields
}

func New

func New[K comparable, V any](opt Option[K, V]) *Loader[K, V]

func (*Loader[K, V]) Load

func (l *Loader[K, V]) Load(v *V, k K) error

Load ensures that exactly one result will be loaded. Suitable for loading data with one-to-one relationships. Returns error if the batchFn does not return exactly 1 result. If the same key is loaded multiple times, the result will be deep-copied before returned.

func (*Loader[K, V]) LoadMany

func (l *Loader[K, V]) LoadMany(v *[]V, ks ...K) error

LoadMany returns zero, one or many results. Suitable for loading data with one-to-many relationships. If the same key is loaded multiple times, the result will be deep-copied before returned.

func (*Loader[K, V]) Wait

func (l *Loader[K, V]) Wait() error

type Option

type Option[K, V any] struct {
	BatchFn batchFn[K, V]
	KeyFn   keyFn[K, V]
	HookFn  hookFn[V]
}

Jump to

Keyboard shortcuts

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