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) 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.
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 (*Store) OnChange ¶
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.