event item
This commit is contained in:
parent
bb80b91a3c
commit
85e91728d9
1
go.mod
1
go.mod
|
@ -4,6 +4,7 @@ go 1.21.5
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||||
|
github.com/google/go-cmp v0.6.0 // indirect
|
||||||
github.com/google/uuid v1.6.0 // indirect
|
github.com/google/uuid v1.6.0 // indirect
|
||||||
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
|
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
|
||||||
github.com/lib/pq v1.10.9 // indirect
|
github.com/lib/pq v1.10.9 // indirect
|
||||||
|
|
2
go.sum
2
go.sum
|
@ -1,5 +1,7 @@
|
||||||
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
|
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
|
||||||
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
|
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
|
||||||
|
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||||
|
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
|
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
|
||||||
|
|
|
@ -0,0 +1,50 @@
|
||||||
|
package item
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type EventBody struct {
|
||||||
|
Title string `json:"title"`
|
||||||
|
Start time.Time `json:"start"`
|
||||||
|
End time.Time `json:"end"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Event struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
EventBody
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewEvent(i Item) (Event, error) {
|
||||||
|
if i.Kind != KindEvent {
|
||||||
|
return Event{}, fmt.Errorf("item is not an event")
|
||||||
|
}
|
||||||
|
|
||||||
|
var e Event
|
||||||
|
if err := json.Unmarshal([]byte(i.Body), &e); err != nil {
|
||||||
|
return Event{}, fmt.Errorf("could not unmarshal item body: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
e.ID = i.ID
|
||||||
|
|
||||||
|
return e, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e Event) Item() (Item, error) {
|
||||||
|
body, err := json.Marshal(EventBody{
|
||||||
|
Title: e.Title,
|
||||||
|
Start: e.Start,
|
||||||
|
End: e.End,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return Item{}, fmt.Errorf("could not marshal event to json")
|
||||||
|
}
|
||||||
|
|
||||||
|
return Item{
|
||||||
|
ID: e.ID,
|
||||||
|
Kind: KindEvent,
|
||||||
|
Body: string(body),
|
||||||
|
}, nil
|
||||||
|
}
|
|
@ -0,0 +1,78 @@
|
||||||
|
package item_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/google/go-cmp/cmp"
|
||||||
|
"go-mod.ewintr.nl/planner/item"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNewEvent(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
for _, tc := range []struct {
|
||||||
|
name string
|
||||||
|
it item.Item
|
||||||
|
expEvent item.Event
|
||||||
|
expErr bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "wrong kind",
|
||||||
|
it: item.Item{
|
||||||
|
ID: "a",
|
||||||
|
Kind: item.KindTask,
|
||||||
|
Body: `{
|
||||||
|
"title":"title",
|
||||||
|
"start":"2024-09-20T08:00:00Z",
|
||||||
|
"end":"2024-09-20T10:00:00Z"
|
||||||
|
}`,
|
||||||
|
},
|
||||||
|
expErr: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "invalid json",
|
||||||
|
it: item.Item{
|
||||||
|
ID: "a",
|
||||||
|
Kind: item.KindEvent,
|
||||||
|
Body: `{"id":"a"`,
|
||||||
|
},
|
||||||
|
expErr: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "valid",
|
||||||
|
it: item.Item{
|
||||||
|
ID: "a",
|
||||||
|
Kind: item.KindEvent,
|
||||||
|
Body: `{
|
||||||
|
"title":"title",
|
||||||
|
"start":"2024-09-20T08:00:00Z",
|
||||||
|
"end":"2024-09-20T10:00:00Z"
|
||||||
|
}`,
|
||||||
|
},
|
||||||
|
expEvent: item.Event{
|
||||||
|
ID: "a",
|
||||||
|
EventBody: item.EventBody{
|
||||||
|
Title: "title",
|
||||||
|
Start: time.Date(2024, 9, 20, 8, 0, 0, 0, time.UTC),
|
||||||
|
End: time.Date(2024, 9, 20, 10, 0, 0, 0, time.UTC),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
} {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
actEvent, err := item.NewEvent(tc.it)
|
||||||
|
actErr := err != nil
|
||||||
|
if tc.expErr != actErr {
|
||||||
|
t.Errorf("exp %v, got %v", tc.expErr, actErr)
|
||||||
|
}
|
||||||
|
if tc.expErr {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if diff := cmp.Diff(tc.expEvent, actEvent); diff != "" {
|
||||||
|
fmt.Errorf("(exp +, got -)\n%s", diff)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue