Skip to content

Commit

Permalink
Add SyncStore which implements the Storer interface
Browse files Browse the repository at this point in the history
SyncStore implements the Storer interface and supplies a NewSyncStore
factory function.

- Uuid increment is handled internally by SyncStore Create
- Read will return an empty Puppy struct if provided uuid doesn't exist
- Update returns a bool whether a matching identifier was modified
- Destroy returns a bool whether a matching identifier was modified

Lab 06 (#537)
  • Loading branch information
patrickmarabeas committed Aug 19, 2019
1 parent 645548f commit a35ef1f
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions 06_puppy/patrickmarabeas/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,5 @@ func (suite *StoreSuite) TestIdIncrementOnDelete() {

func TestStore(t *testing.T) {
suite.Run(t, &StoreSuite{store: NewMapStore()})
suite.Run(t, &StoreSuite{store: NewSyncStore()})
}
50 changes: 50 additions & 0 deletions 06_puppy/patrickmarabeas/syncStore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package main

// NewSyncStore returns a pointer to a new instance of the SyncStore struct which implements the Storer interface.
func NewSyncStore() Storer {
return &SyncStore{
uuid: 0,
}
}

// Create increments the uuid and adds the provided Puppy struct to the store with this identifier.
func (store *SyncStore) Create(puppy Puppy) int {
puppy.ID = store.uuid
store.Store(puppy.ID, puppy)
store.uuid++

return puppy.ID
}

// Read returns the puppy matching the provided uuid.
// An empty Puppy struct is returned if the identifier does not exist.
func (store *SyncStore) Read(id int) Puppy {
if value, ok := store.Load(id); ok {
return value.(Puppy)
}

return Puppy{}
}

// Update modifies the puppy matching the provided uuid in the store with the provided Puppy struct.
// It returns a bool whether a matching identifier was modified or not.
func (store *SyncStore) Update(id int, puppy Puppy) bool {
if _, ok := store.Load(id); !ok {
return false
}

puppy.ID = id
store.Store(id, puppy)
return true
}

// Destroy removes the puppy matching the provided uuid from the store.
// It returns a bool whether a matching identifier was deleted or not.
func (store *SyncStore) Destroy(id int) bool {
if _, ok := store.Load(id); !ok {
return false
}

store.Delete(id)
return true
}
9 changes: 9 additions & 0 deletions 06_puppy/patrickmarabeas/types.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package main

import (
"sync"
)

type Puppy struct {
ID int
Breed string
Expand All @@ -18,3 +22,8 @@ type MapStore struct {
uuid int
store map[int]Puppy
}

type SyncStore struct {
uuid int
sync.Map
}

0 comments on commit a35ef1f

Please sign in to comment.