check

package
v1.6.17 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2022 License: MIT Imports: 7 Imported by: 17

Documentation

Overview

Package check provides a collection of atomic checks that can be applied to values.

Each of the check functions returns an error if the value doesn't pass the check or nil if the check is passed. For instance a function of type check.X will typically take a variable of type X and return an error if the value does not pass the check. Where a check is parameterised there is typically a function which returns a check function as a closure.

The checks are typically very simple to the point where you might question why not perform the check directly. The reason is that as functions they can be composed and combined and then passed to other code to be called later. They are used extensively for checking command line parameters.

Many of the types have a ...Not function that can be used to invert the meaning of a check. Similarly, there are ...And and ...Or functions which can be used to compose checks.

Example

Example demonstrates how check functions might be used. It sets up two collections of checks on a slice of strings, the first collection should all pass (return a nil error) and the second set should all fail. Note that the check functions called below each returns a function of type check.StringSlice. For instance check.StringSliceLenEQ(2) returns a check.StringSlice function that will check that the given slice is of length 2. This technique is used throughout the package.

package main

import (
	"fmt"

	"github.com/nickwells/check.mod/check"
)

func main() {
	s := []string{"hello", "world"}

	passingChecks := []check.StringSlice{
		check.StringSliceLenEQ(2),
		check.StringSliceContains(
			check.StringEquals("hello"),
			"the list of strings must contain 'hello'"),
	}

	for _, c := range passingChecks {
		if err := c(s); err != nil {
			fmt.Println("unexpected error: ", err)
			return
		}
	}
	fmt.Println("All checks expected to pass, passed")

	failingChecks := []check.StringSlice{
		check.StringSliceLenEQ(3),
		check.StringSliceNot(
			check.StringSliceNoDups,
			"the list of strings must contain duplicates"),
	}

	var someCheckPassed bool
	for i, c := range failingChecks {
		if err := c(s); err == nil {
			fmt.Println("unexpected check success: ", i)
			someCheckPassed = true
		}
	}
	if !someCheckPassed {
		fmt.Println("All checks expected to fail, failed")
	}

}
Output:

All checks expected to pass, passed
All checks expected to fail, failed

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func FileInfoIsDir

func FileInfoIsDir(fi os.FileInfo) error

FileInfoIsDir will check that the file info describes a directory

func FileInfoIsRegular

func FileInfoIsRegular(fi os.FileInfo) error

FileInfoIsRegular will check that the file info describes a regular file

func FileInfoOwnedBySelf added in v1.1.0

func FileInfoOwnedBySelf(fi fs.FileInfo) error

FileInfoOwnedBySelf tests that the file is owned by the calling user

func Int64SliceNoDups

func Int64SliceNoDups(v []int64) error

Int64SliceNoDups checks that the list contains no duplicates

func StringOK added in v1.4.0

func StringOK(_ string) error

StringOK always returns a nil error - it can be of use with a StringSliceStringCheckByPos to allow any value in certain slice positions

func StringSliceNoDups

func StringSliceNoDups(v []string) error

StringSliceNoDups checks that the list contains no duplicates

Types

type Duration

type Duration func(d time.Duration) error

Duration is the type of a check function which takes a time.Duration parameter and returns an error or nil if the check passes

func DurationAnd

func DurationAnd(chkFuncs ...Duration) Duration

DurationAnd returns a function that will check that the value, when passed to each of the check funcs in turn, passes all of them

func DurationBetween

func DurationBetween(low, high time.Duration) Duration

DurationBetween returns a function that will check that the value lies between the upper and lower limits (inclusive)

func DurationGE

func DurationGE(limit time.Duration) Duration

DurationGE returns a function that will check that the value is greater than or equal to the limit

func DurationGT

func DurationGT(limit time.Duration) Duration

DurationGT returns a function that will check that the value is greater than the limit

func DurationLE

func DurationLE(limit time.Duration) Duration

DurationLE returns a function that will check that the value is less than or equal to the limit

func DurationLT

func DurationLT(limit time.Duration) Duration

DurationLT returns a function that will check that the value is less than the limit

func DurationNot

func DurationNot(c Duration, errMsg string) Duration

DurationNot returns a function that will check that the value, when passed to the check func, does not pass it. You must also supply the error text to appear after the duration that fails. This error text should be a string that describes the quality that the duration should not have. So, for instance, if the function being Not'ed was

check.DurationGE(5)

then the errMsg parameter should be

"a duration greater than or equal to  5".

func DurationOr

func DurationOr(chkFuncs ...Duration) Duration

DurationOr returns a function that will check that the value, when passed to each of the check funcs in turn, passes at least one of them

type FileInfo

type FileInfo func(d os.FileInfo) error

FileInfo is the type of a check function which takes an os.FileInfo parameter and returns an error or nil if the check passes

func FileInfoAnd

func FileInfoAnd(chkFuncs ...FileInfo) FileInfo

FileInfoAnd returns a function that will check that the value, when passed to each of the check funcs in turn, passes all of them

func FileInfoGidEQ

func FileInfoGidEQ(gid uint32) FileInfo

FileInfoGidEQ returns a FileInfo (func) that tests that the file is owned by the specified user

func FileInfoModTime

func FileInfoModTime(c Time) FileInfo

FileInfoModTime returns a function that will check that the file modification time passes the test specified by the passed Time check

func FileInfoMode

func FileInfoMode(m os.FileMode) FileInfo

FileInfoMode returns a function that will check that the file mode type matches the value passed. Typically this will be a single value as enumerated in the os package under FileMode but if more than one value is given (and'ed together as a bitmask) then if any of the bits is set this function will return nil if any of the bits is set. This allows you to check for several types at once. If a zero value is passed it will check for a regular file - none of the bits are set.

func FileInfoName

func FileInfoName(c String) FileInfo

FileInfoName returns a function that will check that the file name passes the test specified by the passed String check

func FileInfoNot

func FileInfoNot(c FileInfo, errMsg string) FileInfo

FileInfoNot returns a function that will check that the value, when passed to the check func, does not pass it. You must also supply the error text to appear after the value that fails. This error text should be a string that describes the quality that the file info should not have.

func FileInfoOr

func FileInfoOr(chkFuncs ...FileInfo) FileInfo

FileInfoOr returns a function that will check that the value, when passed to each of the check funcs in turn, passes at least one of them

func FileInfoPerm

func FileInfoPerm(c FilePerm) FileInfo

FileInfoPerm returns a function that will check that the file permissions pass the test specified by the passed FilePerm check

func FileInfoSize

func FileInfoSize(c Int64) FileInfo

FileInfoSize returns a function that will check that the file size passes the test specified by the passed Int64 check

func FileInfoUidEQ

func FileInfoUidEQ(uid uint32) FileInfo

FileInfoUidEQ returns a FileInfo (func) that tests that the file is owned by the specified user

type FilePerm

type FilePerm func(d os.FileMode) error

FilePerm is the type of a check function which takes an os.FileMode parameter and returns an error or nil if the check passes

func FilePermAnd

func FilePermAnd(chkFuncs ...FilePerm) FilePerm

FilePermAnd returns a function that will check that the value, when passed to each of the check funcs in turn, passes all of them

func FilePermEQ

func FilePermEQ(perms os.FileMode) FilePerm

FilePermEQ returns a function that will check that the file permission is set to the value of the perms parameter

func FilePermHasAll

func FilePermHasAll(perms os.FileMode) FilePerm

FilePermHasAll returns a function that will check that the file permission has all of the permissions set in the perms parameter

func FilePermHasNone

func FilePermHasNone(perms os.FileMode) FilePerm

FilePermHasNone returns a function that will check that the file permission has none of the permissions set in the perms parameter

func FilePermNot

func FilePermNot(c FilePerm, errMsg string) FilePerm

FilePermNot returns a function that will check that the value, when passed to the check func, does not pass it. You must also supply the error text to appear after the value that fails. This error text should be a string that describes the quality that the permissions should not have.

func FilePermOr

func FilePermOr(chkFuncs ...FilePerm) FilePerm

FilePermOr returns a function that will check that the value, when passed to each of the check funcs in turn, passes at least one of them

type Float64

type Float64 func(f float64) error

Float64 is the type of a check function for a float64 value. It takes a float64 value and returns an error or nil if the check passes

func Float64And

func Float64And(chkFuncs ...Float64) Float64

Float64And returns a function that will check that the value, when passed to each of the check funcs in turn, passes all of them

func Float64Between

func Float64Between(low, high float64) Float64

Float64Between returns a function that will check that the value lies between the upper and lower limits (inclusive)

func Float64GE

func Float64GE(limit float64) Float64

Float64GE returns a function that will check that the value is greater than or equal to the limit

func Float64GT

func Float64GT(limit float64) Float64

Float64GT returns a function that will check that the value is greater than the limit

func Float64LE

func Float64LE(limit float64) Float64

Float64LE returns a function that will check that the value is less than or equal to the limit

func Float64LT

func Float64LT(limit float64) Float64

Float64LT returns a function that will check that the value is less than the limit

func Float64Not

func Float64Not(c Float64, errMsg string) Float64

Float64Not returns a function that will check that the value, when passed to the check func, does not pass it. You must also supply the error text to appear after the value that fails. This error text should be a string that describes the quality that the value should not have. So, for instance, if the function being Not'ed was

check.Float64GT(5.0)

then the errMsg parameter should be

"greater than 5.0".

func Float64Or

func Float64Or(chkFuncs ...Float64) Float64

Float64Or returns a function that will check that the value, when passed to each of the check funcs in turn, passes at least one of them

type Int64

type Int64 func(i int64) error

Int64 is the type of the check function for int64 values. It takes an int64 parameter and returns an error

func Int64And

func Int64And(chkFuncs ...Int64) Int64

Int64And returns a function that will check that the value, when passed to each of the check funcs in turn, passes all of them

func Int64Between

func Int64Between(low, high int64) Int64

Int64Between returns a function that will check that the value lies between the upper and lower limits (inclusive)

func Int64Divides

func Int64Divides(d int64) Int64

Int64Divides returns a function that will check that the value is a divisor of d

func Int64EQ

func Int64EQ(limit int64) Int64

Int64EQ returns a function that will check that the value is greater than the limit

func Int64GE

func Int64GE(limit int64) Int64

Int64GE returns a function that will check that the value is greater than or equal to the limit

func Int64GT

func Int64GT(limit int64) Int64

Int64GT returns a function that will check that the value is greater than the limit

func Int64IsAMultiple

func Int64IsAMultiple(d int64) Int64

Int64IsAMultiple returns a function that will check that the value is a multiple of d

func Int64LE

func Int64LE(limit int64) Int64

Int64LE returns a function that will check that the value is less than or equal to the limit

func Int64LT

func Int64LT(limit int64) Int64

Int64LT returns a function that will check that the value is less than the limit

func Int64Not

func Int64Not(c Int64, errMsg string) Int64

Int64Not returns a function that will check that the value, when passed to the check func, does not pass it. You must also supply the error text to appear after the value that fails. This error text should be a string that describes the quality that the number should not have. So, for instance, if the function being Not'ed was

check.Int64GT(5)

then the errMsg parameter should be

"a number greater than 5".

func Int64Or

func Int64Or(chkFuncs ...Int64) Int64

Int64Or returns a function that will check that the value, when passed to each of the check funcs in turn, passes at least one of them

type Int64Slice

type Int64Slice func(v []int64) error

Int64Slice is the type of a check function for a slice of int64's. It takes a slice of int64's and returns an error or nil if the check passes

func Int64SliceAnd

func Int64SliceAnd(chkFuncs ...Int64Slice) Int64Slice

Int64SliceAnd returns a function that will check that the value, when passed to each of the check funcs in turn, passes all of them

func Int64SliceInt64Check

func Int64SliceInt64Check(sc Int64) Int64Slice

Int64SliceInt64Check returns a check function that checks that every member of the list matches the supplied Int64 check function

func Int64SliceLenBetween

func Int64SliceLenBetween(low, high int) Int64Slice

Int64SliceLenBetween returns a check function that checks that the length of the list is between the two supplied values (inclusive)

func Int64SliceLenEQ

func Int64SliceLenEQ(i int) Int64Slice

Int64SliceLenEQ returns a check function that checks that the length of the list equals the supplied value

func Int64SliceLenGT

func Int64SliceLenGT(i int) Int64Slice

Int64SliceLenGT returns a check function that checks that the length of the list is greater than the supplied value

func Int64SliceLenLT

func Int64SliceLenLT(i int) Int64Slice

Int64SliceLenLT returns a check function that checks that the length of the list is less than the supplied value

func Int64SliceNot

func Int64SliceNot(c Int64Slice, errMsg string) Int64Slice

Int64SliceNot returns a function that will check that the value, when passed to the check func, does not pass it. You must also supply the error text to appear after the value that fails. This error text should be a string that describes the quality that the slice should not have. So, for instance, if the function being Not'ed was

check.Int64SliceLenGT(5)

then the errMsg parameter should be

"a list with length greater than 5".

func Int64SliceOr

func Int64SliceOr(chkFuncs ...Int64Slice) Int64Slice

Int64SliceOr returns a function that will check that the value, when passed to each of the check funcs in turn, passes at least one of them

type MapStringBool added in v1.5.0

type MapStringBool func(v map[string]bool) error

MapStringBool is the type of a check function for a slice of strings. It takes a slice of strings as a parameter and returns an error or nil if there is no error

func MapStringBoolAnd added in v1.5.0

func MapStringBoolAnd(chkFuncs ...MapStringBool) MapStringBool

MapStringBoolAnd returns a function that will check that the value, when passed to each of the check funcs in turn, passes all of them

func MapStringBoolContains added in v1.5.0

func MapStringBoolContains(sc String, condition string) MapStringBool

MapStringBoolContains returns a check function that checks that at least one entry in the list matches the supplied String check func. The condition parameter should describe the check that is being performed. For instance, if the check is that the string length must be greater than 5 characters then the condition parameter should be "the string should be greater than 5 characters"

func MapStringBoolLenBetween added in v1.5.0

func MapStringBoolLenBetween(low, high int) MapStringBool

MapStringBoolLenBetween returns a check function that checks that the length of the list is between the two supplied values (inclusive)

func MapStringBoolLenEQ added in v1.5.0

func MapStringBoolLenEQ(i int) MapStringBool

MapStringBoolLenEQ returns a check function that checks that the length of the list equals the supplied value

func MapStringBoolLenGT added in v1.5.0

func MapStringBoolLenGT(i int) MapStringBool

MapStringBoolLenGT returns a check function that checks that the length of the list is greater than the supplied value

func MapStringBoolLenLT added in v1.5.0

func MapStringBoolLenLT(i int) MapStringBool

MapStringBoolLenLT returns a check function that checks that the length of the list is less than the supplied value

func MapStringBoolNot added in v1.5.0

func MapStringBoolNot(c MapStringBool, errMsg string) MapStringBool

MapStringBoolNot returns a function that will check that the value, when passed to the check func, does not pass it. You must also supply the error text to appear after the value that fails. This error text should be a string that describes the quality that the slice of strings should not have.

func MapStringBoolOr added in v1.5.0

func MapStringBoolOr(chkFuncs ...MapStringBool) MapStringBool

MapStringBoolOr returns a function that will check that the value, when passed to each of the check funcs in turn, passes at least one of them

func MapStringBoolStringCheck added in v1.5.0

func MapStringBoolStringCheck(sc String) MapStringBool

MapStringBoolStringCheck returns a check function that checks that every key in the map passes the supplied String check func

func MapStringBoolTrueCountBetween added in v1.5.0

func MapStringBoolTrueCountBetween(low, high int) MapStringBool

MapStringBoolTrueCountBetween returns a check function that checks that the number of entries in the map set to true is between the two supplied values (inclusive)

func MapStringBoolTrueCountEQ added in v1.5.0

func MapStringBoolTrueCountEQ(i int) MapStringBool

MapStringBoolTrueCountEQ returns a check function that checks that the number of entries in the map set to true equals the supplied value

func MapStringBoolTrueCountGT added in v1.5.0

func MapStringBoolTrueCountGT(i int) MapStringBool

MapStringBoolTrueCountGT returns a check function that checks that the number of entries in the map set to true is greater than the supplied value

func MapStringBoolTrueCountLT added in v1.5.0

func MapStringBoolTrueCountLT(i int) MapStringBool

MapStringBoolTrueCountLT returns a check function that checks that the number of entries in the map set to true is less than the supplied value

type String

type String func(s string) error

String is the type of a check function for a string. It takes a string as a parameter and returns an error or nil if the check passes

func StringAnd

func StringAnd(chkFuncs ...String) String

StringAnd returns a function that will check that the value, when passed to each of the check funcs in turn, passes all of them

func StringEquals added in v1.2.0

func StringEquals(s string) String

StringEquals returns a function that checks that the string equals the supplied string. This could be useful as one of a list of checks in an StringOr(...)

func StringHasPrefix

func StringHasPrefix(prefix string) String

StringHasPrefix returns a function that checks that the string has the supplied string as a prefix

func StringHasSuffix

func StringHasSuffix(suffix string) String

StringHasSuffix returns a function that checks that the string has the supplied string as a suffix

func StringLenBetween

func StringLenBetween(low, high int) String

StringLenBetween returns a function that will check that the length of the string is between the two limits (inclusive)

func StringLenEQ

func StringLenEQ(limit int) String

StringLenEQ returns a function that will check that the length of the string is equal to the limit

func StringLenGT

func StringLenGT(limit int) String

StringLenGT returns a function that will check that the length of the string is less than the limit

func StringLenLT

func StringLenLT(limit int) String

StringLenLT returns a function that will check that the length of the string is less than the limit

func StringMatchesPattern

func StringMatchesPattern(re *regexp.Regexp, reDesc string) String

StringMatchesPattern returns a function that checks that the string matches the supplied regexp. The regexp description should be a description of the string that will match the regexp. The error returned will say that the string "should be: " followed by this description. So, for instance, if the regexp matches a string of numbers then the description could be 'numeric'.

func StringNot

func StringNot(c String, errMsg string) String

StringNot returns a function that will check that the value, when passed to the check func, does not pass it. You must also supply the error text to appear after the string that fails. This error text should be a string that describes the quality that the string should not have. So, for instance, if the function being Not'ed was

check.StringHasPrefix("Hello")

then the errMsg parameter should be

"a string with prefix 'Hello'".

func StringOr

func StringOr(chkFuncs ...String) String

StringOr returns a function that will check that the value, when passed to each of the check funcs in turn, passes at least one of them

type StringSlice

type StringSlice func(v []string) error

StringSlice is the type of a check function for a slice of strings. It takes a slice of strings as a parameter and returns an error or nil if there is no error

func StringSliceAnd

func StringSliceAnd(chkFuncs ...StringSlice) StringSlice

StringSliceAnd returns a function that will check that the value, when passed to each of the check funcs in turn, passes all of them

func StringSliceContains added in v1.3.0

func StringSliceContains(sc String, condition string) StringSlice

StringSliceContains returns a check function that checks that at least one entry in the list matches the supplied String check func. The condition parameter should describe the check that is being performed. For instance, if the check is that the string length must be greater than 5 characters then the condition parameter should be

"the string should be greater than 5 characters"

func StringSliceLenBetween

func StringSliceLenBetween(low, high int) StringSlice

StringSliceLenBetween returns a check function that checks that the length of the list is between the two supplied values (inclusive)

func StringSliceLenEQ

func StringSliceLenEQ(i int) StringSlice

StringSliceLenEQ returns a check function that checks that the length of the list equals the supplied value

func StringSliceLenGT

func StringSliceLenGT(i int) StringSlice

StringSliceLenGT returns a check function that checks that the length of the list is greater than the supplied value

func StringSliceLenLT

func StringSliceLenLT(i int) StringSlice

StringSliceLenLT returns a check function that checks that the length of the list is less than the supplied value

func StringSliceNot

func StringSliceNot(c StringSlice, errMsg string) StringSlice

StringSliceNot returns a function that will check that the value, when passed to the check func, does not pass it. You must also supply the error text to appear after the value that fails. This error text should be a string that describes the quality that the slice of strings should not have.

func StringSliceOr

func StringSliceOr(chkFuncs ...StringSlice) StringSlice

StringSliceOr returns a function that will check that the value, when passed to each of the check funcs in turn, passes at least one of them

func StringSliceStringCheck

func StringSliceStringCheck(sc String) StringSlice

StringSliceStringCheck returns a check function that checks that every entry in the list passes the supplied String check func

func StringSliceStringCheckByPos added in v1.4.0

func StringSliceStringCheckByPos(scs ...String) StringSlice

StringSliceStringCheckByPos returns a check function that checks that a given entry in the list passes the corresponding supplied String check func. If there are more entries in the slice than check.String functions then the final supplied check is applied. If there are fewer entries than functions then the excess checks are not applied. Note that you could choose to combine this check with a test of the length of the slice by means of a StringSliceAnd check.

type Time

type Time func(d time.Time) error

Time is the type of a check function which takes an time.Time parameter and returns an error or nil if the check passes

func TimeAnd

func TimeAnd(chkFuncs ...Time) Time

TimeAnd returns a function that will check that the value, when passed to each of the check funcs in turn, passes all of them

func TimeBetween

func TimeBetween(start, end time.Time) Time

TimeBetween returns a function that will check that the time is between the start and end times

func TimeEQ

func TimeEQ(t time.Time) Time

TimeEQ returns a function that will check that the time is equal to the value of the t parameter

func TimeGT

func TimeGT(t time.Time) Time

TimeGT returns a function that will check that the time is after the value of the t parameter

func TimeIsOnDOW

func TimeIsOnDOW(dow time.Weekday, otherDOW ...time.Weekday) Time

TimeIsOnDOW returns a function that will check that the time is on the day of the week given by one of the parameters

func TimeLT

func TimeLT(t time.Time) Time

TimeLT returns a function that will check that the time is before the value of the t parameter

func TimeNot

func TimeNot(c Time, errMsg string) Time

TimeNot returns a function that will check that the value, when passed to the check func, does not pass it. You must also supply the error text to appear after the value that fails. This error text should be a string that describes the quality that the value should not have. So, for instance, if the function being Not'ed was

check.TimeIsOnDOW(time.Monday)

then the errMsg parameter should be

"on a Monday.

func TimeOr

func TimeOr(chkFuncs ...Time) Time

TimeOr returns a function that will check that the value, when passed to each of the check funcs in turn, passes at least one of them

type TimeLocation

type TimeLocation func(l *time.Location) error

TimeLocation is the type of a check function for a time.Location. It takes a pointer to a time.Location parameter and returns an error or nil if the check passes

Jump to

Keyboard shortcuts

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