From abf3d7291df561fa2de6f4210e1a939516e39324 Mon Sep 17 00:00:00 2001 From: Erik Winter Date: Fri, 6 Dec 2024 07:50:33 +0100 Subject: [PATCH] unified period --- item/event.go | 2 +- item/recur.go | 11 ++++++++++- plan/command/argset.go | 10 ++++++---- plan/command/flag.go | 17 +++++------------ plan/command/update.go | 4 ++-- 5 files changed, 24 insertions(+), 20 deletions(-) diff --git a/item/event.go b/item/event.go index 9ca7a47..0b712b8 100644 --- a/item/event.go +++ b/item/event.go @@ -103,7 +103,7 @@ func (e Event) Valid() bool { if e.Duration.Seconds() < 1 { return false } - if e.Recur != nil && !e.Recur.Valid() { + if e.Recurrer != nil && !e.Recurrer.Valid() { return false } diff --git a/item/recur.go b/item/recur.go index 0df33de..02b371b 100644 --- a/item/recur.go +++ b/item/recur.go @@ -1,6 +1,9 @@ package item -import "time" +import ( + "slices" + "time" +) type RecurPeriod string @@ -9,6 +12,8 @@ const ( PeriodMonth RecurPeriod = "month" ) +var ValidPeriods = []RecurPeriod{PeriodDay, PeriodMonth} + type Recur struct { Start time.Time `json:"start"` Period RecurPeriod `json:"period"` @@ -63,3 +68,7 @@ func (r *Recur) onMonths(date time.Time) bool { tDate = time.Date(y, m+time.Month(r.Count), d, 0, 0, 0, 0, time.UTC) } } + +func (r *Recur) Valid() bool { + return r.Start.IsZero() || !slices.Contains(ValidPeriods, r.Period) +} diff --git a/plan/command/argset.go b/plan/command/argset.go index 7e28c81..0667e34 100644 --- a/plan/command/argset.go +++ b/plan/command/argset.go @@ -3,6 +3,8 @@ package command import ( "fmt" "time" + + "go-mod.ewintr.nl/planner/item" ) type ArgSet struct { @@ -62,14 +64,14 @@ func (as *ArgSet) GetDuration(name string) time.Duration { return val } -func (as *ArgSet) GetPeriod(name string) Period { +func (as *ArgSet) GetRecurPeriod(name string) item.RecurPeriod { flag, ok := as.Flags[name] if !ok { - return Period("") + return item.RecurPeriod("") } - val, ok := flag.Get().(Period) + val, ok := flag.Get().(item.RecurPeriod) if !ok { - return Period("") + return item.RecurPeriod("") } return val } diff --git a/plan/command/flag.go b/plan/command/flag.go index a85d79f..d50f376 100644 --- a/plan/command/flag.go +++ b/plan/command/flag.go @@ -5,6 +5,8 @@ import ( "fmt" "slices" "time" + + "go-mod.ewintr.nl/planner/item" ) const ( @@ -109,25 +111,16 @@ func (fs *FlagDuration) Get() any { return fs.Value } -type Period string - -const ( - PeriodDay = "day" - PeriodMonth = "month" -) - -var validPeriods = []Period{PeriodDay, PeriodMonth} - type FlagPeriod struct { Name string - Value Period + Value item.RecurPeriod } func (fp *FlagPeriod) Set(val string) error { - if !slices.Contains(validPeriods, Period(val)) { + if !slices.Contains(item.ValidPeriods, item.RecurPeriod(val)) { return fmt.Errorf("not a valid period: %v", val) } - fp.Value = Period(val) + fp.Value = item.RecurPeriod(val) return nil } diff --git a/plan/command/update.go b/plan/command/update.go index 73fc2d2..e958346 100644 --- a/plan/command/update.go +++ b/plan/command/update.go @@ -108,13 +108,13 @@ func (update *Update) do() error { } if as.IsSet(FlagRecStart) || as.IsSet(FlagRecPeriod) { if e.Recurrer == nil { - e.Recurrer = *item.Recur{} + e.Recurrer = &item.Recur{} } if as.IsSet(FlagRecStart) { e.Recurrer.Start = as.GetTime(FlagRecStart) } if as.IsSet(FlagRecPeriod) { - e.Recurrer.Period = as.GetPeriod(FlagRecPeriod) + e.Recurrer.Period = as.GetRecurPeriod(FlagRecPeriod) } }