storage

package
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2023 License: MIT Imports: 17 Imported by: 2

Documentation

Overview

Package storage provides a simple interface to store and retrieve data from multiple data stores.

Index

Constants

View Source
const (
	DefaultMetricCounterLabel = "counter"
	Type                      = "storage"

	// Operation names.
	OperationCount    = "count"
	OperationCreate   = "create"
	OperationDelete   = "delete"
	OperationList     = "list"
	OperationRetrieve = "retrieve"
	OperationUpdate   = "update"
)

Type is the type of the entity regarding the framework. It is used to for example, to identify the entity in the logs, metrics, and for tracing.

Variables

View Source
var (
	// ErrRequiredPostHook is the error returned when the post-hook function is
	// missing.
	ErrRequiredPostHook = customerror.NewRequiredError("post-hook function", customerror.WithCode("ERR_REQUIRED_POST_HOOK"))

	// ErrRequiredPreHook is the error returned when the pre-hook function is
	// missing.
	ErrRequiredPreHook = customerror.NewRequiredError("pre-hook function", customerror.WithCode("ERR_REQUIRED_PRE_HOOK"))
)

Functions

func ParseToStruct

func ParseToStruct(from, to any) error

ParseToStruct parses the given JSON (`from`) to struct (`to`).

Types

type Func

type Func[T any] func(o *Options[T]) error

Func allows to set options.

func WithPostHook

func WithPostHook[T any](fn HookFunc[T]) Func[T]

WithPostHook set the post-hook function.

func WithPreHook

func WithPreHook[T any](fn HookFunc[T]) Func[T]

WithPreHook set the pre-hook function.

type HookFunc

type HookFunc[T any] func(ctx context.Context, strg IStorage, id, target string, param T) error

HookFunc specifies the function that will be called before and after the operation.

type IStorage

type IStorage interface {
	// Count counts data.
	Count(ctx context.Context, target string, prm *count.Count, options ...Func[*count.Count]) (int64, error)

	// Delete removes data.
	Delete(ctx context.Context, id, target string, prm *delete.Delete, options ...Func[*delete.Delete]) error

	// Get retrieves data.
	Get(ctx context.Context, id, target string, v any, prm *get.Get, options ...Func[*get.Get]) error

	// List data.
	List(ctx context.Context, target string, v any, prm *list.List, options ...Func[*list.List]) error

	// Set stores data.
	Set(ctx context.Context, id, target string, v any, prm *set.Set, options ...Func[*set.Set]) (string, error)

	// Update updates data.
	Update(ctx context.Context, id, target string, v any, prm *update.Update, options ...Func[*update.Update]) error

	// GetName returns the storage name.
	GetName() string

	// GetClient returns the storage client. Use that to interact with the
	// underlying storage client.
	GetClient() any
}

IStorage defines the data access layer interface.

The Data Access Layer (DAL) is generally more abstract and wider than the Data Access Object (DAO).

The DAL is responsible for providing a consistent and unified interface to access data from different data sources (such as databases, files, or web services) regardless of their underlying implementation details. It abstracts away the complexity of data access by providing a simple, unified interface that shields the rest of the application from the underlying data storage details.

On the other hand, the DAO is typically more specific to a particular data source, such as a particular database management system (such as MySQL or MongoDB) or a particular web service API. Its primary responsibility is to abstract away the details of the underlying data source and provide a simplified interface for performing CRUD (Create, Read, Update, Delete) operations on that data source.

So while both DAL and DAO are used to abstract away the complexities of data access, the DAL is generally wider and more abstract because it deals with multiple data sources, while the DAO is more specific and deals with a particular data source.

type Mock

type Mock struct {
	MockCount  func(ctx context.Context, target string, prm *count.Count, options ...Func[*count.Count]) (int64, error)
	MockDelete func(ctx context.Context, id, target string, prm *delete.Delete, options ...Func[*delete.Delete]) error
	MockGet    func(ctx context.Context, id, target string, v any, prm *get.Get, options ...Func[*get.Get]) error
	MockList   func(ctx context.Context, target string, v any, prm *list.List, opts ...Func[*list.List]) error
	MockSet    func(ctx context.Context, id, target string, v any, prm *set.Set, options ...Func[*set.Set]) (string, error)
	MockUpdate func(ctx context.Context, id, target string, v any, prm *update.Update, opts ...Func[*update.Update]) error

	MockGetClient func() any
	MockGetName   func() string
}

Mock is a struct which satisfies the storage.IStorage interface.

func (*Mock) Count

func (m *Mock) Count(ctx context.Context, target string, prm *count.Count, options ...Func[*count.Count]) (int64, error)

Count counts data.

func (*Mock) Delete

func (m *Mock) Delete(ctx context.Context, id, target string, prm *delete.Delete, options ...Func[*delete.Delete]) error

Delete removes data.

func (*Mock) Get

func (m *Mock) Get(ctx context.Context, id, target string, v any, prm *get.Get, options ...Func[*get.Get]) error

Get retrieves data.

func (*Mock) GetClient

func (m *Mock) GetClient() any

GetClient returns the storage client. Use that to interact with the underlying storage client.

func (*Mock) GetName added in v0.0.6

func (m *Mock) GetName() string

GetName returns the storage name.

func (*Mock) List

func (m *Mock) List(ctx context.Context, target string, v any, prm *list.List, opts ...Func[*list.List]) error

List data.

func (*Mock) Set

func (m *Mock) Set(ctx context.Context, id, target string, v any, prm *set.Set, options ...Func[*set.Set]) (string, error)

Set stores data.

func (*Mock) Update

func (m *Mock) Update(ctx context.Context, id, target string, v any, prm *update.Update, opts ...Func[*update.Update]) error

Update updates data.

type Options

type Options[T any] struct {
	// PreHookFunc is the function which runs before the operation.
	PreHookFunc HookFunc[T] `json:"-"`

	// PostHookFunc is the function which runs after the operation.
	PostHookFunc HookFunc[T] `json:"-"`
}

Options for operations.

func NewOptions

func NewOptions[T any]() (*Options[T], error)

NewOptions creates Options.

type Storage

type Storage struct {
	// Logger.
	Logger sypl.ISypl `json:"-" validate:"required"`

	// Name of the storage type.
	Name string `json:"name" validate:"required,lowercase,gte=1"`
	// contains filtered or unexported fields
}

Storage definition.

func New

func New(name string) (*Storage, error)

New returns a new Storage.

func (*Storage) GetCounterCount

func (s *Storage) GetCounterCount() *expvar.Int

GetCounterCount returns the counterCount metric.

func (*Storage) GetCounterDelete

func (s *Storage) GetCounterDelete() *expvar.Int

GetCounterDelete returns the counterDelete metric.

func (*Storage) GetCounterGet

func (s *Storage) GetCounterGet() *expvar.Int

GetCounterGet returns the counterGet metric.

func (*Storage) GetCounterList

func (s *Storage) GetCounterList() *expvar.Int

GetCounterList returns the counterList metric.

func (*Storage) GetCounterSet

func (s *Storage) GetCounterSet() *expvar.Int

GetCounterSet returns the counterSet metric.

func (*Storage) GetCounterUpdate

func (s *Storage) GetCounterUpdate() *expvar.Int

GetCounterUpdate returns the counterUpdate metric.

func (*Storage) GetLogger

func (s *Storage) GetLogger() sypl.ISypl

GetLogger returns the logger.

func (*Storage) GetName

func (s *Storage) GetName() string

GetName returns the storage name.

func (*Storage) GetType

func (s *Storage) GetType() string

GetType returns its type.

Jump to

Keyboard shortcuts

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