Skip to content

Commit ae9a14e

Browse files
committed
Use pointers for optional fields
Also: - Refactor how exposed type is converted to proto type - Remove unnecesary log - Input validation for job api calls Signed-off-by: Albert Callarisa <albert@diagrid.io>
1 parent f6d10d3 commit ae9a14e

File tree

4 files changed

+71
-64
lines changed

4 files changed

+71
-64
lines changed

client/jobs.go

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@ package client
1515

1616
import (
1717
"context"
18-
"log"
18+
"errors"
1919
"time"
2020

2121
"google.golang.org/protobuf/types/known/anypb"
22-
"google.golang.org/protobuf/types/known/durationpb"
2322

2423
commonpb "github.com/dapr/dapr/pkg/proto/common/v1"
2524
runtimepb "github.com/dapr/dapr/pkg/proto/runtime/v1"
@@ -35,18 +34,18 @@ type JobFailurePolicyConstant struct {
3534
}
3635

3736
func (f *JobFailurePolicyConstant) GetPBFailurePolicy() *commonpb.JobFailurePolicy {
38-
policy := &commonpb.JobFailurePolicy{
39-
Policy: &commonpb.JobFailurePolicy_Constant{
40-
Constant: &commonpb.JobFailurePolicyConstant{},
41-
},
42-
}
37+
constantfp := &commonpb.JobFailurePolicyConstant{}
4338
if f.maxRetries != nil {
44-
policy.Policy.(*commonpb.JobFailurePolicy_Constant).Constant.MaxRetries = f.maxRetries
39+
constantfp.MaxRetries = f.maxRetries
4540
}
4641
if f.interval != nil {
47-
policy.Policy.(*commonpb.JobFailurePolicy_Constant).Constant.Interval = &durationpb.Duration{Seconds: int64(f.interval.Seconds())}
42+
constantfp.Interval = toProtoDuration(*f.interval)
43+
}
44+
return &commonpb.JobFailurePolicy{
45+
Policy: &commonpb.JobFailurePolicy_Constant{
46+
Constant: constantfp,
47+
},
4848
}
49-
return policy
5049
}
5150

5251
type JobFailurePolicyDrop struct {
@@ -73,36 +72,30 @@ func NewFailurePolicyDrop() FailurePolicy {
7372

7473
type Job struct {
7574
Name string
76-
Schedule string // Optional
77-
Repeats uint32 // Optional
78-
DueTime string // Optional
79-
TTL string // Optional
75+
Schedule *string
76+
Repeats *uint32
77+
DueTime *string
78+
TTL *string
8079
Data *anypb.Any
8180
FailurePolicy FailurePolicy
8281
}
8382

8483
// ScheduleJobAlpha1 raises and schedules a job.
8584
func (c *GRPCClient) ScheduleJobAlpha1(ctx context.Context, job *Job) error {
86-
// TODO: Assert job fields are defined: Name, Data
87-
jobRequest := &runtimepb.Job{
88-
Name: job.Name,
89-
Data: job.Data,
90-
}
91-
92-
if job.Schedule != "" {
93-
jobRequest.Schedule = &job.Schedule
85+
if job.Name == "" {
86+
return errors.New("job name is required")
9487
}
95-
96-
if job.Repeats != 0 {
97-
jobRequest.Repeats = &job.Repeats
88+
if job.Data == nil {
89+
return errors.New("job data is required")
9890
}
9991

100-
if job.DueTime != "" {
101-
jobRequest.DueTime = &job.DueTime
102-
}
103-
104-
if job.TTL != "" {
105-
jobRequest.Ttl = &job.TTL
92+
jobRequest := &runtimepb.Job{
93+
Name: job.Name,
94+
Data: job.Data,
95+
Schedule: job.Schedule,
96+
Repeats: job.Repeats,
97+
DueTime: job.DueTime,
98+
Ttl: job.TTL,
10699
}
107100

108101
if job.FailurePolicy != nil {
@@ -116,11 +109,13 @@ func (c *GRPCClient) ScheduleJobAlpha1(ctx context.Context, job *Job) error {
116109

117110
// GetJobAlpha1 retrieves a scheduled job.
118111
func (c *GRPCClient) GetJobAlpha1(ctx context.Context, name string) (*Job, error) {
119-
// TODO: Name validation
112+
if name == "" {
113+
return nil, errors.New("job name is required")
114+
}
115+
120116
resp, err := c.protoClient.GetJobAlpha1(ctx, &runtimepb.GetJobRequest{
121117
Name: name,
122118
})
123-
log.Println(resp)
124119
if err != nil {
125120
return nil, err
126121
}
@@ -139,18 +134,21 @@ func (c *GRPCClient) GetJobAlpha1(ctx context.Context, name string) (*Job, error
139134

140135
return &Job{
141136
Name: resp.GetJob().GetName(),
142-
Schedule: resp.GetJob().GetSchedule(),
143-
Repeats: resp.GetJob().GetRepeats(),
144-
DueTime: resp.GetJob().GetDueTime(),
145-
TTL: resp.GetJob().GetTtl(),
137+
Schedule: resp.GetJob().Schedule,
138+
Repeats: resp.GetJob().Repeats,
139+
DueTime: resp.GetJob().DueTime,
140+
TTL: resp.GetJob().Ttl,
146141
Data: resp.GetJob().GetData(),
147142
FailurePolicy: failurePolicy,
148143
}, nil
149144
}
150145

151146
// DeleteJobAlpha1 deletes a scheduled job.
152147
func (c *GRPCClient) DeleteJobAlpha1(ctx context.Context, name string) error {
153-
// TODO: Name validation
148+
if name == "" {
149+
return errors.New("job name is required")
150+
}
151+
154152
_, err := c.protoClient.DeleteJobAlpha1(ctx, &runtimepb.DeleteJobRequest{
155153
Name: name,
156154
})

client/jobs_test.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ func TestSchedulingAlpha1(t *testing.T) {
2626
ctx := t.Context()
2727

2828
t.Run("schedule job - valid", func(t *testing.T) {
29+
schedule := "test"
2930
err := testClient.ScheduleJobAlpha1(ctx, &Job{
3031
Name: "test",
31-
Schedule: "test",
32+
Schedule: &schedule,
3233
Data: &anypb.Any{},
3334
FailurePolicy: NewFailurePolicyConstant(nil, nil),
3435
})
@@ -40,12 +41,17 @@ func TestSchedulingAlpha1(t *testing.T) {
4041
maxRetries := uint32(4)
4142
interval := time.Second * 10
4243

44+
schedule := "@every 10s"
45+
repeats := uint32(4)
46+
dueTime := "10s"
47+
ttl := "10s"
48+
4349
expected := &Job{
4450
Name: "name",
45-
Schedule: "@every 10s",
46-
Repeats: 4,
47-
DueTime: "10s",
48-
TTL: "10s",
51+
Schedule: &schedule,
52+
Repeats: &repeats,
53+
DueTime: &dueTime,
54+
TTL: &ttl,
4955
Data: nil,
5056
FailurePolicy: NewFailurePolicyConstant(&maxRetries, &interval),
5157
}

client/state.go

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@ import (
1717
"context"
1818
"errors"
1919
"fmt"
20-
"math"
21-
"time"
22-
23-
"google.golang.org/protobuf/types/known/durationpb"
2420

2521
v1 "github.com/dapr/dapr/pkg/proto/common/v1"
2622
pb "github.com/dapr/dapr/pkg/proto/runtime/v1"
@@ -249,22 +245,6 @@ func copyStateOptionDefault() *StateOptions {
249245
}
250246
}
251247

252-
func toProtoDuration(d time.Duration) *durationpb.Duration {
253-
nanos := d.Nanoseconds()
254-
secs := nanos / 1e9
255-
nanos -= secs * 1e9
256-
257-
// conversion check - gosec ignored below for conversion
258-
if nanos <= int64(math.MinInt32) && nanos >= int64(math.MaxInt32) {
259-
panic("integer overflow converting duration to proto")
260-
}
261-
262-
return &durationpb.Duration{
263-
Seconds: secs,
264-
Nanos: int32(nanos), //nolint:gosec
265-
}
266-
}
267-
268248
// ExecuteStateTransaction provides way to execute multiple operations on a specified store.
269249
func (c *GRPCClient) ExecuteStateTransaction(ctx context.Context, storeName string, meta map[string]string, ops []*StateOperation) error {
270250
if storeName == "" {

client/utils.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@ limitations under the License.
1313

1414
package client
1515

16-
import "encoding/json"
16+
import (
17+
"encoding/json"
18+
"math"
19+
"time"
20+
21+
"google.golang.org/protobuf/types/known/durationpb"
22+
)
1723

1824
// isCloudEvent returns true if the event is a CloudEvent.
1925
// An event is a CloudEvent if it `id`, `source`, `specversion` and `type` fields.
@@ -30,3 +36,20 @@ func isCloudEvent(event []byte) bool {
3036
}
3137
return ce.ID != "" && ce.Source != "" && ce.SpecVersion != "" && ce.Type != ""
3238
}
39+
40+
// toProtoDuration converts a time.Duration to a protobuf duration.
41+
func toProtoDuration(d time.Duration) *durationpb.Duration {
42+
nanos := d.Nanoseconds()
43+
secs := nanos / 1e9
44+
nanos -= secs * 1e9
45+
46+
// conversion check - gosec ignored below for conversion
47+
if nanos <= int64(math.MinInt32) && nanos >= int64(math.MaxInt32) {
48+
panic("integer overflow converting duration to proto")
49+
}
50+
51+
return &durationpb.Duration{
52+
Seconds: secs,
53+
Nanos: int32(nanos), //nolint:gosec
54+
}
55+
}

0 commit comments

Comments
 (0)