This commit is contained in:
Erik Winter 2024-12-22 12:27:44 +01:00
parent 7de4408926
commit e5fd6a6f14
2 changed files with 138 additions and 132 deletions

View File

@ -1,7 +1,11 @@
package item package item
import ( import (
"encoding/json"
"fmt"
"time" "time"
"github.com/google/go-cmp/cmp"
) )
type EventBody struct { type EventBody struct {
@ -11,67 +15,60 @@ type EventBody struct {
Duration time.Duration `json:"duration"` Duration time.Duration `json:"duration"`
} }
// func (e EventBody) MarshalJSON() ([]byte, error) { func (e EventBody) MarshalJSON() ([]byte, error) {
// type Alias EventBody type Alias EventBody
// return json.Marshal(&struct { return json.Marshal(&struct {
// Start string `json:"start"` Duration string `json:"duration"`
// Duration string `json:"duration"` *Alias
// *Alias }{
// }{ Duration: e.Duration.String(),
// Start: e.Start.UTC().Format(time.RFC3339), Alias: (*Alias)(&e),
// Duration: e.Duration.String(), })
// Alias: (*Alias)(&e), }
// })
// }
// func (e *EventBody) UnmarshalJSON(data []byte) error { func (e *EventBody) UnmarshalJSON(data []byte) error {
// type Alias EventBody type Alias EventBody
// aux := &struct { aux := &struct {
// Start string `json:"start"` Duration string `json:"duration"`
// Duration string `json:"duration"` *Alias
// *Alias }{
// }{ Alias: (*Alias)(e),
// Alias: (*Alias)(e), }
// } if err := json.Unmarshal(data, &aux); err != nil {
// if err := json.Unmarshal(data, &aux); err != nil { return err
// return err }
// }
// var err error var err error
// if e.Start, err = time.Parse(time.RFC3339, aux.Start); err != nil { if e.Duration, err = time.ParseDuration(aux.Duration); err != nil {
// return err return err
// } }
// if e.Duration, err = time.ParseDuration(aux.Duration); err != nil { return nil
// return err }
// }
// return nil type Event struct {
// } ID string `json:"id"`
Recurrer Recurrer `json:"recurrer"`
RecurNext Date `json:"recurNext"`
EventBody
}
// type Event struct { func NewEvent(i Item) (Event, error) {
// ID string `json:"id"` if i.Kind != KindEvent {
// Recurrer Recurrer `json:"recurrer"` return Event{}, fmt.Errorf("item is not an event")
// RecurNext time.Time `json:"recurNext"` }
// EventBody
// }
// func NewEvent(i Item) (Event, error) { var e Event
// if i.Kind != KindEvent { if err := json.Unmarshal([]byte(i.Body), &e); err != nil {
// return Event{}, fmt.Errorf("item is not an event") return Event{}, fmt.Errorf("could not unmarshal item body: %v", err)
// } }
// var e Event e.ID = i.ID
// if err := json.Unmarshal([]byte(i.Body), &e); err != nil { e.Recurrer = i.Recurrer
// return Event{}, fmt.Errorf("could not unmarshal item body: %v", err) e.RecurNext = i.RecurNext
// }
// e.ID = i.ID return e, nil
// e.Recurrer = i.Recurrer }
// e.RecurNext = i.RecurNext
// return e, nil
// }
// func (e Event) Item() (Item, error) { // func (e Event) Item() (Item, error) {
// body, err := json.Marshal(EventBody{ // body, err := json.Marshal(EventBody{
@ -108,3 +105,10 @@ type EventBody struct {
// return true // return true
// } // }
func EventDiff(a, b Event) string {
aJSON, _ := json.Marshal(a)
bJSON, _ := json.Marshal(b)
return cmp.Diff(string(aJSON), string(bJSON))
}

View File

@ -1,85 +1,87 @@
package item_test package item_test
// func TestNewEvent(t *testing.T) { import (
// t.Parallel() "testing"
"time"
// oneHour, err := time.ParseDuration("1h") "go-mod.ewintr.nl/planner/item"
// if err != nil { )
// t.Errorf("exp nil, got %v", err)
// } func TestNewEvent(t *testing.T) {
// for _, tc := range []struct { t.Parallel()
// name string
// it item.Item oneHour, err := time.ParseDuration("1h")
// expEvent item.Event if err != nil {
// expErr bool t.Errorf("exp nil, got %v", err)
// }{ }
// { for _, tc := range []struct {
// name: "wrong kind", name string
// it: item.Item{ it item.Item
// ID: "a", expEvent item.Event
// Kind: item.KindTask, expErr bool
// Body: `{ }{
// "title":"title", {
// "start":"2024-09-20T08:00:00Z", name: "wrong kind",
// "duration":"1h" it: item.Item{
// }`, ID: "a",
// }, Kind: item.KindTask,
// expErr: true, Body: `{
// }, "title":"title",
// { "date":"2024-09-20",
// name: "invalid json", "time":"08:00",
// it: item.Item{ "duration":"1h"
// ID: "a", }`,
// Kind: item.KindEvent, },
// Body: `{"id":"a"`, expErr: true,
// }, },
// expErr: true, {
// }, name: "invalid json",
// { it: item.Item{
// name: "valid", ID: "a",
// it: item.Item{ Kind: item.KindEvent,
// ID: "a", Body: `{"id":"a"`,
// Kind: item.KindEvent, },
// Recurrer: &item.Recur{ expErr: true,
// Start: time.Date(2024, 12, 8, 9, 0, 0, 0, time.UTC), },
// Period: item.PeriodDay, {
// Count: 1, name: "valid",
// }, it: item.Item{
// Body: `{ ID: "a",
// "title":"title", Kind: item.KindEvent,
// "start":"2024-09-20T08:00:00Z", Recurrer: item.NewRecurrer("2024-12-08, daily"),
// "duration":"1h" Body: `{
// }`, "title":"title",
// }, "date":"2024-09-20",
// expEvent: item.Event{ "time":"08:00",
// ID: "a", "duration":"1h"
// Recurrer: &item.Recur{ }`,
// Start: time.Date(2024, 12, 8, 9, 0, 0, 0, time.UTC), },
// Period: item.PeriodDay, expEvent: item.Event{
// Count: 1, ID: "a",
// }, Recurrer: item.NewRecurrer("2024-12-08, daily"),
// EventBody: item.EventBody{ EventBody: item.EventBody{
// Title: "title", Title: "title",
// Start: time.Date(2024, 9, 20, 8, 0, 0, 0, time.UTC), Date: item.NewDate(2024, 9, 20),
// Duration: oneHour, Time: item.NewTime(8, 0),
// }, Duration: oneHour,
// }, },
// }, },
// } { },
// t.Run(tc.name, func(t *testing.T) { } {
// actEvent, actErr := item.NewEvent(tc.it) t.Run(tc.name, func(t *testing.T) {
// if tc.expErr != (actErr != nil) { actEvent, actErr := item.NewEvent(tc.it)
// t.Errorf("exp nil, got %v", actErr) if tc.expErr != (actErr != nil) {
// } t.Errorf("exp nil, got %v", actErr)
// if tc.expErr { }
// return if tc.expErr {
// } return
// if diff := cmp.Diff(tc.expEvent, actEvent); diff != "" { }
// t.Errorf("(exp +, got -)\n%s", diff) if diff := item.EventDiff(tc.expEvent, actEvent); diff != "" {
// } t.Errorf("(+exp, -got)\n%s", diff)
// }) }
// } })
// } }
}
// func TestEventItem(t *testing.T) { // func TestEventItem(t *testing.T) {
// t.Parallel() // t.Parallel()