storage

package
v0.0.16 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2023 License: MIT Imports: 18 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"
)

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, data any, param T) error

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

type IStorage

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

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

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

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

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

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

	// GetType returns its type.
	GetType() string

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

	// GetLogger returns the logger.
	GetLogger() sypl.ISypl

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

	// GetCounterCounted returns the metric.
	GetCounterCounted() *expvar.Int

	// GetCounterCountedFailed returns the metric.
	GetCounterCountedFailed() *expvar.Int

	// GetCounterDeleted returns the metric.
	GetCounterDeleted() *expvar.Int

	// GetCounterDeletedFailed returns the metric.
	GetCounterDeletedFailed() *expvar.Int

	// GetCounterRetrieved returns the metric.
	GetCounterRetrieved() *expvar.Int

	// GetCounterRetrievedFailed returns the metric.
	GetCounterRetrievedFailed() *expvar.Int

	// GetCounterListed returns the metric.
	GetCounterListed() *expvar.Int

	// GetCounterListedFailed returns the metric.
	GetCounterListedFailed() *expvar.Int

	// GetCounterPingFailed returns the metric.
	GetCounterPingFailed() *expvar.Int

	// GetCounterCreated returns the metric.
	GetCounterCreated() *expvar.Int

	// GetCounterCreatedFailed returns the metric.
	GetCounterCreatedFailed() *expvar.Int

	// GetCounterUpdated returns the metric.
	GetCounterUpdated() *expvar.Int

	// GetCounterUpdatedFailed returns the metric.
	GetCounterUpdatedFailed() *expvar.Int
}

type Mock

type Mock struct {
	MockCount    func(ctx context.Context, target string, prm *count.Count, options ...Func[*count.Count]) (int64, error)
	MockCreate   func(ctx context.Context, id, target string, v any, prm *create.Create, options ...Func[*create.Create]) (string, error)
	MockDelete   func(ctx context.Context, id, target string, prm *delete.Delete, options ...Func[*delete.Delete]) error
	MockList     func(ctx context.Context, target string, v any, prm *list.List, opts ...Func[*list.List]) error
	MockRetrieve func(ctx context.Context, id, target string, v any, prm *retrieve.Retrieve, options ...Func[*retrieve.Retrieve]) error
	MockUpdate   func(ctx context.Context, id, target string, v any, prm *update.Update, opts ...Func[*update.Update]) error

	MockGetClient func() any
	MockGetLogger func() sypl.ISypl
	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 *retrieve.Retrieve, options ...Func[*retrieve.Retrieve]) 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) GetLogger added in v0.0.12

func (m *Mock) GetLogger() sypl.ISypl

GetLogger returns the logger.

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 *create.Create, options ...Func[*create.Create]) (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 Operation added in v0.0.11

type Operation string

Operation is the operation name.

const (
	OperationCount    Operation = "count"
	OperationCreate   Operation = "create"
	OperationDelete   Operation = "delete"
	OperationList     Operation = "list"
	OperationRetrieve Operation = "retrieve"
	OperationUpdate   Operation = "update"
)

func (Operation) String added in v0.0.11

func (o Operation) String() string

String implements the Stringer interface.

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(ctx context.Context, name string) (*Storage, error)

New returns a new Storage.

func (*Storage) GetCounterCounted added in v0.0.15

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

GetCounterCounted returns the metric.

func (*Storage) GetCounterCountedFailed added in v0.0.15

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

GetCounterCountedFailed returns the metric.

func (*Storage) GetCounterCreated added in v0.0.15

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

GetCounterCreated returns the metric.

func (*Storage) GetCounterCreatedFailed added in v0.0.15

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

GetCounterCreatedFailed returns the metric.

func (*Storage) GetCounterDeleted added in v0.0.15

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

GetCounterDeleted returns the metric.

func (*Storage) GetCounterDeletedFailed added in v0.0.15

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

GetCounterDeletedFailed returns the metric.

func (*Storage) GetCounterListed added in v0.0.15

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

GetCounterListed returns the metric.

func (*Storage) GetCounterListedFailed added in v0.0.15

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

GetCounterListedFailed returns the metric.

func (*Storage) GetCounterPingFailed added in v0.0.15

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

GetCounterPingFailed returns the metric.

func (*Storage) GetCounterRetrieved added in v0.0.15

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

GetCounterRetrieved returns the metric.

func (*Storage) GetCounterRetrievedFailed added in v0.0.15

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

GetCounterRetrievedFailed returns the metric.

func (*Storage) GetCounterUpdated added in v0.0.15

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

GetCounterUpdated returns the metric.

func (*Storage) GetCounterUpdatedFailed added in v0.0.15

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

GetCounterUpdatedFailed returns the 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