From bb8b33a2892fbd8885d39610f0946fc13f953abe Mon Sep 17 00:00:00 2001 From: Christoph Anderson <37236531+lupusA@users.noreply.github.com> Date: Fri, 22 Dec 2023 03:12:48 +0100 Subject: [PATCH] fix(general): Delete duplicates in time of day array within policies (#3484) Test in #3535 --- snapshot/policy/scheduling_policy.go | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/snapshot/policy/scheduling_policy.go b/snapshot/policy/scheduling_policy.go index ff83a1baf..4870cf714 100644 --- a/snapshot/policy/scheduling_policy.go +++ b/snapshot/policy/scheduling_policy.go @@ -1,10 +1,11 @@ package policy import ( + "cmp" "context" "fmt" "reflect" - "sort" + "slices" "strings" "time" @@ -45,14 +46,17 @@ func (t TimeOfDay) String() string { // SortAndDedupeTimesOfDay sorts the slice of times of day and removes duplicates. func SortAndDedupeTimesOfDay(tod []TimeOfDay) []TimeOfDay { - sort.Slice(tod, func(i, j int) bool { - if a, b := tod[i].Hour, tod[j].Hour; a != b { - return a < b + slices.SortFunc(tod, func(a, b TimeOfDay) int { + if n := cmp.Compare(a.Hour, b.Hour); n != 0 { + return n } - return tod[i].Minute < tod[j].Minute + + // If hours are equal sort by minute + return cmp.Compare(a.Minute, b.Minute) }) - return tod + // Remove subsequent duplicates + return slices.Compact[[]TimeOfDay, TimeOfDay](tod) } // SchedulingPolicy describes policy for scheduling snapshots.