diff --git a/database.go b/database.go index b40ff07..c850530 100644 --- a/database.go +++ b/database.go @@ -74,7 +74,8 @@ func Save(value any, key, table string) error { } // Get retrieves a value for key in specified table -func Get[T any](value T, key, table string) (T, error) { +func Get[T any](key, table string) (T, error) { + var value T err := db.View(func(tx *bbolt.Tx) error { b := tx.Bucket([]byte(table)) if b == nil { @@ -93,7 +94,8 @@ func Get[T any](value T, key, table string) (T, error) { } // GetAll retrieves all values from table -func GetAll[T any](value T, table string) ([]T, error) { +func GetAll[T any](table string) ([]T, error) { + var value T var values []T err := db.View(func(tx *bbolt.Tx) error { b := tx.Bucket([]byte(table)) @@ -113,8 +115,8 @@ func GetAll[T any](value T, table string) ([]T, error) { } // Delete deletes the entry in table corresponding to key -func Delete[T any](value T, key, table string) error { - if _, err := Get(value, key, table); err != nil { +func Delete[T any](key, table string) error { + if _, err := Get[T](key, table); err != nil { return err } err := db.Update(func(tx *bbolt.Tx) error { diff --git a/database_test.go b/database_test.go index 65b6799..4da7302 100644 --- a/database_test.go +++ b/database_test.go @@ -13,6 +13,11 @@ type User struct { Pass string } +type Other struct { + Name string + Foo int +} + func TestInitialize(t *testing.T) { t.Log(tables) //tables := []string{"users", "keys", "networks"} @@ -70,25 +75,29 @@ func TestGetOne(t *testing.T) { err := Initialize("./test.db", tables) assert.Nil(t, err) deleteTestEntries(t) - user := User{} t.Run("noSuchTable", func(t *testing.T) { - value, err := Get(user, "first", "nosuchtable") + value, err := Get[User]("first", "nosuchtable") assert.Equal(t, User{}, value) assert.Equal(t, ErrInvalidTableName, err) }) t.Run("noValues", func(t *testing.T) { - value, err := Get(user, "first", "users") + value, err := Get[User]("first", "users") assert.Equal(t, User{}, value) assert.Equal(t, ErrNoResults, err) }) createTestEntries(t) t.Run("wrongkey", func(t *testing.T) { - value, err := Get(user, "third", "users") + value, err := Get[User]("third", "users") assert.Equal(t, ErrNoResults, err) assert.Equal(t, User{}, value) }) + t.Run("wrongType", func(t *testing.T) { + value, err := Get[Other]("first", "users") + assert.Nil(t, err) + assert.Equal(t, Other{}, value) + }) t.Run("valid", func(t *testing.T) { - value, err := Get(user, "first", "users") + value, err := Get[User]("first", "users") assert.Nil(t, err) assert.Equal(t, "first", value.User) assert.Equal(t, "password", value.Pass) @@ -102,20 +111,19 @@ func TestGetAll(t *testing.T) { err := Initialize("./test.db", tables) assert.Nil(t, err) deleteTestEntries(t) - user := User{} t.Run("noSuchTable", func(t *testing.T) { - value, err := GetAll(user, "nosuchtable") + value, err := GetAll[User]("nosuchtable") assert.Equal(t, []User(nil), value) assert.Equal(t, ErrInvalidTableName, err) }) t.Run("noValues", func(t *testing.T) { - value, err := GetAll(user, "users") + value, err := GetAll[User]("users") assert.Equal(t, []User(nil), value) assert.Nil(t, err) }) createTestEntries(t) t.Run("valid", func(t *testing.T) { - value, err := GetAll(user, "users") + value, err := GetAll[User]("users") assert.Nil(t, err) assert.Equal(t, "first", value[0].User) assert.Equal(t, "password", value[0].Pass) @@ -129,16 +137,16 @@ func TestDelete(t *testing.T) { err := Initialize("./test.db", tables) assert.Nil(t, err) t.Run("nonexistentTable", func(t *testing.T) { - err := Delete(User{}, "first", "tabledoesnotexist") + err := Delete[User]("first", "tabledoesnotexist") assert.Equal(t, ErrInvalidTableName, err) }) t.Run("nosuchrecord", func(t *testing.T) { - err := Delete(User{}, "first", "users") + err := Delete[User]("first", "users") assert.Equal(t, ErrNoResults, err) }) t.Run("valid", func(t *testing.T) { createTestEntries(t) - err := Delete(User{}, "first", "users") + err := Delete[User]("first", "users") assert.Nil(t, err) }) deleteTestEntries(t) @@ -166,11 +174,10 @@ func createTestEntries(t *testing.T) { func deleteTestEntries(t *testing.T) { t.Helper() - user := User{} - values, err := GetAll(user, "users") + values, err := GetAll[User]("users") assert.Nil(t, err) for _, value := range values { - err := Delete(user, value.User, "users") + err := Delete[User](value.User, "users") assert.Nil(t, err) } } diff --git a/go.mod b/go.mod index 9f05259..48e0b0c 100644 --- a/go.mod +++ b/go.mod @@ -10,6 +10,6 @@ require ( require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - golang.org/x/sys v0.4.0 // indirect + golang.org/x/sys v0.13.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 3d0c28e..40ad737 100644 --- a/go.sum +++ b/go.sum @@ -6,8 +6,8 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= go.etcd.io/bbolt v1.3.8 h1:xs88BrvEv273UsB79e0hcVrlUWmS0a8upikMFhSyAtA= go.etcd.io/bbolt v1.3.8/go.mod h1:N9Mkw9X8x5fupy0IKsmuqVtoGDyxsaDlbk4Rd05IAQw= -golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18= -golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= +golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=