From dc4f883fc62dd266579b73b46e1e49b936291fd5 Mon Sep 17 00:00:00 2001 From: Quinn Klassen Date: Tue, 21 Feb 2023 09:17:18 -0800 Subject: [PATCH] Use sensible defaults for ScheduleCalendarSpec (#1047) --- internal/internal_schedule_client.go | 28 +++++++++++++++ internal/schedule_client.go | 15 +++++++- test/integration_test.go | 51 ++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+), 1 deletion(-) diff --git a/internal/internal_schedule_client.go b/internal/internal_schedule_client.go index b22fe72d9..f2dc14d7f 100644 --- a/internal/internal_schedule_client.go +++ b/internal/internal_schedule_client.go @@ -719,9 +719,37 @@ func convertFromPBScheduleCalendarSpecList(calendarSpecPB []*schedulepb.Structur return calendarSpec } +func applyScheduleCalendarSpecDefault(calendarSpec *ScheduleCalendarSpec) { + if calendarSpec.Second == nil { + calendarSpec.Second = []ScheduleRange{{Start: 0}} + } + + if calendarSpec.Minute == nil { + calendarSpec.Minute = []ScheduleRange{{Start: 0}} + } + + if calendarSpec.Hour == nil { + calendarSpec.Hour = []ScheduleRange{{Start: 0}} + } + + if calendarSpec.DayOfMonth == nil { + calendarSpec.DayOfMonth = []ScheduleRange{{Start: 1, End: 31}} + } + + if calendarSpec.Month == nil { + calendarSpec.Month = []ScheduleRange{{Start: 1, End: 12}} + } + + if calendarSpec.DayOfWeek == nil { + calendarSpec.DayOfWeek = []ScheduleRange{{Start: 0, End: 6}} + } +} + func convertToPBScheduleCalendarSpecList(calendarSpec []ScheduleCalendarSpec) []*schedulepb.StructuredCalendarSpec { calendarSpecPB := make([]*schedulepb.StructuredCalendarSpec, len(calendarSpec)) for i, e := range calendarSpec { + applyScheduleCalendarSpecDefault(&e) + calendarSpecPB[i] = &schedulepb.StructuredCalendarSpec{ Second: convertToPBRangeList(e.Second), Minute: convertToPBRangeList(e.Minute), diff --git a/internal/schedule_client.go b/internal/schedule_client.go index 2c1961418..5a2282477 100644 --- a/internal/schedule_client.go +++ b/internal/schedule_client.go @@ -54,25 +54,38 @@ type ( // that means all years match. For all fields besides year, at least one Range must be present to match anything. ScheduleCalendarSpec struct { // Second range to match (0-59). + // + // default: matches 0 Second []ScheduleRange // Minute range to match (0-59). + // + // default: matches 0 Minute []ScheduleRange // Hour range to match (0-23). + // + // default: matches 0 Hour []ScheduleRange // DayOfMonth range to match (1-31) + // + // default: matches all days DayOfMonth []ScheduleRange // Month range to match (1-12) + // + // default: matches all months Month []ScheduleRange // Year range to match. - // Optional: Defaulted to "*" + // + // default: empty that matches all years Year []ScheduleRange // DayOfWeek range to match (0-6; 0 is Sunday) + // + // default: matches all days of the week DayOfWeek []ScheduleRange // Comment - Description of the intention of this schedule. diff --git a/test/integration_test.go b/test/integration_test.go index 9705dfde7..b8913a9d9 100644 --- a/test/integration_test.go +++ b/test/integration_test.go @@ -2879,6 +2879,57 @@ func (ts *IntegrationTestSuite) TestScheduleCreate() { ts.Nil(description) } +func (ts *IntegrationTestSuite) TestScheduleCalendarDefault() { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + handle, err := ts.client.ScheduleClient().Create(ctx, client.ScheduleOptions{ + ID: "test-schedule-calendar-default-schedule", + Spec: client.ScheduleSpec{ + Calendars: []client.ScheduleCalendarSpec{ + { + Second: []client.ScheduleRange{{Start: 30, End: 30}}, + }, + }, + }, + Action: ts.createBasicScheduleWorkflowAction("test-schedule-calendar-default-workflow"), + Paused: true, + }) + ts.NoError(err) + ts.EqualValues("test-schedule-calendar-default-schedule", handle.GetID()) + defer func() { + ts.NoError(handle.Delete(ctx)) + }() + description, err := handle.Describe(ctx) + ts.NoError(err) + // test default calendar spec + ts.Equal([]client.ScheduleCalendarSpec{ + { + Second: []client.ScheduleRange{{Start: 30, End: 30}}, + Minute: []client.ScheduleRange{{}}, + Hour: []client.ScheduleRange{{}}, + DayOfMonth: []client.ScheduleRange{ + { + Start: 1, + End: 31, + }, + }, + Month: []client.ScheduleRange{ + { + Start: 1, + End: 12, + }, + }, + Year: []client.ScheduleRange{}, + DayOfWeek: []client.ScheduleRange{ + { + Start: 0, + End: 6, + }, + }, + }, + }, description.Schedule.Spec.Calendars) +} + func (ts *IntegrationTestSuite) TestScheduleCreateDuplicate() { ctx, cancel := context.WithCancel(context.Background()) defer cancel()