Merge pull request #4 from nanobox-io/fix/read-write-match
matched the format of the write function
This commit is contained in:
commit
bbc07fd24c
10
README.md
10
README.md
|
@ -21,29 +21,29 @@ if err != nil {
|
||||||
|
|
||||||
// Write a fish to the database
|
// Write a fish to the database
|
||||||
fish := Fish{}
|
fish := Fish{}
|
||||||
if err := db.Write("/fish", "onefish", fish); err != nil {
|
if err := db.Write("fish", "onefish", fish); err != nil {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read all fish from the database
|
// Read all fish from the database
|
||||||
fish := []Fish{}
|
fish := []Fish{}
|
||||||
if err := db.Read("/fish", fish); err != nil {
|
if err := db.Read("fish", fish); err != nil {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read a fish from the database
|
// Read a fish from the database
|
||||||
fish := Fish{}
|
fish := Fish{}
|
||||||
if err := db.Read("/fish/onefish", fish); err != nil {
|
if err := db.Read("fish", "onefish", fish); err != nil {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete all fish from the database
|
// Delete all fish from the database
|
||||||
if err := db.Delete("/fish"); err != nil {
|
if err := db.Delete("fish"); err != nil {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete a fish from the database
|
// Delete a fish from the database
|
||||||
if err := db.Delete("/fish/onefish"); err != nil {
|
if err := db.Delete("fish", "onefish"); err != nil {
|
||||||
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
|
@ -65,9 +65,10 @@ func New(dir string, logger hatchet.Logger) (*Driver, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read a record from the database
|
// Read a record from the database
|
||||||
func (d *Driver) Read(path string, v interface{}) error {
|
func (d *Driver) Read(collection, resource string, v interface{}) error {
|
||||||
|
|
||||||
//
|
//
|
||||||
|
path := filepath.Join(collection, resource)
|
||||||
dir := filepath.Join(d.dir, path)
|
dir := filepath.Join(d.dir, path)
|
||||||
|
|
||||||
//
|
//
|
||||||
|
@ -154,8 +155,8 @@ func (d *Driver) Write(collection, resource string, v interface{}) error {
|
||||||
|
|
||||||
// Delete locks that database and then attempts to remove the collection/resource
|
// Delete locks that database and then attempts to remove the collection/resource
|
||||||
// specified by [path]
|
// specified by [path]
|
||||||
func (d *Driver) Delete(path string) error {
|
func (d *Driver) Delete(collection, resource string) error {
|
||||||
|
path := filepath.Join(collection, resource)
|
||||||
//
|
//
|
||||||
mutex := d.getOrCreateMutex(path)
|
mutex := d.getOrCreateMutex(path)
|
||||||
mutex.Lock()
|
mutex.Lock()
|
||||||
|
|
|
@ -72,7 +72,7 @@ func TestWrite(t *testing.T) {
|
||||||
func TestRead(t *testing.T) {
|
func TestRead(t *testing.T) {
|
||||||
createFriend(t)
|
createFriend(t)
|
||||||
|
|
||||||
if err := db.Read(friendsPath+"/friend1", &friend0); err != nil {
|
if err := db.Read(friendsPath, "friend1", &friend0); err != nil {
|
||||||
t.Error("Failed to read", err)
|
t.Error("Failed to read", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -86,7 +86,7 @@ func TestRead(t *testing.T) {
|
||||||
//
|
//
|
||||||
func TestReadEmpty(t *testing.T) {
|
func TestReadEmpty(t *testing.T) {
|
||||||
|
|
||||||
if err := db.Read(friendsPath+"/friend1", &friend0); err == nil {
|
if err := db.Read(friendsPath, "friend1", &friend0); err == nil {
|
||||||
t.Error("Expected nothing, found friend")
|
t.Error("Expected nothing, found friend")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -98,7 +98,7 @@ func TestReadall(t *testing.T) {
|
||||||
createFriends(t)
|
createFriends(t)
|
||||||
|
|
||||||
friends := []Friend{}
|
friends := []Friend{}
|
||||||
if err := db.Read(friendsPath, &friends); err != nil {
|
if err := db.Read(friendsPath, "", &friends); err != nil {
|
||||||
t.Error("Failed to read", err)
|
t.Error("Failed to read", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -113,7 +113,7 @@ func TestReadall(t *testing.T) {
|
||||||
func TestReadallEmpty(t *testing.T) {
|
func TestReadallEmpty(t *testing.T) {
|
||||||
|
|
||||||
friends := []Friend{}
|
friends := []Friend{}
|
||||||
if err := db.Read(friendsPath, &friends); err == nil {
|
if err := db.Read(friendsPath, "", &friends); err == nil {
|
||||||
t.Error("Expected nothing, found friends")
|
t.Error("Expected nothing, found friends")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -124,7 +124,7 @@ func TestReadallEmpty(t *testing.T) {
|
||||||
func TestDelete(t *testing.T) {
|
func TestDelete(t *testing.T) {
|
||||||
createFriend(t)
|
createFriend(t)
|
||||||
|
|
||||||
if err := db.Delete(friendsPath + "/friend1"); err != nil {
|
if err := db.Delete(friendsPath, "friend1"); err != nil {
|
||||||
t.Error("Failed to delete", err)
|
t.Error("Failed to delete", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -139,7 +139,7 @@ func TestDelete(t *testing.T) {
|
||||||
func TestDeleteall(t *testing.T) {
|
func TestDeleteall(t *testing.T) {
|
||||||
createFriends(t)
|
createFriends(t)
|
||||||
|
|
||||||
if err := db.Delete(friendsPath); err != nil {
|
if err := db.Delete(friendsPath, ""); err != nil {
|
||||||
t.Error("Failed to delete ", err)
|
t.Error("Failed to delete ", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue