Create internal/thestore/event.Event and merge method

master
Bel LaPointe 2023-10-11 15:28:40 -06:00
parent 6f811b9267
commit 70841d7b14
2 changed files with 87 additions and 0 deletions

View 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
}

View 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)
}
})
}
}