Create internal/thestore/event.Event and merge method
parent
6f811b9267
commit
70841d7b14
|
|
@ -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
|
||||||
|
}
|
||||||
|
|
@ -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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue