unified period

This commit is contained in:
Erik Winter 2024-12-06 07:50:33 +01:00
parent 4bb7640e84
commit abf3d7291d
5 changed files with 24 additions and 20 deletions

View File

@ -103,7 +103,7 @@ func (e Event) Valid() bool {
if e.Duration.Seconds() < 1 { if e.Duration.Seconds() < 1 {
return false return false
} }
if e.Recur != nil && !e.Recur.Valid() { if e.Recurrer != nil && !e.Recurrer.Valid() {
return false return false
} }

View File

@ -1,6 +1,9 @@
package item package item
import "time" import (
"slices"
"time"
)
type RecurPeriod string type RecurPeriod string
@ -9,6 +12,8 @@ const (
PeriodMonth RecurPeriod = "month" PeriodMonth RecurPeriod = "month"
) )
var ValidPeriods = []RecurPeriod{PeriodDay, PeriodMonth}
type Recur struct { type Recur struct {
Start time.Time `json:"start"` Start time.Time `json:"start"`
Period RecurPeriod `json:"period"` 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) 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)
}

View File

@ -3,6 +3,8 @@ package command
import ( import (
"fmt" "fmt"
"time" "time"
"go-mod.ewintr.nl/planner/item"
) )
type ArgSet struct { type ArgSet struct {
@ -62,14 +64,14 @@ func (as *ArgSet) GetDuration(name string) time.Duration {
return val return val
} }
func (as *ArgSet) GetPeriod(name string) Period { func (as *ArgSet) GetRecurPeriod(name string) item.RecurPeriod {
flag, ok := as.Flags[name] flag, ok := as.Flags[name]
if !ok { if !ok {
return Period("") return item.RecurPeriod("")
} }
val, ok := flag.Get().(Period) val, ok := flag.Get().(item.RecurPeriod)
if !ok { if !ok {
return Period("") return item.RecurPeriod("")
} }
return val return val
} }

View File

@ -5,6 +5,8 @@ import (
"fmt" "fmt"
"slices" "slices"
"time" "time"
"go-mod.ewintr.nl/planner/item"
) )
const ( const (
@ -109,25 +111,16 @@ func (fs *FlagDuration) Get() any {
return fs.Value return fs.Value
} }
type Period string
const (
PeriodDay = "day"
PeriodMonth = "month"
)
var validPeriods = []Period{PeriodDay, PeriodMonth}
type FlagPeriod struct { type FlagPeriod struct {
Name string Name string
Value Period Value item.RecurPeriod
} }
func (fp *FlagPeriod) Set(val string) error { 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) return fmt.Errorf("not a valid period: %v", val)
} }
fp.Value = Period(val) fp.Value = item.RecurPeriod(val)
return nil return nil
} }

View File

@ -108,13 +108,13 @@ func (update *Update) do() error {
} }
if as.IsSet(FlagRecStart) || as.IsSet(FlagRecPeriod) { if as.IsSet(FlagRecStart) || as.IsSet(FlagRecPeriod) {
if e.Recurrer == nil { if e.Recurrer == nil {
e.Recurrer = *item.Recur{} e.Recurrer = &item.Recur{}
} }
if as.IsSet(FlagRecStart) { if as.IsSet(FlagRecStart) {
e.Recurrer.Start = as.GetTime(FlagRecStart) e.Recurrer.Start = as.GetTime(FlagRecStart)
} }
if as.IsSet(FlagRecPeriod) { if as.IsSet(FlagRecPeriod) {
e.Recurrer.Period = as.GetPeriod(FlagRecPeriod) e.Recurrer.Period = as.GetRecurPeriod(FlagRecPeriod)
} }
} }