planner/item/item.go

79 lines
1.4 KiB
Go
Raw Normal View History

2024-09-18 07:55:14 +02:00
package item
2024-08-20 08:34:11 +02:00
import (
2024-12-19 12:06:03 +01:00
"encoding/json"
2024-08-20 08:34:11 +02:00
"time"
2024-12-25 11:16:12 +01:00
"github.com/google/go-cmp/cmp"
2024-08-20 08:34:11 +02:00
"github.com/google/uuid"
)
2024-09-08 11:17:49 +02:00
type Kind string
const (
2024-12-24 08:00:23 +01:00
KindSchedule Kind = "schedule"
KindTask Kind = "task"
2024-09-11 07:33:22 +02:00
)
var (
2024-12-24 08:00:23 +01:00
KnownKinds = []Kind{KindSchedule, KindTask}
2024-09-08 11:17:49 +02:00
)
2024-09-09 07:36:05 +02:00
type Item struct {
2024-12-01 10:22:47 +01:00
ID string `json:"id"`
Kind Kind `json:"kind"`
Updated time.Time `json:"updated"`
Deleted bool `json:"deleted"`
2024-12-19 12:06:03 +01:00
Date Date `json:"date"`
Recurrer Recurrer `json:"recurrer"`
RecurNext Date `json:"recurNext"`
2024-12-01 10:22:47 +01:00
Body string `json:"body"`
2024-08-28 07:21:02 +02:00
}
2024-12-19 12:06:03 +01:00
func (i Item) MarshalJSON() ([]byte, error) {
var recurStr string
if i.Recurrer != nil {
recurStr = i.Recurrer.String()
}
type Alias Item
return json.Marshal(&struct {
Recurrer string `json:"recurrer"`
*Alias
}{
Recurrer: recurStr,
Alias: (*Alias)(&i),
})
}
func (i *Item) UnmarshalJSON(data []byte) error {
type Alias Item
aux := &struct {
Recurrer string `json:"recurrer"`
*Alias
}{
Alias: (*Alias)(i),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
i.Recurrer = NewRecurrer(aux.Recurrer)
return nil
}
2024-09-11 07:33:22 +02:00
func NewItem(k Kind, body string) Item {
2024-09-09 07:36:05 +02:00
return Item{
2024-08-28 07:21:02 +02:00
ID: uuid.New().String(),
2024-09-11 07:33:22 +02:00
Kind: k,
2024-08-28 07:21:02 +02:00
Updated: time.Now(),
2024-09-09 07:36:05 +02:00
Body: body,
2024-08-28 07:21:02 +02:00
}
2024-08-20 08:34:11 +02:00
}
2024-12-25 11:16:12 +01:00
func ItemDiff(exp, got Item) string {
expJSON, _ := json.Marshal(exp)
actJSON, _ := json.Marshal(got)
return cmp.Diff(string(expJSON), string(actJSON))
}