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 {
return false
}
if e.Recur != nil && !e.Recur.Valid() {
if e.Recurrer != nil && !e.Recurrer.Valid() {
return false
}

View File

@ -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)
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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)
}
}