2023-06-29 14:34:06 +03:00
|
|
|
package configmgr
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2024-11-13 15:44:21 +03:00
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
2023-06-29 14:34:06 +03:00
|
|
|
"github.com/AdguardTeam/golibs/timeutil"
|
|
|
|
"golang.org/x/exp/constraints"
|
|
|
|
)
|
|
|
|
|
2024-11-13 15:44:21 +03:00
|
|
|
// validator is the interface for configuration entities that can validate
|
|
|
|
// themselves.
|
|
|
|
type validator interface {
|
|
|
|
// validate returns an error if the entity isn't valid.
|
|
|
|
validate() (err error)
|
|
|
|
}
|
|
|
|
|
2023-06-29 14:34:06 +03:00
|
|
|
// numberOrDuration is the constraint for integer types along with
|
|
|
|
// timeutil.Duration.
|
|
|
|
type numberOrDuration interface {
|
|
|
|
constraints.Integer | timeutil.Duration
|
|
|
|
}
|
|
|
|
|
2024-11-13 15:44:21 +03:00
|
|
|
// newErrNotPositive returns an error about the value that must be positive but
|
|
|
|
// isn't. prop is the name of the property to mention in the error message.
|
2023-06-29 14:34:06 +03:00
|
|
|
//
|
|
|
|
// TODO(a.garipov): Consider moving such helpers to golibs and use in AdGuardDNS
|
|
|
|
// as well.
|
2024-11-13 15:44:21 +03:00
|
|
|
func newErrNotPositive[T numberOrDuration](prop string, v T) (err error) {
|
|
|
|
return fmt.Errorf("%s: %w, got %v", prop, errors.ErrNotPositive, v)
|
2023-06-29 14:34:06 +03:00
|
|
|
}
|