feat: Update mockstorage package with Entry struct

This commit is contained in:
Jason McNeil
2024-06-04 11:29:55 -03:00
parent ce553f5f1c
commit 1ab83b0150
4 changed files with 77 additions and 33 deletions

View File

@@ -0,0 +1,19 @@
name: Release Drafter MockStorage
on:
push:
# branches to consider in the event; optional, defaults to all
branches:
- master
- main
paths:
- 'mockstorage/**'
jobs:
draft_release_memcache:
runs-on: ubuntu-latest
timeout-minutes: 30
steps:
- uses: release-drafter/release-drafter@v6
with:
config-name: release-drafter-memory.yml
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

View File

@@ -23,6 +23,31 @@ A mock storage implementation for Fiber. This storage is not persistent and is o
### Signatures
#### Structs
```go
type Storage struct {
// contains filtered or unexported fields
}
type Entry struct {
Value []byte
Exp time.Time
}
type CustomFuncs struct {
GetFunc func(key string) ([]byte, error)
SetFunc func(key string, val []byte, exp time.Duration) error
DeleteFunc func(key string) error
ResetFunc func() error
CloseFunc func() error
ConnFunc func() map[string]Entry
KeysFunc func() ([][]byte, error)
}
```
#### Functions
```go
// New creates a new Storage instance. You can optionally pass a Config.
func New(config ...Config) *Storage
@@ -43,7 +68,7 @@ func (s *Storage) Reset() error
func (s *Storage) Close() error
// Conn returns a copy of the current state of the storage.
func (s *Storage) Conn() map[string]entry
func (s *Storage) Conn() map[string]Entry
// Keys returns a list of all keys in the storage.
func (s *Storage) Keys() ([][]byte, error)

View File

@@ -14,14 +14,14 @@ type Config struct {
// Storage is the mock storage adapter.
type Storage struct {
mu sync.RWMutex
data map[string]entry
data map[string]Entry
custom *CustomFuncs
}
// entry struct to hold value and expiration time.
type entry struct {
value []byte
exp time.Time
// Entry struct to hold value and expiration time.
type Entry struct {
Value []byte
Exp time.Time
}
// CustomFuncs allows injecting custom behaviors for testing.
@@ -31,14 +31,14 @@ type CustomFuncs struct {
DeleteFunc func(key string) error
ResetFunc func() error
CloseFunc func() error
ConnFunc func() map[string]entry
ConnFunc func() map[string]Entry
KeysFunc func() ([][]byte, error)
}
// New creates a new mock storage with optional configuration.
func New(config ...Config) *Storage {
s := &Storage{
data: make(map[string]entry),
data: make(map[string]Entry),
custom: &CustomFuncs{
GetFunc: nil,
SetFunc: nil,
@@ -71,11 +71,11 @@ func (s *Storage) Get(key string) ([]byte, error) {
if !ok {
return nil, errors.New("key not found")
}
if !e.exp.IsZero() && time.Now().After(e.exp) {
if !e.Exp.IsZero() && time.Now().After(e.Exp) {
delete(s.data, key)
return nil, errors.New("key expired")
}
return e.value, nil
return e.Value, nil
}
// Set sets the value for a given key with an expiration time.
@@ -92,7 +92,7 @@ func (s *Storage) Set(key string, val []byte, exp time.Duration) error {
expTime = time.Now().Add(exp)
}
s.data[key] = entry{value: val, exp: expTime}
s.data[key] = Entry{Value: val, Exp: expTime}
return nil
}
@@ -118,7 +118,7 @@ func (s *Storage) Reset() error {
s.mu.Lock()
defer s.mu.Unlock()
s.data = make(map[string]entry)
s.data = make(map[string]Entry)
return nil
}
@@ -133,7 +133,7 @@ func (s *Storage) Close() error {
}
// Conn returns the internal data map (for testing purposes).
func (s *Storage) Conn() map[string]entry {
func (s *Storage) Conn() map[string]Entry {
if s.custom.ConnFunc != nil {
return s.custom.ConnFunc()
}
@@ -141,9 +141,9 @@ func (s *Storage) Conn() map[string]entry {
s.mu.RLock()
defer s.mu.RUnlock()
copyData := make(map[string]entry)
copyData := make(map[string]Entry)
for k, v := range s.data {
copyData[k] = v
copyData[k] = Entry{Value: v.Value, Exp: v.Exp}
}
return copyData
}

View File

@@ -68,10 +68,10 @@ func TestStorageConnFunc(t *testing.T) {
store := New()
customFuncs := &CustomFuncs{
ConnFunc: func() map[string]entry {
return map[string]entry{
"customKey1": {value: []byte("customValue1"), exp: time.Time{}},
"customKey2": {value: []byte("customValue2"), exp: time.Now().Add(1 * time.Hour)},
ConnFunc: func() map[string]Entry {
return map[string]Entry{
"customKey1": {Value: []byte("customValue1"), Exp: time.Time{}},
"customKey2": {Value: []byte("customValue2"), Exp: time.Now().Add(1 * time.Hour)},
}
},
}
@@ -80,13 +80,13 @@ func TestStorageConnFunc(t *testing.T) {
// Test custom Conn
conn := store.Conn()
expectedConn := map[string]entry{
"customKey1": {value: []byte("customValue1"), exp: time.Time{}},
"customKey2": {value: []byte("customValue2"), exp: time.Now().Add(1 * time.Hour)},
expectedConn := map[string]Entry{
"customKey1": {Value: []byte("customValue1"), Exp: time.Time{}},
"customKey2": {Value: []byte("customValue2"), Exp: time.Now().Add(1 * time.Hour)},
}
for k, v := range expectedConn {
if val, ok := conn[k]; !ok || !bytes.Equal(val.value, v.value) {
if val, ok := conn[k]; !ok || !bytes.Equal(val.Value, v.Value) {
t.Errorf("Conn() = %v, want %v", conn, expectedConn)
}
}
@@ -174,10 +174,10 @@ func TestStorageCustomBehavior(t *testing.T) {
}
return nil
},
ConnFunc: func() map[string]entry {
return map[string]entry{
"customKey1": {value: []byte("customValue1"), exp: time.Time{}},
"customKey2": {value: []byte("customValue2"), exp: time.Now().Add(1 * time.Hour)},
ConnFunc: func() map[string]Entry {
return map[string]Entry{
"customKey1": {Value: []byte("customValue1"), Exp: time.Time{}},
"customKey2": {Value: []byte("customValue2"), Exp: time.Now().Add(1 * time.Hour)},
}
},
KeysFunc: func() ([][]byte, error) {
@@ -225,13 +225,13 @@ func TestStorageCustomBehavior(t *testing.T) {
// Test custom Conn
conn := store.Conn()
expectedConn := map[string]entry{
"customKey1": {value: []byte("customValue1"), exp: time.Time{}},
"customKey2": {value: []byte("customValue2"), exp: time.Now().Add(1 * time.Hour)},
expectedConn := map[string]Entry{
"customKey1": {Value: []byte("customValue1"), Exp: time.Time{}},
"customKey2": {Value: []byte("customValue2"), Exp: time.Now().Add(1 * time.Hour)},
}
for k, v := range expectedConn {
if val, ok := conn[k]; !ok || !bytes.Equal(val.value, v.value) {
if val, ok := conn[k]; !ok || !bytes.Equal(val.Value, v.Value) {
t.Errorf("Conn() = %v, want %v", conn, expectedConn)
}
}
@@ -261,8 +261,8 @@ func TestStorageConnAndKeys(t *testing.T) {
t.Fatalf("Set() error = %v, wantErr %v", err, nil)
}
conn := store.Conn()
if val, ok := conn["key1"]; !ok || !bytes.Equal(val.value, []byte("value1")) {
t.Errorf("Conn() = %v, want %v", conn, map[string]entry{"key1": {value: []byte("value1"), exp: time.Time{}}})
if val, ok := conn["key1"]; !ok || !bytes.Equal(val.Value, []byte("value1")) {
t.Errorf("Conn() = %v, want %v", conn, map[string]Entry{"key1": {Value: []byte("value1"), Exp: time.Time{}}})
}
// Test Keys