storj/satellite/metabase/util.go
Egon Elbre 5a56021bca satellite/metabase: add intLimitRange
We have quite a bit code duplication in ensuring valid int ranges.

Change-Id: Ib31db8c4b2be6a677625cab5bcdddefb60401fe3
2021-07-19 07:51:16 +00:00

34 lines
751 B
Go

// Copyright (C) 2020 Storj Labs, Inc.
// See LICENSE for copying information.
package metabase
import (
"github.com/zeebo/errs"
"storj.io/private/tagsql"
)
func withRows(rows tagsql.Rows, err error) func(func(tagsql.Rows) error) error {
return func(callback func(tagsql.Rows) error) error {
if err != nil {
return err
}
err := callback(rows)
return errs.Combine(rows.Err(), rows.Close(), err)
}
}
// intLimitRange defines a valid range (1,limit].
type intLimitRange int
// Ensure clamps v to a value between [1,limit].
func (limit intLimitRange) Ensure(v *int) {
if *v <= 0 || *v > int(limit) {
*v = int(limit)
}
}
// Max returns maximum value for the given range.
func (limit intLimitRange) Max() int { return int(limit) }