resource

package
v0.4.7 Latest Latest
Warning

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

Go to latest
Published: Dec 22, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package resource supplies a generic Access[K, V] interface for CRUD operations on key-value pairs, backed by an in-memory and JSON file implementation.

Index

Constants

View Source
const (
	ErrorResourceAlreadyExists = "Resource already exists"
	ErrorResourceNotFound      = "Resource not found"
)

Variables

This section is empty.

Functions

func NewInMemoryAccess

func NewInMemoryAccess[K comparable, V any]() *inMemoryAccess[K, V]

NewInMemoryAccess creates a new in-memory access.

func NewJsonFileAccess added in v0.1.51

func NewJsonFileAccess[K comparable, V any](path string) *jsonFileAccess[K, V]

NewJsonFileAccess creates a new json file access.

func NewMockAccess added in v0.2.14

func NewMockAccess[K, V any]() *mockAccess[K, V]

NewMockAccess creates a new instance of MockAccess[K, V].

func NewSqliteAccess added in v0.2.25

func NewSqliteAccess[K comparable, V any](db *sql.DB) *sqliteAccess[K, V]

NewSqliteAccess creates a new instance of sqliteAccess.

func NewYamlFileAccess added in v0.4.1

func NewYamlFileAccess[K comparable, V any](path string) *yamlFileAccess[K, V]

NewYamlFileAccess creates a new yaml file access.

Types

type Access

type Access[K, V any] interface {
	Create(ctx context.Context, key K, value V) error
	Read(ctx context.Context, key K) (*V, error)
	ReadAll(ctx context.Context) ([]V, error)
	Update(ctx context.Context, key K, value V) error
	Delete(ctx context.Context, key K) error
}

Access specifies the CRUD operations for a resource using generics. It supports context.Context for cancellation and timeouts.

type IndexFunc added in v0.4.7

type IndexFunc[V any] func(V) string

IndexFunc extracts an index key from a value. Returns an empty string if the value should not be indexed.

type IndexedAccess added in v0.4.7

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

IndexedAccess wraps a resource.Access and maintains secondary indexes. It supports both unique and non-unique indexes (stored as lists of keys).

func NewIndexedAccess added in v0.4.7

func NewIndexedAccess[K comparable, V any](access Access[K, V]) *IndexedAccess[K, V]

NewIndexedAccess creates a new indexed access wrapper.

func (*IndexedAccess[K, V]) AddIndex added in v0.4.7

func (a *IndexedAccess[K, V]) AddIndex(name string, fn IndexFunc[V])

AddIndex adds a new secondary index. name is the unique name of the index. fn is the function to extract the index key from the value.

func (*IndexedAccess[K, V]) Create added in v0.4.7

func (a *IndexedAccess[K, V]) Create(ctx context.Context, key K, value V) error

Create stores a new value and updates secondary indexes.

func (*IndexedAccess[K, V]) Delete added in v0.4.7

func (a *IndexedAccess[K, V]) Delete(ctx context.Context, key K) error

Delete removes a value and its secondary index entries.

func (*IndexedAccess[K, V]) FindByIndex added in v0.4.7

func (a *IndexedAccess[K, V]) FindByIndex(ctx context.Context, indexName string, indexKey string) ([]V, error)

FindByIndex retrieves values by a secondary index key.

func (*IndexedAccess[K, V]) FindOneByIndex added in v0.4.7

func (a *IndexedAccess[K, V]) FindOneByIndex(ctx context.Context, indexName string, indexKey string) (*V, bool)

FindOneByIndex retrieves a single value by a secondary index key. Returns nil, false if not found.

func (*IndexedAccess[K, V]) Read added in v0.4.7

func (a *IndexedAccess[K, V]) Read(ctx context.Context, key K) (*V, error)

Read retrieves a value by its primary key.

func (*IndexedAccess[K, V]) ReadAll added in v0.4.7

func (a *IndexedAccess[K, V]) ReadAll(ctx context.Context) ([]V, error)

ReadAll retrieves all values.

func (*IndexedAccess[K, V]) Update added in v0.4.7

func (a *IndexedAccess[K, V]) Update(ctx context.Context, key K, value V) error

Update updates an existing value and its secondary indexes.

Jump to

Keyboard shortcuts

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