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 (
programBinary string = "musala"
programBinary string = "kiwimix"
)
var (

6
go.mod
View file

@ -6,6 +6,12 @@ require (
github.com/PuloV/ics-golang v0.0.0-20190808201353-a3394d3bcade
github.com/spf13/cobra v1.7.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 (

View file

@ -14,19 +14,42 @@ func NewCalendar() *Calendar {
return &Calendar{*ics.NewCalendar()}
}
// SetEvent adds an event to the calendar
func (cal *Calendar) SetEvent(event Event) (*Calendar, error) {
c, err := cal.SetEvent(event)
return c, err
// ParseCalendarFile parses an ICS file and returns a calendar
func ParseCalendarFile(filePath string) (*Calendar, error) {
return &Calendar{*ics.NewCalendar()}, nil
}
// 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
func (cal *Calendar) GetEvents() []Event {
func (cal *Calendar) GetEvents() []*Event {
subcal := cal.Calendar
subevents := subcal.GetEvents()
wrapped := make([]Event, len(subevents))
wrapped := make([]*Event, len(subevents))
for i, v := range subevents {
wrapped[i] = Event{v}
wrapped[i] = &Event{v}
}
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 {
ics.Event
}
func (event *Event) Equals(other *Event) bool {
return false
}

View file

@ -1 +1,9 @@
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
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
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
@ -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 isUnique {
differenceCalendar.SetEvent(event1)
differenceCalendar.AddEvent(event1)
}
}

View file

@ -13,9 +13,9 @@ type IntersectCalendarOperation struct{}
// Ensure IntersectCalendarOperation implements CalendarOperation
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 {
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.
return calendars[0], nil // Replace this with actual implementation

View file

@ -15,9 +15,9 @@ type UnionCalendarOperation struct {
var _ CalendarOperation = (*UnionCalendarOperation)(nil)
// 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 {
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.
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.
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)
// 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 {
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.
return calendars[0], nil // Replace this with actual implementation

View file

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

View file

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

View file

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

View file

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

View file

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