mockstore

package
v0.4.6 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2022 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type QueryChange

type QueryChange struct {
	IDValue        string
	BeforeValue    interface{}
	AfterValue     interface{}
	OnAffectsQuery func(q url.Values) bool
	OnEvents       func(q url.Values) ([]store.ResultEvent, bool, error)
}

QueryChange mocks a change in a resource that affects the query.

It implements the store.QueryChange interface.

func (QueryChange) After

func (qc QueryChange) After() interface{}

After returns the AfterValue.

func (QueryChange) Before

func (qc QueryChange) Before() interface{}

Before returns the BeforeValue.

func (QueryChange) Events

func (qc QueryChange) Events(q url.Values) ([]store.ResultEvent, bool, error)

Events calls the OnEvents callback, or returns nil and false if OnEvents is nil.

func (QueryChange) ID

func (qc QueryChange) ID() string

ID returns the IDValue string.

type QueryStore

type QueryStore struct {
	// OnQueryChangeCallbacks contains callbacks added with OnQueryChange.
	OnQueryChangeCallbacks []func(store.QueryChange)

	// OnQuery handles calls to Query.
	OnQuery func(q url.Values) (interface{}, error)
}

QueryStore mocks a query store.

It implements the store.QueryStore interface.

func NewQueryStore

func NewQueryStore(cb func(q url.Values) (interface{}, error)) *QueryStore

NewQueryStore creates a new QueryStore and initializes it.

func (*QueryStore) OnQueryChange

func (qs *QueryStore) OnQueryChange(cb func(store.QueryChange))

OnQueryChange adds a listener callback that is triggered using the TriggerQueryChange.

func (*QueryStore) Query

func (qs *QueryStore) Query(q url.Values) (interface{}, error)

Query returns a collection of references to store ID's matching the query. If error is non-nil the reference slice is nil.

func (*QueryStore) TriggerQueryChange

func (qs *QueryStore) TriggerQueryChange(qc QueryChange)

TriggerQueryChange call all OnQueryChange listeners with the QueryChange.

type Store

type Store struct {
	// OnChangeCallbacks contains callbacks added with OnChange.
	OnChangeCallbacks []func(id string, before, after interface{})

	// RWMutex protects the Resources map.
	sync.RWMutex

	// Resources is a map of stored resources.
	Resources map[string]interface{}

	// NewID is a mock function returning an new ID when Create is called with
	// an empty ID. Default is that Create returns an error.
	NewID func() string

	// OnExists overrides the Exists call. Default behavior is to return true if
	// Resources contains the id.
	OnExists func(st *Store, id string) bool

	// OnValue overrides the Value call. Default behavior is to return the value
	// in Resources, or store.ErrNotFound if not found.
	OnValue func(st *Store, id string) (interface{}, error)

	// OnCreate overrides the Create call. Default behavior is to set Resources
	// with the value if the id does not exist, otherwise return a
	// store.ErrDuplicate error.
	OnCreate func(st *Store, id string, v interface{}) error

	// OnUpdate overrides the Update call. It should return the previous value,
	// or an error. Default behavior is to replace the Resources value if it
	// exists, or return store.ErrNotFound if not found.
	OnUpdate func(st *Store, id string, v interface{}) (interface{}, error)

	// OnDelete overrides the OnDelete call. It should return the deleted value,
	// or an error. Default behavior is to delete the Resources value if it
	// exists, or return store.ErrNotFound if not found.
	OnDelete func(st *Store, id string) (interface{}, error)
}

Store is an in-memory CRUD mock store implementation.

It implements the store.Store interface.

A Store must not be copied after first call to Read or Write.

func NewStore

func NewStore() *Store

NewStore creates a new empty Store.

func (*Store) Add

func (st *Store) Add(id string, v interface{}) *Store

Add inserts a value into the Resources map.

func (*Store) OnChange

func (st *Store) OnChange(cb func(id string, before, after interface{}))

OnChange adds a listener callback that is called whenever a value is created, updated, or deleted from the store.

If a value is created, before will be set to nil.

If a value is deleted, after will be set to nil.

func (*Store) Read

func (st *Store) Read(id string) store.ReadTxn

Read makes a read-lock for the resource that lasts until Close is called.

func (*Store) Write

func (st *Store) Write(id string) store.WriteTxn

Write makes a write-lock for the resource that lasts until Close is called.

Jump to

Keyboard shortcuts

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