wip
This commit is contained in:
parent
7de4408926
commit
e5fd6a6f14
110
item/event.go
110
item/event.go
|
@ -1,7 +1,11 @@
|
|||
package item
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
)
|
||||
|
||||
type EventBody struct {
|
||||
|
@ -11,67 +15,60 @@ type EventBody struct {
|
|||
Duration time.Duration `json:"duration"`
|
||||
}
|
||||
|
||||
// func (e EventBody) MarshalJSON() ([]byte, error) {
|
||||
// type Alias EventBody
|
||||
// return json.Marshal(&struct {
|
||||
// Start string `json:"start"`
|
||||
// Duration string `json:"duration"`
|
||||
// *Alias
|
||||
// }{
|
||||
// Start: e.Start.UTC().Format(time.RFC3339),
|
||||
// Duration: e.Duration.String(),
|
||||
// Alias: (*Alias)(&e),
|
||||
// })
|
||||
// }
|
||||
func (e EventBody) MarshalJSON() ([]byte, error) {
|
||||
type Alias EventBody
|
||||
return json.Marshal(&struct {
|
||||
Duration string `json:"duration"`
|
||||
*Alias
|
||||
}{
|
||||
Duration: e.Duration.String(),
|
||||
Alias: (*Alias)(&e),
|
||||
})
|
||||
}
|
||||
|
||||
// func (e *EventBody) UnmarshalJSON(data []byte) error {
|
||||
// type Alias EventBody
|
||||
// aux := &struct {
|
||||
// Start string `json:"start"`
|
||||
// Duration string `json:"duration"`
|
||||
// *Alias
|
||||
// }{
|
||||
// Alias: (*Alias)(e),
|
||||
// }
|
||||
// if err := json.Unmarshal(data, &aux); err != nil {
|
||||
// return err
|
||||
// }
|
||||
func (e *EventBody) UnmarshalJSON(data []byte) error {
|
||||
type Alias EventBody
|
||||
aux := &struct {
|
||||
Duration string `json:"duration"`
|
||||
*Alias
|
||||
}{
|
||||
Alias: (*Alias)(e),
|
||||
}
|
||||
if err := json.Unmarshal(data, &aux); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// var err error
|
||||
// if e.Start, err = time.Parse(time.RFC3339, aux.Start); err != nil {
|
||||
// return err
|
||||
// }
|
||||
var err error
|
||||
if e.Duration, err = time.ParseDuration(aux.Duration); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// if e.Duration, err = time.ParseDuration(aux.Duration); err != nil {
|
||||
// return err
|
||||
// }
|
||||
return nil
|
||||
}
|
||||
|
||||
// return nil
|
||||
// }
|
||||
type Event struct {
|
||||
ID string `json:"id"`
|
||||
Recurrer Recurrer `json:"recurrer"`
|
||||
RecurNext Date `json:"recurNext"`
|
||||
EventBody
|
||||
}
|
||||
|
||||
// type Event struct {
|
||||
// ID string `json:"id"`
|
||||
// Recurrer Recurrer `json:"recurrer"`
|
||||
// RecurNext time.Time `json:"recurNext"`
|
||||
// EventBody
|
||||
// }
|
||||
func NewEvent(i Item) (Event, error) {
|
||||
if i.Kind != KindEvent {
|
||||
return Event{}, fmt.Errorf("item is not an event")
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
|
||||
// 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
|
||||
e.Recurrer = i.Recurrer
|
||||
e.RecurNext = i.RecurNext
|
||||
|
||||
// e.ID = i.ID
|
||||
// e.Recurrer = i.Recurrer
|
||||
// e.RecurNext = i.RecurNext
|
||||
|
||||
// return e, nil
|
||||
// }
|
||||
return e, nil
|
||||
}
|
||||
|
||||
// func (e Event) Item() (Item, error) {
|
||||
// body, err := json.Marshal(EventBody{
|
||||
|
@ -108,3 +105,10 @@ type EventBody struct {
|
|||
|
||||
// return true
|
||||
// }
|
||||
|
||||
func EventDiff(a, b Event) string {
|
||||
aJSON, _ := json.Marshal(a)
|
||||
bJSON, _ := json.Marshal(b)
|
||||
|
||||
return cmp.Diff(string(aJSON), string(bJSON))
|
||||
}
|
||||
|
|
|
@ -1,85 +1,87 @@
|
|||
package item_test
|
||||
|
||||
// func TestNewEvent(t *testing.T) {
|
||||
// t.Parallel()
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
// oneHour, err := time.ParseDuration("1h")
|
||||
// if err != nil {
|
||||
// t.Errorf("exp nil, got %v", err)
|
||||
// }
|
||||
// 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",
|
||||
// "duration":"1h"
|
||||
// }`,
|
||||
// },
|
||||
// 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,
|
||||
// Recurrer: &item.Recur{
|
||||
// Start: time.Date(2024, 12, 8, 9, 0, 0, 0, time.UTC),
|
||||
// Period: item.PeriodDay,
|
||||
// Count: 1,
|
||||
// },
|
||||
// Body: `{
|
||||
// "title":"title",
|
||||
// "start":"2024-09-20T08:00:00Z",
|
||||
// "duration":"1h"
|
||||
// }`,
|
||||
// },
|
||||
// expEvent: item.Event{
|
||||
// ID: "a",
|
||||
// Recurrer: &item.Recur{
|
||||
// Start: time.Date(2024, 12, 8, 9, 0, 0, 0, time.UTC),
|
||||
// Period: item.PeriodDay,
|
||||
// Count: 1,
|
||||
// },
|
||||
// EventBody: item.EventBody{
|
||||
// Title: "title",
|
||||
// Start: time.Date(2024, 9, 20, 8, 0, 0, 0, time.UTC),
|
||||
// Duration: oneHour,
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// } {
|
||||
// t.Run(tc.name, func(t *testing.T) {
|
||||
// actEvent, actErr := item.NewEvent(tc.it)
|
||||
// if tc.expErr != (actErr != nil) {
|
||||
// t.Errorf("exp nil, got %v", actErr)
|
||||
// }
|
||||
// if tc.expErr {
|
||||
// return
|
||||
// }
|
||||
// if diff := cmp.Diff(tc.expEvent, actEvent); diff != "" {
|
||||
// t.Errorf("(exp +, got -)\n%s", diff)
|
||||
// }
|
||||
// })
|
||||
// }
|
||||
// }
|
||||
"go-mod.ewintr.nl/planner/item"
|
||||
)
|
||||
|
||||
func TestNewEvent(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
oneHour, err := time.ParseDuration("1h")
|
||||
if err != nil {
|
||||
t.Errorf("exp nil, got %v", err)
|
||||
}
|
||||
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",
|
||||
"date":"2024-09-20",
|
||||
"time":"08:00",
|
||||
"duration":"1h"
|
||||
}`,
|
||||
},
|
||||
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,
|
||||
Recurrer: item.NewRecurrer("2024-12-08, daily"),
|
||||
Body: `{
|
||||
"title":"title",
|
||||
"date":"2024-09-20",
|
||||
"time":"08:00",
|
||||
"duration":"1h"
|
||||
}`,
|
||||
},
|
||||
expEvent: item.Event{
|
||||
ID: "a",
|
||||
Recurrer: item.NewRecurrer("2024-12-08, daily"),
|
||||
EventBody: item.EventBody{
|
||||
Title: "title",
|
||||
Date: item.NewDate(2024, 9, 20),
|
||||
Time: item.NewTime(8, 0),
|
||||
Duration: oneHour,
|
||||
},
|
||||
},
|
||||
},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
actEvent, actErr := item.NewEvent(tc.it)
|
||||
if tc.expErr != (actErr != nil) {
|
||||
t.Errorf("exp nil, got %v", actErr)
|
||||
}
|
||||
if tc.expErr {
|
||||
return
|
||||
}
|
||||
if diff := item.EventDiff(tc.expEvent, actEvent); diff != "" {
|
||||
t.Errorf("(+exp, -got)\n%s", diff)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// func TestEventItem(t *testing.T) {
|
||||
// t.Parallel()
|
||||
|
|
Loading…
Reference in New Issue