From a35ef1fb26c0008588e011484b3367950d4669e3 Mon Sep 17 00:00:00 2001 From: Patrick Marabeas Date: Mon, 19 Aug 2019 14:35:22 +1000 Subject: [PATCH] Add SyncStore which implements the Storer interface 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) --- 06_puppy/patrickmarabeas/store_test.go | 1 + 06_puppy/patrickmarabeas/syncStore.go | 50 ++++++++++++++++++++++++++ 06_puppy/patrickmarabeas/types.go | 9 +++++ 3 files changed, 60 insertions(+) create mode 100644 06_puppy/patrickmarabeas/syncStore.go diff --git a/06_puppy/patrickmarabeas/store_test.go b/06_puppy/patrickmarabeas/store_test.go index 241f429a6..6990ef871 100644 --- a/06_puppy/patrickmarabeas/store_test.go +++ b/06_puppy/patrickmarabeas/store_test.go @@ -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()}) } diff --git a/06_puppy/patrickmarabeas/syncStore.go b/06_puppy/patrickmarabeas/syncStore.go new file mode 100644 index 000000000..47dc770f5 --- /dev/null +++ b/06_puppy/patrickmarabeas/syncStore.go @@ -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 +} diff --git a/06_puppy/patrickmarabeas/types.go b/06_puppy/patrickmarabeas/types.go index f2050d1c1..bbf6907be 100644 --- a/06_puppy/patrickmarabeas/types.go +++ b/06_puppy/patrickmarabeas/types.go @@ -1,5 +1,9 @@ package main +import ( + "sync" +) + type Puppy struct { ID int Breed string @@ -18,3 +22,8 @@ type MapStore struct { uuid int store map[int]Puppy } + +type SyncStore struct { + uuid int + sync.Map +}