diff --git a/item/item.go b/item/item.go index af1b379..e4550ff 100644 --- a/item/item.go +++ b/item/item.go @@ -2,12 +2,17 @@ package item import ( "encoding/json" + "errors" "time" "github.com/google/go-cmp/cmp" "github.com/google/uuid" ) +var ( + ErrInvalidKind = errors.New("invalid kind") +) + type Kind string const ( diff --git a/item/schedule.go b/item/schedule.go new file mode 100644 index 0000000..2f7dfd3 --- /dev/null +++ b/item/schedule.go @@ -0,0 +1,64 @@ +package item + +import ( + "encoding/json" + "fmt" + + "github.com/google/go-cmp/cmp" +) + +type ScheduleBody struct { + Title string `json:"title"` +} + +type Schedule struct { + ID string `json:"id"` + Date Date `json:"date"` + Recurrer Recurrer `json:"recurrer"` + RecurNext Date `json:"recurNext"` + ScheduleBody +} + +func NewSchedule(i Item) (Schedule, error) { + if i.Kind != KindSchedule { + return Schedule{}, ErrInvalidKind + } + + var s Schedule + if err := json.Unmarshal([]byte(i.Body), &s); err != nil { + return Schedule{}, fmt.Errorf("could not unmarshal item body: %v", err) + } + + s.ID = i.ID + s.Date = i.Date + + return s, nil +} + +func (s Schedule) Item() (Item, error) { + body, err := json.Marshal(s.ScheduleBody) + if err != nil { + return Item{}, fmt.Errorf("could not marshal schedule body: %v", err) + } + + return Item{ + ID: s.ID, + Kind: KindSchedule, + Date: s.Date, + Body: string(body), + }, nil +} + +func ScheduleDiff(a, b Schedule) string { + aJSON, _ := json.Marshal(a) + bJSON, _ := json.Marshal(b) + + return cmp.Diff(string(aJSON), string(bJSON)) +} + +func ScheduleDiffs(a, b []Schedule) string { + aJSON, _ := json.Marshal(a) + bJSON, _ := json.Marshal(b) + + return cmp.Diff(string(aJSON), string(bJSON)) +} diff --git a/item/task.go b/item/task.go index a9eb0d8..4286f57 100644 --- a/item/task.go +++ b/item/task.go @@ -56,7 +56,7 @@ type Task struct { func NewTask(i Item) (Task, error) { if i.Kind != KindTask { - return Task{}, fmt.Errorf("item is not an task") + return Task{}, ErrInvalidKind } var t Task