fix: ensure type consistency accross code

This commit is contained in:
Glenn Y. Rolland 2023-08-06 12:52:27 +02:00
parent d0a9a06455
commit 6d566ba077
16 changed files with 114 additions and 46 deletions

View file

@ -12,7 +12,7 @@ import (
) )
const ( const (
programBinary string = "musala" programBinary string = "kiwimix"
) )
var ( var (

6
go.mod
View file

@ -6,6 +6,12 @@ require (
github.com/PuloV/ics-golang v0.0.0-20190808201353-a3394d3bcade github.com/PuloV/ics-golang v0.0.0-20190808201353-a3394d3bcade
github.com/spf13/cobra v1.7.0 github.com/spf13/cobra v1.7.0
github.com/spf13/viper v1.15.0 github.com/spf13/viper v1.15.0
github.com/stretchr/testify v1.8.1
)
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
) )
require ( require (

View file

@ -14,19 +14,42 @@ func NewCalendar() *Calendar {
return &Calendar{*ics.NewCalendar()} return &Calendar{*ics.NewCalendar()}
} }
// SetEvent adds an event to the calendar // ParseCalendarFile parses an ICS file and returns a calendar
func (cal *Calendar) SetEvent(event Event) (*Calendar, error) { func ParseCalendarFile(filePath string) (*Calendar, error) {
c, err := cal.SetEvent(event) return &Calendar{*ics.NewCalendar()}, nil
return c, err }
// AddEvent adds an event to the calendar
func (cal *Calendar) AddEvent(event *Event) (*Calendar, error) {
//c, err := cal.SetEvent(event)
return cal, nil
}
// AddEvents adds all events to the calendar
func (cal *Calendar) AddEvents(event []*Event) (*Calendar, error) {
//c, err := cal.SetEvent(event)
return cal, nil
}
// AddEvent adds an event to the calendar
func (cal *Calendar) RemoveEvent(event *Event) (*Calendar, error) {
// c, err := cal.SetEvent(event)
return cal, nil
}
// AddEvent adds an event to the calendar
func (cal *Calendar) RemoveEvents(event []*Event) (*Calendar, error) {
// c, err := cal.SetEvent(event)
return cal, nil
} }
// GetEvents gets all events in the calendar // GetEvents gets all events in the calendar
func (cal *Calendar) GetEvents() []Event { func (cal *Calendar) GetEvents() []*Event {
subcal := cal.Calendar subcal := cal.Calendar
subevents := subcal.GetEvents() subevents := subcal.GetEvents()
wrapped := make([]Event, len(subevents)) wrapped := make([]*Event, len(subevents))
for i, v := range subevents { for i, v := range subevents {
wrapped[i] = Event{v} wrapped[i] = &Event{v}
} }
return wrapped return wrapped
} }

View file

@ -0,0 +1,13 @@
package basetypes_test
import (
"testing"
"code.apps.glenux.net/glenux/kiwimix/pkg/basetypes"
"github.com/stretchr/testify/assert"
)
func TestCalendarFoo(t *testing.T) {
cal = basetypes.NewCalendar()
assert.Equal(t, cal, nil, "they should be equal")
}

View file

@ -6,3 +6,7 @@ import ics "github.com/PuloV/ics-golang"
type Event struct { type Event struct {
ics.Event ics.Event
} }
func (event *Event) Equals(other *Event) bool {
return false
}

View file

@ -1 +1,9 @@
package basetypes package basetypes
type Stack struct {
calendars []Calendar
}
func (stack *Stack) Push(calendar *Calendar) (*Stack, error) {
return nil, nil
}

View file

@ -13,10 +13,10 @@ type DifferenceCalendarOperation struct{}
// Ensure DifferenceCalendarOperation implements CalendarOperation // Ensure DifferenceCalendarOperation implements CalendarOperation
var _ CalendarOperation = (*DifferenceCalendarOperation)(nil) var _ CalendarOperation = (*DifferenceCalendarOperation)(nil)
func (op DifferenceCalendarOperation) Execute(calendars ...basetypes.Calendar) (basetypes.Calendar, error) { func (op *DifferenceCalendarOperation) Execute(calendars ...*basetypes.Calendar) (*basetypes.Calendar, error) {
// Check that two calendars are provided // Check that two calendars are provided
if len(calendars) != 2 { if len(calendars) != 2 {
return basetypes.Calendar{}, errors.New("difference operation requires exactly two calendars") return &basetypes.Calendar{}, errors.New("difference operation requires exactly two calendars")
} }
// Create a new calendar to store the difference // Create a new calendar to store the difference
@ -38,7 +38,7 @@ func (op DifferenceCalendarOperation) Execute(calendars ...basetypes.Calendar) (
// If the event is unique to the first calendar, add it to the difference calendar // If the event is unique to the first calendar, add it to the difference calendar
if isUnique { if isUnique {
differenceCalendar.SetEvent(event1) differenceCalendar.AddEvent(event1)
} }
} }

View file

@ -13,9 +13,9 @@ type IntersectCalendarOperation struct{}
// Ensure IntersectCalendarOperation implements CalendarOperation // Ensure IntersectCalendarOperation implements CalendarOperation
var _ CalendarOperation = (*IntersectCalendarOperation)(nil) var _ CalendarOperation = (*IntersectCalendarOperation)(nil)
func (op IntersectCalendarOperation) Execute(calendars ...basetypes.Calendar) (basetypes.Calendar, error) { func (op *IntersectCalendarOperation) Execute(calendars ...*basetypes.Calendar) (*basetypes.Calendar, error) {
if len(calendars) != 2 { if len(calendars) != 2 {
return basetypes.Calendar{}, errors.New("Difference operation requires exactly two calendars") return &basetypes.Calendar{}, errors.New("Difference operation requires exactly two calendars")
} }
// Do your union operation here and return the new union basetypes. // Do your union operation here and return the new union basetypes.
return calendars[0], nil // Replace this with actual implementation return calendars[0], nil // Replace this with actual implementation

View file

@ -15,9 +15,9 @@ type UnionCalendarOperation struct {
var _ CalendarOperation = (*UnionCalendarOperation)(nil) var _ CalendarOperation = (*UnionCalendarOperation)(nil)
// Execute combines the events of two calendars and returns a new calendar that contains all events from both. // Execute combines the events of two calendars and returns a new calendar that contains all events from both.
func (op *UnionCalendarOperation) Execute(calendars ...basetypes.Calendar) (basetypes.Calendar, error) { func (op *UnionCalendarOperation) Execute(calendars ...*basetypes.Calendar) (*basetypes.Calendar, error) {
if len(calendars) != 2 { if len(calendars) != 2 {
return basetypes.Calendar{}, errors.New("Union operation requires exactly two calendars") return &basetypes.Calendar{}, errors.New("Union operation requires exactly two calendars")
} }
// Do your union operation here and return the new union basetypes. // Do your union operation here and return the new union basetypes.
return calendars[0], nil // Replace this with actual implementation return calendars[0], nil // Replace this with actual implementation

View file

@ -4,5 +4,5 @@ import basetypes "code.apps.glenux.net/glenux/kiwimix/pkg/basetypes"
// CalendarOperation defines the interface for operations on calendars. // CalendarOperation defines the interface for operations on calendars.
type CalendarOperation interface { type CalendarOperation interface {
Execute(calendars ...basetypes.Calendar) (basetypes.Calendar, error) Execute(calendars ...*basetypes.Calendar) (*basetypes.Calendar, error)
} }

View file

@ -15,9 +15,9 @@ type FilterCalendarOperation struct {
var _ CalendarOperation = (*FilterCalendarOperation)(nil) var _ CalendarOperation = (*FilterCalendarOperation)(nil)
// Execute filters the events of a calendar and returns a new filtered calendar. // Execute filters the events of a calendar and returns a new filtered calendar.
func (op *FilterCalendarOperation) Execute(calendars ...basetypes.Calendar) (basetypes.Calendar, error) { func (op *FilterCalendarOperation) Execute(calendars ...*basetypes.Calendar) (*basetypes.Calendar, error) {
if len(calendars) != 1 { if len(calendars) != 1 {
return basetypes.Calendar{}, errors.New("Filter operation requires exactly one calendar") return &basetypes.Calendar{}, errors.New("Filter operation requires exactly one calendar")
} }
// Do your filter operation here and return the filtered calendar. // Do your filter operation here and return the filtered calendar.
return calendars[0], nil // Replace this with actual implementation return calendars[0], nil // Replace this with actual implementation

View file

@ -1,8 +1,6 @@
package calendarops package calendarops
import ( import (
"sort"
basetypes "code.apps.glenux.net/glenux/kiwimix/pkg/basetypes" basetypes "code.apps.glenux.net/glenux/kiwimix/pkg/basetypes"
) )
@ -19,27 +17,33 @@ func NewMergeCalendarOperation() *MergeCalendarOperation {
} }
// Execute réalise l'opération de fusion. // Execute réalise l'opération de fusion.
func (op *MergeCalendarOperation) Execute(cal ...basetypes.Calendar) (basetypes.Calendar, error) { func (op *MergeCalendarOperation) Execute(cal ...*basetypes.Calendar) (*basetypes.Calendar, error) {
if len(cal.Events) == 0 { // Guard: exit if no calendar provided
return cal, nil if len(cal) == 0 {
return nil, nil
} }
sort.Slice(cal.Event, func(i, j int) bool { mergedEvents := cal[0].GetEvents()
return cal.Event[i].Start.Before(cal.Event[j].Start) /*
}) events := cal.GetEvents()
sort.Slice(cal.Event, func(i, j int) bool {
return cal.Event[i].Start.Before(cal.Event[j].Start)
})
mergedEvents := []basetypes.Event{cal.Event[0]} mergedEvents := []basetypes.Event{cal.Event[0]}
for _, event := range cal.Event[1:] { for _, event := range cal.Event[1:] {
lastEvent := &mergedEvents[len(mergedEvents)-1] lastEvent := &mergedEvents[len(mergedEvents)-1]
if event.Start.Before(lastEvent.End) || event.Start.Equal(lastEvent.End) { if event.Start.Before(lastEvent.End) || event.Start.Equal(lastEvent.End) {
if event.End.After(lastEvent.End) { if event.End.After(lastEvent.End) {
lastEvent.End = event.End lastEvent.End = event.End
}
} else {
mergedEvents = append(mergedEvents, event)
} }
} else {
mergedEvents = append(mergedEvents, event)
} }
} */
mergedCalendar := &basetypes.Calendar{}
return &basetypes.Calendar{Events: mergedEvents}, nil mergedCalendar.AddEvents(mergedEvents)
return mergedCalendar, nil
} }

View file

@ -1,16 +1,24 @@
package stackops package stackops
import "time" import (
"time"
basetypes "code.apps.glenux.net/glenux/kiwimix/pkg/basetypes"
)
// GeneratorLimit is a type definining // GeneratorLimit is a type definining
type GenerateConfig struct { type GenerateConfig struct {
DateBegin *time.Date DateBegin *time.Time
DateEnd *time.Date DateEnd *time.Time
TimeBegin *time.Time TimeBegin *time.Time
TimeEnd *time.Time TimeEnd *time.Time
} }
type GenerateStackOperation struct{}
var _ StackOperation = (*GenerateStackOperation)(nil)
// generate events that last all day (from X hour to Y hour) // generate events that last all day (from X hour to Y hour)
func (cal *Calendar) generate() *Calendar { func (op *GenerateStackOperation) Execute(stack *basetypes.Stack, str string) (*basetypes.Stack, error) {
return nil return nil, nil
} }

View file

@ -6,8 +6,8 @@ import (
type LoadOperation struct{} type LoadOperation struct{}
func (op *LoadOperation) Execute(stack *basetypes.CalendarStack, filePath string) error { func (op *LoadOperation) Execute(stack *basetypes.Stack, filePath string) error {
cal, err := basetypes.ParseICSFile(filePath) // hypothetically, ParseICSFile parses an ICS file into a Calendar object cal, err := basetypes.ParseCalendarFile(filePath) // hypothetically, ParseICSFile parses an ICS file into a Calendar object
if err != nil { if err != nil {
return err return err
} }

View file

@ -13,7 +13,7 @@ func NewSaveOperation(filePath string) *SaveOperation {
return saveop return saveop
} }
func (op *SaveOperation) Execute(stack *basetypes.CalendarStack) error { func (op *SaveOperation) Execute(stack *basetypes.Stack) error {
cal, err := basetypes.ParseCalendarFile(op.filePath) cal, err := basetypes.ParseCalendarFile(op.filePath)
if err != nil { if err != nil {
return err return err

View file

@ -1,5 +1,7 @@
package stackops package stackops
import basetypes "code.apps.glenux.net/glenux/kiwimix/pkg/basetypes"
type StackOperation interface { type StackOperation interface {
Execute(stack *CalendarStack, filePath string) error Execute(stack *basetypes.Stack, filePath string) (*basetypes.Stack, error)
} }