Skip to content

Commit

Permalink
generic sigs
Browse files Browse the repository at this point in the history
  • Loading branch information
mattkasun committed Dec 23, 2023
1 parent df08030 commit 79fced9
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 22 deletions.
10 changes: 6 additions & 4 deletions database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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))
Expand All @@ -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 {
Expand Down
37 changes: 22 additions & 15 deletions database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit 79fced9

Please sign in to comment.