initial
This commit is contained in:
26
internal/thestore/event.go
Normal file
26
internal/thestore/event.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package thestore
|
||||
|
||||
type Event struct {
|
||||
ID string `json:"service-id"`
|
||||
Region string `json:"region"`
|
||||
Status string `json:"status"`
|
||||
URL string `json:"url"`
|
||||
}
|
||||
|
||||
func (event Event) Push(operation Event) Event {
|
||||
if event.ID != operation.ID {
|
||||
panic(operation)
|
||||
}
|
||||
|
||||
if operation.Region != "" {
|
||||
event.Region = operation.Region
|
||||
}
|
||||
if operation.Status != "" {
|
||||
event.Status = operation.Status
|
||||
}
|
||||
if operation.URL != "" {
|
||||
event.URL = operation.URL
|
||||
}
|
||||
|
||||
return event
|
||||
}
|
||||
61
internal/thestore/event_test.go
Normal file
61
internal/thestore/event_test.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package thestore_test
|
||||
|
||||
import (
|
||||
"render231011/internal/thestore"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestEventMerging(t *testing.T) {
|
||||
cases := map[string]struct {
|
||||
base thestore.Event
|
||||
operation thestore.Event
|
||||
want thestore.Event
|
||||
}{
|
||||
"noop on noop": {},
|
||||
"add to nothing": {
|
||||
base: thestore.Event{
|
||||
ID: "x",
|
||||
},
|
||||
operation: thestore.Event{
|
||||
ID: "x",
|
||||
Region: "newregion",
|
||||
Status: "newstatus",
|
||||
URL: "newurl",
|
||||
},
|
||||
want: thestore.Event{
|
||||
ID: "x",
|
||||
Region: "newregion",
|
||||
Status: "newstatus",
|
||||
URL: "newurl",
|
||||
},
|
||||
},
|
||||
"change region": {
|
||||
base: thestore.Event{
|
||||
ID: "x",
|
||||
Region: "region",
|
||||
Status: "status",
|
||||
URL: "url",
|
||||
},
|
||||
operation: thestore.Event{
|
||||
ID: "x",
|
||||
Region: "newregion",
|
||||
},
|
||||
want: thestore.Event{
|
||||
ID: "x",
|
||||
Region: "newregion",
|
||||
Status: "status",
|
||||
URL: "url",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for name, d := range cases {
|
||||
c := d
|
||||
t.Run(name, func(t *testing.T) {
|
||||
got := c.base.Push(c.operation)
|
||||
if got != c.want {
|
||||
t.Errorf("wanted \n\t%+v, got\n\t%+v", c.want, got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
46
internal/thestore/store.go
Normal file
46
internal/thestore/store.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package thestore
|
||||
|
||||
import "sync"
|
||||
|
||||
type Store sync.Map
|
||||
|
||||
func NewStore() *Store {
|
||||
s := sync.Map{}
|
||||
store := Store(s)
|
||||
return &store
|
||||
}
|
||||
|
||||
// todo pass context.context and failfast
|
||||
func (store *Store) Dump() []Event {
|
||||
result := make([]Event, 0)
|
||||
(*sync.Map)(store).Range(func(_, v any) bool {
|
||||
result = append(result, v.(Event))
|
||||
return true
|
||||
})
|
||||
return result
|
||||
}
|
||||
|
||||
func (store *Store) Push(op Event) {
|
||||
k := op.ID
|
||||
event, ok := store.Get(k)
|
||||
if ok {
|
||||
event = event.Push(op)
|
||||
} else {
|
||||
event = op
|
||||
}
|
||||
store.Set(event)
|
||||
}
|
||||
|
||||
func (store *Store) Set(v Event) {
|
||||
k := v.ID
|
||||
(*sync.Map)(store).Store(k, v)
|
||||
}
|
||||
|
||||
func (store *Store) Get(k string) (Event, bool) {
|
||||
got, ok := (*sync.Map)(store).Load(k)
|
||||
if !ok {
|
||||
return Event{}, false
|
||||
}
|
||||
event := got.(Event)
|
||||
return event, true
|
||||
}
|
||||
Reference in New Issue
Block a user