From b96cb471c4899515bb0fabcd36fe80a38ee02f49 Mon Sep 17 00:00:00 2001 From: Alexander Huck Date: Sat, 20 Apr 2024 17:34:39 +0200 Subject: [PATCH 1/9] fix: type conversion in autoConfigure function for integers --- internal/sync/util.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/sync/util.go b/internal/sync/util.go index 8512c55..132795a 100644 --- a/internal/sync/util.go +++ b/internal/sync/util.go @@ -20,7 +20,7 @@ func autoConfigure(object any, config config.CustomMap) { reflect.Int16, reflect.Int32, reflect.Int64: - field.SetInt(value.(int64)) + field.SetInt(int64(value.(int))) case reflect.Bool: field.SetBool(value.(bool)) case reflect.String: From 6789b658a41267881903eb71c0832157285eae2c Mon Sep 17 00:00:00 2001 From: Alexander Huck Date: Sat, 20 Apr 2024 17:38:30 +0200 Subject: [PATCH 2/9] feat: add timeFrame Filter --- internal/filter/timeFrame.go | 21 +++++++++++++++++++++ internal/sync/filter.go | 2 ++ 2 files changed, 23 insertions(+) create mode 100644 internal/filter/timeFrame.go diff --git a/internal/filter/timeFrame.go b/internal/filter/timeFrame.go new file mode 100644 index 0000000..966a85e --- /dev/null +++ b/internal/filter/timeFrame.go @@ -0,0 +1,21 @@ +package filter + +import ( + "github.com/inovex/CalendarSync/internal/models" +) + +type TimeFrameEvents struct { + HourStart int64 + HourEnd int64 +} + +func (a TimeFrameEvents) Name() string { + return "TimeFrame" +} + +func (a TimeFrameEvents) Filter(event models.Event) bool { + if int64(event.StartTime.Hour()) >= a.HourStart && int64(event.StartTime.Hour()) <= a.HourEnd { + return true + } + return false +} diff --git a/internal/sync/filter.go b/internal/sync/filter.go index 3222995..d6043bd 100644 --- a/internal/sync/filter.go +++ b/internal/sync/filter.go @@ -30,12 +30,14 @@ func FilterEvent(event models.Event, filters ...Filter) (result bool) { var ( filterConfigMapping = map[string]Filter{ + "TimeFrame": &filter.TimeFrameEvents{}, "DeclinedEvents": &filter.DeclinedEvents{}, "AllDayEvents": &filter.AllDayEvents{}, "RegexTitle": &filter.RegexTitle{}, } filterOrder = []string{ + "TimeFrame", "DeclinedEvents", "AllDayEvents", "RegexTitle", From 314fc84cf805338314fd36962a3c0aba08198ba8 Mon Sep 17 00:00:00 2001 From: Alexander Huck Date: Sat, 20 Apr 2024 17:38:42 +0200 Subject: [PATCH 3/9] feat: add timeFrame filter tests --- internal/filter/timeFrame_test.go | 62 +++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 internal/filter/timeFrame_test.go diff --git a/internal/filter/timeFrame_test.go b/internal/filter/timeFrame_test.go new file mode 100644 index 0000000..ffc4be9 --- /dev/null +++ b/internal/filter/timeFrame_test.go @@ -0,0 +1,62 @@ +package filter_test + +import ( + "testing" + "time" + + "github.com/inovex/CalendarSync/internal/filter" + "github.com/inovex/CalendarSync/internal/models" +) + +const timeFormat string = "2006-01-02T15:04" + +// Events which match the start and end hour should be kept +func TestTimeFrameEventsFilter(t *testing.T) { + t1, err := time.Parse(timeFormat, "2024-01-01T13:00") + if err != nil { + t.Error(err) + } + + t2, err := time.Parse(timeFormat, "2024-01-01T18:00") + if err != nil { + t.Error(err) + } + + sourceEvents := []models.Event{ + // Should be kept + { + ICalUID: "testId", + ID: "testUid", + Title: "test", + Description: "bar", + AllDay: true, + StartTime: t1, + }, + // Should be filtered + { + ICalUID: "testId2", + ID: "testUid2", + Title: "Test 2", + Description: "bar", + AllDay: false, + StartTime: t2, + }, + // Should be kept + { + ICalUID: "testId3", + ID: "testUid2", + Title: "foo", + Description: "bar", + StartTime: t1, + }, + } + + expectedSinkEvents := []models.Event{sourceEvents[0], sourceEvents[2]} + + eventFilter := filter.TimeFrameEvents{ + // Events outside 8 am and 5 pm will be filtered. + HourStart: 8, + HourEnd: 17, + } + checkEventFilter(t, eventFilter, sourceEvents, expectedSinkEvents) +} From ae1f9dcb1062b0f328edd669cb4f98d6a3c7d064 Mon Sep 17 00:00:00 2001 From: Alexander Huck Date: Sat, 20 Apr 2024 17:38:51 +0200 Subject: [PATCH 4/9] docs: update readme with the new filter --- README.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 707e051..d1aaee3 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ asdf plugin add calendarsync https://github.com/FeryET/asdf-calendarsync.git ## finally asdf install calendarsync ``` -Note: The `asdf` plugin is not managed by inovex, but is provided by a CalendarSync user. inovex assumes no responsibility for proper provisioning. +Note: The `asdf` plugin is not managed by inovex, but is provided by a CalendarSync user. inovex assumes no responsibility for proper provisioning. ## First Time Execution @@ -167,6 +167,12 @@ filters: - name: DeclinedEvents # Events which cover the full day aren't synced - name: AllDayEvents + # Events within the specified timeframe will be retained, while all others will be filtered out. + # hours are represented in the 24h time format + - name: TimeFrame + config: + HourStart: 8 + HourEnd: 17 # Events where the title matches the ExcludeRegexp (RE2 Regex) aren't synced - name: RegexTitle config: From ab772d162ac3932026ff54ff253bc1a1438a08e8 Mon Sep 17 00:00:00 2001 From: Alexander Huck Date: Sat, 20 Apr 2024 17:39:04 +0200 Subject: [PATCH 5/9] docs: update example.sync.yaml with the new filter --- example.sync.yaml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/example.sync.yaml b/example.sync.yaml index 5dfd598..868e000 100644 --- a/example.sync.yaml +++ b/example.sync.yaml @@ -61,6 +61,12 @@ filters: - name: DeclinedEvents # Events which cover the full day aren't synced - name: AllDayEvents + # Events within the specified timeframe will be retained, while all others will be filtered out. + # hours are represented in the 24h time format + - name: TimeFrame + config: + HourStart: 8 + HourEnd: 17 # Events where the title matches the ExcludeRegexp (RE2 Regex) aren't synced - name: RegexTitle config: From 33e584cc8371d4fec0d464004104b1bf3153abca Mon Sep 17 00:00:00 2001 From: Alexander Huck Date: Sat, 20 Apr 2024 17:49:40 +0200 Subject: [PATCH 6/9] fix: use ints instead of int64s --- internal/filter/timeFrame.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/filter/timeFrame.go b/internal/filter/timeFrame.go index 966a85e..e794f37 100644 --- a/internal/filter/timeFrame.go +++ b/internal/filter/timeFrame.go @@ -5,8 +5,8 @@ import ( ) type TimeFrameEvents struct { - HourStart int64 - HourEnd int64 + HourStart int + HourEnd int } func (a TimeFrameEvents) Name() string { @@ -14,7 +14,7 @@ func (a TimeFrameEvents) Name() string { } func (a TimeFrameEvents) Filter(event models.Event) bool { - if int64(event.StartTime.Hour()) >= a.HourStart && int64(event.StartTime.Hour()) <= a.HourEnd { + if event.StartTime.Hour() >= a.HourStart && event.StartTime.Hour() <= a.HourEnd { return true } return false From 2e8c270bd30a0bbb4171144d0ae2a3e5c3a22fea Mon Sep 17 00:00:00 2001 From: Alexander Huck Date: Sun, 21 Apr 2024 14:52:54 +0200 Subject: [PATCH 7/9] docs: add comment on what happens if the users enters invalid hours --- internal/filter/timeFrame.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/filter/timeFrame.go b/internal/filter/timeFrame.go index e794f37..5afc2a1 100644 --- a/internal/filter/timeFrame.go +++ b/internal/filter/timeFrame.go @@ -14,6 +14,7 @@ func (a TimeFrameEvents) Name() string { } func (a TimeFrameEvents) Filter(event models.Event) bool { + // If the user enters invalid hours, such as numbers higher than 24 or lower than 0, all events will be synchronized up to that point in time. For example, if the start hour is -1 and the end hour is 17, all events until 17 o'clock will be synchronized. if event.StartTime.Hour() >= a.HourStart && event.StartTime.Hour() <= a.HourEnd { return true } From 4d3f7759ac502dc80a065850ec4dfd3c70cf0e8d Mon Sep 17 00:00:00 2001 From: Alexander Huck Date: Sat, 18 May 2024 11:52:20 +0200 Subject: [PATCH 8/9] fix: make sure to include all the events which are part of the timeframe defined --- internal/filter/timeFrame.go | 20 ++++++++++++++++++- internal/filter/timeFrame_test.go | 32 ++++++++++++++++++++++++++++--- 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/internal/filter/timeFrame.go b/internal/filter/timeFrame.go index 5afc2a1..a00699c 100644 --- a/internal/filter/timeFrame.go +++ b/internal/filter/timeFrame.go @@ -14,9 +14,27 @@ func (a TimeFrameEvents) Name() string { } func (a TimeFrameEvents) Filter(event models.Event) bool { - // If the user enters invalid hours, such as numbers higher than 24 or lower than 0, all events will be synchronized up to that point in time. For example, if the start hour is -1 and the end hour is 17, all events until 17 o'clock will be synchronized. + + // if start time is inside the timeframe + // example: event from 10-12, timeframe is 8-18 + // starttime and endtime are inside the timeframe if event.StartTime.Hour() >= a.HourStart && event.StartTime.Hour() <= a.HourEnd { return true } + + // if the endtime is inside the timeframe + // example: event from 7-9, timeframe is 8-18 + // then the endtime of the event is inside the timeframe and therefore should be kept + if event.EndTime.Hour() <= a.HourEnd && event.EndTime.Hour() >= a.HourStart { + return true + } + + // if the starttime is inside the timeframe + // example: event from 17-19 timeframe is 8-18 + // then the starttime of the event is inside the timeframe and therefore should be kept + if event.StartTime.Hour() >= a.HourStart && event.StartTime.Hour() <= a.HourEnd { + return true + } + return false } diff --git a/internal/filter/timeFrame_test.go b/internal/filter/timeFrame_test.go index ffc4be9..bf4f249 100644 --- a/internal/filter/timeFrame_test.go +++ b/internal/filter/timeFrame_test.go @@ -22,8 +22,13 @@ func TestTimeFrameEventsFilter(t *testing.T) { t.Error(err) } + t3, err := time.Parse(timeFormat, "2024-01-01T07:00") + if err != nil { + t.Error(err) + } + sourceEvents := []models.Event{ - // Should be kept + // Should be kept, as Start AND Endtime is inside the timeframe { ICalUID: "testId", ID: "testUid", @@ -31,8 +36,9 @@ func TestTimeFrameEventsFilter(t *testing.T) { Description: "bar", AllDay: true, StartTime: t1, + EndTime: t1.Add(time.Hour * 1), }, - // Should be filtered + // Should be filtered, as the Start and EndTime is out of range { ICalUID: "testId2", ID: "testUid2", @@ -40,6 +46,7 @@ func TestTimeFrameEventsFilter(t *testing.T) { Description: "bar", AllDay: false, StartTime: t2, + EndTime: t2.Add(time.Hour * 1), }, // Should be kept { @@ -48,10 +55,29 @@ func TestTimeFrameEventsFilter(t *testing.T) { Title: "foo", Description: "bar", StartTime: t1, + EndTime: t1.Add(time.Hour * 1), + }, + // Should be kept, as the end time is part of the timeframe + { + ICalUID: "testId4", + ID: "testUid3", + Title: "foo", + Description: "bar", + StartTime: t3, + EndTime: t3.Add(time.Hour * 1), + }, + // Should be kept, as the start time is part of the timeframe + { + ICalUID: "testId5", + ID: "testUid4", + Title: "foo", + Description: "bar", + StartTime: t1, + EndTime: t1.Add(time.Hour * 7), }, } - expectedSinkEvents := []models.Event{sourceEvents[0], sourceEvents[2]} + expectedSinkEvents := []models.Event{sourceEvents[0], sourceEvents[2], sourceEvents[3], sourceEvents[4]} eventFilter := filter.TimeFrameEvents{ // Events outside 8 am and 5 pm will be filtered. From 29b18db4f50f0d4a6ee1bd38fb671a623d327e6d Mon Sep 17 00:00:00 2001 From: Alexander Huck Date: Sat, 18 May 2024 11:53:27 +0200 Subject: [PATCH 9/9] fix: use int if the size allows it --- internal/sync/util.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/internal/sync/util.go b/internal/sync/util.go index 132795a..470f41b 100644 --- a/internal/sync/util.go +++ b/internal/sync/util.go @@ -20,7 +20,11 @@ func autoConfigure(object any, config config.CustomMap) { reflect.Int16, reflect.Int32, reflect.Int64: - field.SetInt(int64(value.(int))) + if v, ok := value.(int); ok { + field.SetInt(int64(v)) + } else { + field.SetInt(value.(int64)) + } case reflect.Bool: field.SetBool(value.(bool)) case reflect.String: