cal mem repo
This commit is contained in:
parent
9f98a8a9da
commit
540482bc52
|
@ -0,0 +1,65 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"sort"
|
||||
"sync"
|
||||
|
||||
"go-mod.ewintr.nl/planner/item"
|
||||
)
|
||||
|
||||
type Memory struct {
|
||||
events map[string]item.Event
|
||||
mutex sync.RWMutex
|
||||
}
|
||||
|
||||
func NewMemory() *Memory {
|
||||
return &Memory{
|
||||
events: make(map[string]item.Event),
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Memory) Find(id string) (item.Event, error) {
|
||||
r.mutex.RLock()
|
||||
defer r.mutex.RUnlock()
|
||||
|
||||
event, exists := r.events[id]
|
||||
if !exists {
|
||||
return item.Event{}, errors.New("event not found")
|
||||
}
|
||||
return event, nil
|
||||
}
|
||||
|
||||
func (r *Memory) FindAll() ([]item.Event, error) {
|
||||
r.mutex.RLock()
|
||||
defer r.mutex.RUnlock()
|
||||
|
||||
events := make([]item.Event, 0, len(r.events))
|
||||
for _, event := range r.events {
|
||||
events = append(events, event)
|
||||
}
|
||||
sort.Slice(events, func(i, j int) bool {
|
||||
return events[i].ID < events[j].ID
|
||||
})
|
||||
|
||||
return events, nil
|
||||
}
|
||||
|
||||
func (r *Memory) Store(e item.Event) error {
|
||||
r.mutex.Lock()
|
||||
defer r.mutex.Unlock()
|
||||
|
||||
r.events[e.ID] = e
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Memory) Delete(id string) error {
|
||||
r.mutex.Lock()
|
||||
defer r.mutex.Unlock()
|
||||
|
||||
if _, exists := r.events[id]; !exists {
|
||||
return errors.New("event not found")
|
||||
}
|
||||
delete(r.events, id)
|
||||
return nil
|
||||
}
|
|
@ -0,0 +1,56 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"go-mod.ewintr.nl/planner/item"
|
||||
)
|
||||
|
||||
func TestMemory(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
mem := NewMemory()
|
||||
|
||||
t.Log("empty")
|
||||
actEvents, actErr := mem.FindAll()
|
||||
if actErr != nil {
|
||||
t.Errorf("exp nil, got %v", actErr)
|
||||
}
|
||||
if len(actEvents) != 0 {
|
||||
t.Errorf("exp 0, got %d", len(actEvents))
|
||||
}
|
||||
|
||||
t.Log("store")
|
||||
e1 := item.Event{
|
||||
ID: "id-1",
|
||||
}
|
||||
if err := mem.Store(e1); err != nil {
|
||||
t.Errorf("exp nil, got %v", err)
|
||||
}
|
||||
|
||||
e2 := item.Event{
|
||||
ID: "id-2",
|
||||
}
|
||||
if err := mem.Store(e2); err != nil {
|
||||
t.Errorf("exp nil, got %v", err)
|
||||
}
|
||||
|
||||
t.Log("find one")
|
||||
actEvent, actErr := mem.Find(e1.ID)
|
||||
if actErr != nil {
|
||||
t.Errorf("exp nil, got %v", actErr)
|
||||
}
|
||||
if actEvent.ID != e1.ID {
|
||||
t.Errorf("exp %v, got %v", e1.ID, actEvent.ID)
|
||||
}
|
||||
|
||||
t.Log("find all")
|
||||
actEvents, actErr = mem.FindAll()
|
||||
if actErr != nil {
|
||||
t.Errorf("exp nil, got %v", actErr)
|
||||
}
|
||||
if diff := cmp.Diff([]item.Event{e1, e2}, actEvents); diff != "" {
|
||||
t.Errorf("(exp -, got +)\n%s", diff)
|
||||
}
|
||||
}
|
|
@ -2,6 +2,7 @@ package main
|
|||
|
||||
import (
|
||||
"slices"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"go-mod.ewintr.nl/planner/item"
|
||||
|
@ -9,6 +10,7 @@ import (
|
|||
|
||||
type Memory struct {
|
||||
items map[string]item.Item
|
||||
mutex sync.RWMutex
|
||||
}
|
||||
|
||||
func NewMemory() *Memory {
|
||||
|
@ -18,12 +20,18 @@ func NewMemory() *Memory {
|
|||
}
|
||||
|
||||
func (m *Memory) Update(item item.Item) error {
|
||||
m.mutex.Lock()
|
||||
defer m.mutex.Unlock()
|
||||
|
||||
m.items[item.ID] = item
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Memory) Updated(kinds []item.Kind, timestamp time.Time) ([]item.Item, error) {
|
||||
m.mutex.RLock()
|
||||
defer m.mutex.RUnlock()
|
||||
|
||||
result := make([]item.Item, 0)
|
||||
|
||||
for _, i := range m.items {
|
||||
|
|
Loading…
Reference in New Issue