type
This commit is contained in:
parent
f28a364df5
commit
4818d7e71a
|
@ -2,12 +2,17 @@ package item
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/go-cmp/cmp"
|
"github.com/google/go-cmp/cmp"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrInvalidKind = errors.New("invalid kind")
|
||||||
|
)
|
||||||
|
|
||||||
type Kind string
|
type Kind string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
|
@ -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))
|
||||||
|
}
|
|
@ -56,7 +56,7 @@ type Task struct {
|
||||||
|
|
||||||
func NewTask(i Item) (Task, error) {
|
func NewTask(i Item) (Task, error) {
|
||||||
if i.Kind != KindTask {
|
if i.Kind != KindTask {
|
||||||
return Task{}, fmt.Errorf("item is not an task")
|
return Task{}, ErrInvalidKind
|
||||||
}
|
}
|
||||||
|
|
||||||
var t Task
|
var t Task
|
||||||
|
|
Loading…
Reference in New Issue