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
- func NewInMemoryAccess[K comparable, V any]() *inMemoryAccess[K, V]
- func NewJsonFileAccess[K comparable, V any](path string) *jsonFileAccess[K, V]
- func NewMockAccess[K, V any]() *mockAccess[K, V]
- func NewSqliteAccess[K comparable, V any](db *sql.DB) *sqliteAccess[K, V]
- func NewYamlFileAccess[K comparable, V any](path string) *yamlFileAccess[K, V]
- type Access
- type IndexFunc
- type IndexedAccess
- func (a *IndexedAccess[K, V]) AddIndex(name string, fn IndexFunc[V])
- func (a *IndexedAccess[K, V]) Create(ctx context.Context, key K, value V) error
- func (a *IndexedAccess[K, V]) Delete(ctx context.Context, key K) error
- func (a *IndexedAccess[K, V]) FindByIndex(ctx context.Context, indexName string, indexKey string) ([]V, error)
- func (a *IndexedAccess[K, V]) FindOneByIndex(ctx context.Context, indexName string, indexKey string) (*V, bool)
- func (a *IndexedAccess[K, V]) Read(ctx context.Context, key K) (*V, error)
- func (a *IndexedAccess[K, V]) ReadAll(ctx context.Context) ([]V, error)
- func (a *IndexedAccess[K, V]) Update(ctx context.Context, key K, value V) error
Constants ¶
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
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.