Skip to content

Commit

Permalink
normalize with Days of week
Browse files Browse the repository at this point in the history
  • Loading branch information
jh-bate committed Nov 8, 2023
1 parent e8e07ff commit 2e31017
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 61 deletions.
15 changes: 13 additions & 2 deletions data/types/settings/pump/sleep_schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,14 @@ func (s *SleepScheduleMap) Validate(validator structure.Validator) {
}
}

func (s *SleepScheduleMap) Normalize(normalizer data.Normalizer) {}
func (s *SleepScheduleMap) Normalize(normalizer data.Normalizer) {
for _, name := range s.sortedNames() {
datumNormalizer := normalizer.WithReference(name)
if datum := s.Get(name); datum != nil {
datum.Normalize(datumNormalizer)
}
}
}

func (s *SleepScheduleMap) Get(name string) *SleepSchedule {
if datum, exists := (*s)[name]; exists {
Expand Down Expand Up @@ -106,4 +113,8 @@ func (s *SleepSchedule) Validate(validator structure.Validator) {
}
}

func (s *SleepSchedule) Normalize(normalizer data.Normalizer) {}
func (s *SleepSchedule) Normalize(normalizer data.Normalizer) {
if s.Days != nil {
sort.Sort(common.DaysOfWeekByDayIndex(*s.Days))
}
}
51 changes: 9 additions & 42 deletions dexcom/alert_schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"
"time"

dataTypesCommon "github.com/tidepool-org/platform/data/types/common"
dataTypesSettingsCgm "github.com/tidepool-org/platform/data/types/settings/cgm"
"github.com/tidepool-org/platform/errors"
"github.com/tidepool-org/platform/pointer"
Expand All @@ -17,13 +18,13 @@ const (
AlertScheduleSettingsStartTimeDefault = "00:00"
AlertScheduleSettingsEndTimeDefault = "00:00"

AlertScheduleSettingsDaySunday = "sunday"
AlertScheduleSettingsDayMonday = "monday"
AlertScheduleSettingsDayTuesday = "tuesday"
AlertScheduleSettingsDayWednesday = "wednesday"
AlertScheduleSettingsDayThursday = "thursday"
AlertScheduleSettingsDayFriday = "friday"
AlertScheduleSettingsDaySaturday = "saturday"
AlertScheduleSettingsDaySunday = dataTypesCommon.DaySunday
AlertScheduleSettingsDayMonday = dataTypesCommon.DayMonday
AlertScheduleSettingsDayTuesday = dataTypesCommon.DayTuesday
AlertScheduleSettingsDayWednesday = dataTypesCommon.DayWednesday
AlertScheduleSettingsDayThursday = dataTypesCommon.DayThursday
AlertScheduleSettingsDayFriday = dataTypesCommon.DayFriday
AlertScheduleSettingsDaySaturday = dataTypesCommon.DaySaturday

AlertScheduleSettingsOverrideModeUnknown = "unknown"
AlertScheduleSettingsOverrideModeQuiet = "quiet"
Expand Down Expand Up @@ -95,27 +96,6 @@ func AlertScheduleSettingsOverrideModes() []string {
}
}

func AlertScheduleSettingsDayIndex(day string) int {
switch day {
case AlertScheduleSettingsDaySunday:
return 1
case AlertScheduleSettingsDayMonday:
return 2
case AlertScheduleSettingsDayTuesday:
return 3
case AlertScheduleSettingsDayWednesday:
return 4
case AlertScheduleSettingsDayThursday:
return 5
case AlertScheduleSettingsDayFriday:
return 6
case AlertScheduleSettingsDaySaturday:
return 7
default:
return 0
}
}

func AlertSettingAlertNames() []string {
return []string{
AlertSettingAlertNameUnknown,
Expand Down Expand Up @@ -410,27 +390,14 @@ func (a *AlertScheduleSettings) Validate(validator structure.Validator) {

func (a *AlertScheduleSettings) Normalize(normalizer structure.Normalizer) {
if a.DaysOfWeek != nil {
sort.Sort(DaysOfWeekByAlertScheduleSettingsDayIndex(*a.DaysOfWeek))
sort.Sort(dataTypesCommon.DaysOfWeekByDayIndex(*a.DaysOfWeek))
}
}

func (a *AlertScheduleSettings) IsDefault() bool {
return a.Default != nil && *a.Default
}

type DaysOfWeekByAlertScheduleSettingsDayIndex []string

func (d DaysOfWeekByAlertScheduleSettingsDayIndex) Len() int {
return len(d)
}
func (d DaysOfWeekByAlertScheduleSettingsDayIndex) Swap(i int, j int) {
d[i], d[j] = d[j], d[i]
}

func (d DaysOfWeekByAlertScheduleSettingsDayIndex) Less(i int, j int) bool {
return AlertScheduleSettingsDayIndex(d[i]) < AlertScheduleSettingsDayIndex(d[j])
}

type AlertSettings []*AlertSetting

func ParseAlertSettings(parser structure.ArrayParser) *AlertSettings {
Expand Down
17 changes: 0 additions & 17 deletions dexcom/alert_schedule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,23 +120,6 @@ var _ = Describe("Alert", func() {
}))
})

Context("AlertScheduleSettingsDayIndex", func() {
DescribeTable("return the expected index when the day",
func(day string, expectedIndex int) {
Expect(dexcom.AlertScheduleSettingsDayIndex(day)).To(Equal(expectedIndex))
},
Entry("is an empty string", "", 0),
Entry("is sunday", "sunday", 1),
Entry("is monday", "monday", 2),
Entry("is tuesday", "tuesday", 3),
Entry("is wednesday", "wednesday", 4),
Entry("is thursday", "thursday", 5),
Entry("is friday", "friday", 6),
Entry("is saturday", "saturday", 7),
Entry("is an invalid string", "invalid", 0),
)
})

It("AlertSettingAlertNames returns expected", func() {
Expect(dexcom.AlertSettingAlertNames()).To(Equal([]string{"unknown", "fall", "high", "low", "noReadings", "outOfRange", "rise", "urgentLow", "urgentLowSoon", "fixedLow"}))
Expect(dexcom.AlertSettingAlertNames()).To(Equal([]string{
Expand Down

0 comments on commit 2e31017

Please sign in to comment.