Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package fake
- import "sort"
- type Doc struct {
- ID uint
- Foo string
- Bar string
- }
- type store map[uint]Doc
- type Fake struct {
- stores store
- }
- func NewFake() *Fake {
- stores := make(store, 5)
- return &Fake{stores}
- }
- func (f *Fake) Seed(docs []Doc) {
- for _, d := range docs {
- f.stores[d.ID] = d
- }
- }
- func (f *Fake) Create(d Doc) error {
- f.stores[d.ID] = d
- return nil
- }
- func (f *Fake) Get(id uint) Doc {
- return f.stores[id]
- }
- func (f *Fake) Update(d Doc) error {
- if _, found := f.stores[d.ID]; found {
- f.stores[d.ID] = d
- }
- return nil
- }
- func (f *Fake) Delete(id uint) error {
- delete(f.stores, id)
- return nil
- }
- func (f *Fake) Upsert(d Doc) error {
- if _, found := f.stores[d.ID]; found {
- f.stores[d.ID] = d
- return nil
- }
- return f.Create(d)
- }
- func (f *Fake) CountAll() int {
- return len(f.stores)
- }
- func (f *Fake) RemoveAll() error {
- f.stores = make(store, 5)
- return nil
- }
- func (f *Fake) All() []Doc {
- var docs []Doc
- for _, d := range f.stores {
- docs = append(docs, d)
- }
- sort.Slice(docs, func(i, j int) bool {
- return docs[i].ID < docs[j].ID
- })
- return docs
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement