Skip to content

Commit

Permalink
Use sensible defaults for ScheduleCalendarSpec (#1047)
Browse files Browse the repository at this point in the history
  • Loading branch information
Quinn-With-Two-Ns authored Feb 21, 2023
1 parent 0b0234f commit dc4f883
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 1 deletion.
28 changes: 28 additions & 0 deletions internal/internal_schedule_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
15 changes: 14 additions & 1 deletion internal/schedule_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
51 changes: 51 additions & 0 deletions test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit dc4f883

Please sign in to comment.