✏ update comments

This commit is contained in:
Fenny
2020-11-17 13:56:09 +01:00
parent deb10fb2a7
commit ddef41c2ee
28 changed files with 173 additions and 99 deletions

View File

@@ -13,7 +13,8 @@ A Postgres storage driver using [lib/pq](https://github.com/lib/pq).
```go
func New(config ...Config) Storage
var ErrNotExist = errors.New("key does not exist")
// ErrNotFound means that a get call did not find the requested key.
var ErrNotFound = errors.New("key not found")
func (s *Storage) Get(key string) ([]byte, error)
func (s *Storage) Set(key string, val []byte, exp time.Duration) error

View File

@@ -24,8 +24,9 @@ type Storage struct {
sqlGC string
}
// Common storage errors
var ErrNotExist = errors.New("key does not exist")
// ErrNotFound means that a get call did not find the requested key.
var ErrNotFound = errors.New("key not found")
var (
dropQuery = `DROP TABLE IF EXISTS %s;`
@@ -117,7 +118,7 @@ var noRows = errors.New("sql: no rows in result set")
// Get value by key
func (s *Storage) Get(key string) ([]byte, error) {
if len(key) <= 0 {
return nil, ErrNotExist
return nil, ErrNotFound
}
row := s.db.QueryRow(s.sqlSelect, key)
// Add db response to data
@@ -127,14 +128,14 @@ func (s *Storage) Get(key string) ([]byte, error) {
)
if err := row.Scan(&data, &exp); err != nil {
if err == sql.ErrNoRows {
return nil, ErrNotExist
return nil, ErrNotFound
}
return nil, err
}
// If the expiration time has already passed, then return nil
if exp != 0 && exp <= time.Now().Unix() {
return nil, ErrNotExist
return nil, ErrNotFound
}
return data, nil

View File

@@ -71,14 +71,14 @@ func Test_Postgres_Get_Expired(t *testing.T) {
)
result, err := testStore.Get(key)
utils.AssertEqual(t, ErrNotExist, err)
utils.AssertEqual(t, ErrNotFound, err)
utils.AssertEqual(t, true, len(result) == 0)
}
func Test_Postgres_Get_NotExist(t *testing.T) {
result, err := testStore.Get("notexist")
utils.AssertEqual(t, ErrNotExist, err)
utils.AssertEqual(t, ErrNotFound, err)
utils.AssertEqual(t, true, len(result) == 0)
}
@@ -95,7 +95,7 @@ func Test_Postgres_Delete(t *testing.T) {
utils.AssertEqual(t, nil, err)
result, err := testStore.Get(key)
utils.AssertEqual(t, ErrNotExist, err)
utils.AssertEqual(t, ErrNotFound, err)
utils.AssertEqual(t, true, len(result) == 0)
}
@@ -114,11 +114,11 @@ func Test_Postgres_Reset(t *testing.T) {
utils.AssertEqual(t, nil, err)
result, err := testStore.Get("john1")
utils.AssertEqual(t, ErrNotExist, err)
utils.AssertEqual(t, ErrNotFound, err)
utils.AssertEqual(t, true, len(result) == 0)
result, err = testStore.Get("john2")
utils.AssertEqual(t, ErrNotExist, err)
utils.AssertEqual(t, ErrNotFound, err)
utils.AssertEqual(t, true, len(result) == 0)
}