-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(allsrv): refactor DB interface out of in-mem database type
This paves the way for more interesting additions. With this DB interface in place, can you add metrics for the datastore without futzing with the in-mem database implmementation? Add/update tests to verify the db still exhibits the same behavior.
- Loading branch information
Showing
2 changed files
with
68 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package allsrv | ||
|
||
import ( | ||
"errors" | ||
) | ||
|
||
// InmemDB is an in-memory store. | ||
type InmemDB struct { | ||
m []Foo // 12) | ||
} | ||
|
||
func (db *InmemDB) CreateFoo(f Foo) error { | ||
for _, existing := range db.m { | ||
if f.Name == existing.Name { | ||
return errors.New("foo " + f.Name + " exists") // 8) | ||
} | ||
} | ||
|
||
db.m = append(db.m, f) | ||
|
||
return nil | ||
} | ||
|
||
func (db *InmemDB) ReadFoo(id string) (Foo, error) { | ||
for _, f := range db.m { | ||
if id == f.ID { | ||
return f, nil | ||
} | ||
} | ||
return Foo{}, errors.New("foo not found for id: " + id) // 8) | ||
} | ||
|
||
func (db *InmemDB) UpdateFoo(f Foo) error { | ||
for i, existing := range db.m { | ||
if f.ID == existing.ID { | ||
db.m[i] = f | ||
return nil | ||
} | ||
} | ||
return errors.New("foo not found for id: " + f.ID) // 8) | ||
} | ||
|
||
func (db *InmemDB) DelFoo(id string) error { | ||
for i, f := range db.m { | ||
if id == f.ID { | ||
db.m = append(db.m[:i], db.m[i+1:]...) | ||
return nil // 13) | ||
} | ||
} | ||
return errors.New("foo not found for id: " + id) // 8) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters