Skip to content

Commit 57a6b46

Browse files
authored
Replace uses of valutil.FirstNonZero with cmp.Or + deprecate former (#845)
`cmp.Or` wasn't available originally in River, but was add in 1.22. We're now minimum version 1.23, so it's fine to switch over it given it does exactly the same thing as the custom `valutil.FirstNonZero`. We have some dependencies using `valutil.FirstNonZero` so we shouldn't remove it until there's at least one more version of those that've been cut to avoid potential version compatibility problems. For now, mark the function as deprecated to warn against use.
1 parent 7aa1751 commit 57a6b46

3 files changed

Lines changed: 9 additions & 5 deletions

File tree

client.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package river
22

33
import (
4+
"cmp"
45
"context"
56
"encoding/json"
67
"errors"
@@ -1323,9 +1324,9 @@ func insertParamsFromConfigArgsAndOptions(archetype *baseservice.Archetype, conf
13231324
// by drivers as necessary.
13241325
createdAt := archetype.Time.NowUTCOrNil()
13251326

1326-
maxAttempts := valutil.FirstNonZero(insertOpts.MaxAttempts, jobInsertOpts.MaxAttempts, config.MaxAttempts)
1327-
priority := valutil.FirstNonZero(insertOpts.Priority, jobInsertOpts.Priority, rivercommon.PriorityDefault)
1328-
queue := valutil.FirstNonZero(insertOpts.Queue, jobInsertOpts.Queue, rivercommon.QueueDefault)
1327+
maxAttempts := cmp.Or(insertOpts.MaxAttempts, jobInsertOpts.MaxAttempts, config.MaxAttempts)
1328+
priority := cmp.Or(insertOpts.Priority, jobInsertOpts.Priority, rivercommon.PriorityDefault)
1329+
queue := cmp.Or(insertOpts.Queue, jobInsertOpts.Queue, rivercommon.QueueDefault)
13291330

13301331
if err := validateQueueName(queue); err != nil {
13311332
return nil, err

internal/jobexecutor/job_executor.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package jobexecutor
22

33
import (
4+
"cmp"
45
"context"
56
"encoding/json"
67
"errors"
@@ -20,7 +21,6 @@ import (
2021
"github.com/riverqueue/river/internal/workunit"
2122
"github.com/riverqueue/river/riverdriver"
2223
"github.com/riverqueue/river/rivershared/baseservice"
23-
"github.com/riverqueue/river/rivershared/util/valutil"
2424
"github.com/riverqueue/river/rivertype"
2525
)
2626

@@ -203,7 +203,7 @@ func (e *JobExecutor) execute(ctx context.Context) (res *jobExecutorResult) {
203203
return err
204204
}
205205

206-
jobTimeout := valutil.FirstNonZero(e.WorkUnit.Timeout(), e.ClientJobTimeout)
206+
jobTimeout := cmp.Or(e.WorkUnit.Timeout(), e.ClientJobTimeout)
207207
ctx, cancel := execution.MaybeApplyTimeout(ctx, jobTimeout)
208208
defer cancel()
209209

rivershared/util/valutil/val_util.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ func ValOrDefaultFunc[T comparable](val T, defaultFunc func() T) T {
2222

2323
// FirstNonZero returns the first argument that is non-zero, or the zero value if
2424
// all are zero.
25+
//
26+
// Deprecated: Use `cmp.Or` instead. This function will be removed in a near
27+
// future version.
2528
func FirstNonZero[T comparable](values ...T) T {
2629
var zero T
2730
for _, val := range values {

0 commit comments

Comments
 (0)