Initial commit
This commit is contained in:
46
store/memory.go
Normal file
46
store/memory.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type MemoryStore struct {
|
||||
data map[string][]byte
|
||||
}
|
||||
|
||||
func NewMemoryStore() *MemoryStore {
|
||||
return &MemoryStore{data: map[string][]byte{}}
|
||||
}
|
||||
|
||||
func (s *MemoryStore) Add(r io.Reader) (string, error) {
|
||||
id := uuid.Must(uuid.NewRandom()).String()
|
||||
|
||||
data, err := ioutil.ReadAll(r)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
s.data[id] = data
|
||||
return id, nil
|
||||
}
|
||||
|
||||
func (s *MemoryStore) Delete(id string) error {
|
||||
delete(s.data, id)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *MemoryStore) Get(id string) (io.ReadCloser, error) {
|
||||
data, ok := s.data[id]
|
||||
if !ok {
|
||||
return nil, ErrNoSuchItem
|
||||
}
|
||||
br := bytes.NewReader(data)
|
||||
|
||||
r := io.NopCloser(br)
|
||||
|
||||
return r, nil
|
||||
}
|
12
store/memory_test.go
Normal file
12
store/memory_test.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package store_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"git.t-juice.club/torjus/minipaste/store"
|
||||
)
|
||||
|
||||
func TestMemoryStore(t *testing.T) {
|
||||
s := store.NewMemoryStore()
|
||||
testStore(s, t)
|
||||
}
|
14
store/store.go
Normal file
14
store/store.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
)
|
||||
|
||||
var ErrNoSuchItem = errors.New("no such item")
|
||||
|
||||
type Store interface {
|
||||
Add(r io.Reader) (string, error)
|
||||
Delete(id string) error
|
||||
Get(id string) (io.ReadCloser, error)
|
||||
}
|
45
store/store_test.go
Normal file
45
store/store_test.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package store_test
|
||||
|
||||
import (
|
||||
"io"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.t-juice.club/torjus/minipaste/store"
|
||||
)
|
||||
|
||||
func testStore(s store.Store, t *testing.T) {
|
||||
t.Run("TestSimple", func(t *testing.T) {
|
||||
expected := "this is some text"
|
||||
sr := strings.NewReader(expected)
|
||||
|
||||
// Add
|
||||
id, err := s.Add(sr)
|
||||
if err != nil {
|
||||
t.Fatalf("Error when adding: %s", err)
|
||||
}
|
||||
if id == "" {
|
||||
t.Fatalf("Blank ID returned")
|
||||
}
|
||||
|
||||
// Get
|
||||
data, err := s.Get(id)
|
||||
if err != nil {
|
||||
t.Fatalf("Error getting data: %s", err)
|
||||
}
|
||||
|
||||
var sb strings.Builder
|
||||
|
||||
if _, err := io.Copy(&sb, data); err != nil {
|
||||
t.Fatalf("Error reading returned data: %s", err)
|
||||
}
|
||||
|
||||
if sb.String() != expected {
|
||||
t.Fatalf("Returned data does not match expected")
|
||||
}
|
||||
|
||||
if err := s.Delete(id); err != nil {
|
||||
t.Fatalf("Error deleting: %s", err)
|
||||
}
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user