require

package
v0.47.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 13, 2020 License: BSD-2-Clause Imports: 2 Imported by: 2

Documentation

Overview

Package require provides simple constraints to assist with detecting errors in database queries that arise from the wrong number of result (for example no result or too many results).

The errors arising when requirements are not met are Sizer values.

Index

Constants

View Source
const (
	// All is a requirement that is updated to exactly match some number that is only known at
	// call time. The generated code accepts this requirement, then substitues for it the
	// expression 'Exactly(n)', with n being determined automatically. For example, given
	// a slice of primary keys, a select query that should always match all of them can use
	// the length of the slice as 'n'.
	All lateBound = iota
)

Variables

View Source
var AtLeastOne = AtLeast(1)

AtLeastOne is a requirement that is met by the actual results being at least one, i.e. not empty.

View Source
var NoMoreThanOne = NoMoreThan(1)

NoMoreThanOne is a requirement that is met by the actual results being no more than one.

Functions

func ActualResultSize added in v0.9.0

func ActualResultSize(err error) (int64, bool)

ActualResultSize tests the error and returns true only if the error is a wrong-size error, in which case it also returns the actual result size.

func ChainErrorIfExecNotSatisfiedBy

func ChainErrorIfExecNotSatisfiedBy(err error, r Requirement, actual int64) error

ChainErrorIfExecNotSatisfiedBy matches a requirement against the actual result size for an exec query. The requirement may be nil in which case there will be no error. This function accepts an existing potential error, passing it on if not nil.

func ChainErrorIfQueryNotSatisfiedBy

func ChainErrorIfQueryNotSatisfiedBy(err error, r Requirement, actual int64) error

ChainErrorIfQueryNotSatisfiedBy matches a requirement against the actual result size for a select query. The requirement may be nil in which case there will be no error. This function accepts an existing potential error, passing it on if not nil.

func ErrWrongSize added in v0.9.0

func ErrWrongSize(actualSize int64, format string, args ...interface{}) error

ErrWrongSize returns an error based on the actual size received and a message describing the unsatisfied requirement. The returned value is both an error and a Sizer.

func ErrorIfExecNotSatisfiedBy

func ErrorIfExecNotSatisfiedBy(r Requirement, actual int64) error

ErrorIfExecNotSatisfiedBy matches a requirement against the actual result size for an exec query. The requirement may be nil in which case there will be no error.

func ErrorIfQueryNotSatisfiedBy

func ErrorIfQueryNotSatisfiedBy(r Requirement, actual int64) error

ErrorIfQueryNotSatisfiedBy matches a requirement against the actual result size for a select query. The requirement may be nil in which case there will be no error.

func IsNotFound added in v0.9.0

func IsNotFound(err error) bool

IsNotFound tests the error and returns true only if the error is a wrong-size error and the actual size less than one.

func IsNotUnique added in v0.9.0

func IsNotUnique(err error) bool

IsNotUnique tests the error and returns true only if the error is a wrong-size error and the actual size was more than one.

Types

type AtLeast

type AtLeast int64

AtLeast is a requirement that is met by the actual results being at least a specified value.

func (AtLeast) String added in v0.9.0

func (n AtLeast) String() string

type Exactly

type Exactly int64

Exactly is a requirement that is met by a number matching exactly.

func (Exactly) String added in v0.9.0

func (n Exactly) String() string

type NoMoreThan

type NoMoreThan int64

NoMoreThan is a requirement that is met by the actual results being no more than a specified value.

func (NoMoreThan) String added in v0.9.0

func (n NoMoreThan) String() string

type Quantifier

type Quantifier int64

Quantifier is a requirement that is met by imprecise zero, singular or plural results. The value All will be automatically updated to match exactly some number known at call time.

const (
	None Quantifier = iota
	One
	Many
)

func (Quantifier) String

func (q Quantifier) String() string

type Requirement

type Requirement interface {
	String() string
	// contains filtered or unexported methods
}

Requirement set an expectation on the outcome of a query.

type Sizer added in v0.9.0

type Sizer interface {
	Size() int64
}

Sizer is any type that provides the result set size. This is used here for errors where the actual size did not meet the requirements.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL