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 ¶
- func FileInfoIsDir(fi os.FileInfo) error
- func FileInfoIsRegular(fi os.FileInfo) error
- func FileInfoOwnedBySelf(fi fs.FileInfo) error
- func Int64SliceNoDups(v []int64) error
- func StringOK(_ string) error
- func StringSliceNoDups(v []string) error
- type Duration
- func DurationAnd(chkFuncs ...Duration) Duration
- func DurationBetween(low, high time.Duration) Duration
- func DurationGE(limit time.Duration) Duration
- func DurationGT(limit time.Duration) Duration
- func DurationLE(limit time.Duration) Duration
- func DurationLT(limit time.Duration) Duration
- func DurationNot(c Duration, errMsg string) Duration
- func DurationOr(chkFuncs ...Duration) Duration
- type FileInfo
- func FileInfoAnd(chkFuncs ...FileInfo) FileInfo
- func FileInfoGidEQ(gid uint32) FileInfo
- func FileInfoModTime(c Time) FileInfo
- func FileInfoMode(m os.FileMode) FileInfo
- func FileInfoName(c String) FileInfo
- func FileInfoNot(c FileInfo, errMsg string) FileInfo
- func FileInfoOr(chkFuncs ...FileInfo) FileInfo
- func FileInfoPerm(c FilePerm) FileInfo
- func FileInfoSize(c Int64) FileInfo
- func FileInfoUidEQ(uid uint32) FileInfo
- type FilePerm
- type Float64
- func Float64And(chkFuncs ...Float64) Float64
- func Float64Between(low, high float64) Float64
- func Float64GE(limit float64) Float64
- func Float64GT(limit float64) Float64
- func Float64LE(limit float64) Float64
- func Float64LT(limit float64) Float64
- func Float64Not(c Float64, errMsg string) Float64
- func Float64Or(chkFuncs ...Float64) Float64
- type Int64
- func Int64And(chkFuncs ...Int64) Int64
- func Int64Between(low, high int64) Int64
- func Int64Divides(d int64) Int64
- func Int64EQ(limit int64) Int64
- func Int64GE(limit int64) Int64
- func Int64GT(limit int64) Int64
- func Int64IsAMultiple(d int64) Int64
- func Int64LE(limit int64) Int64
- func Int64LT(limit int64) Int64
- func Int64Not(c Int64, errMsg string) Int64
- func Int64Or(chkFuncs ...Int64) Int64
- type Int64Slice
- func Int64SliceAnd(chkFuncs ...Int64Slice) Int64Slice
- func Int64SliceInt64Check(sc Int64) Int64Slice
- func Int64SliceLenBetween(low, high int) Int64Slice
- func Int64SliceLenEQ(i int) Int64Slice
- func Int64SliceLenGT(i int) Int64Slice
- func Int64SliceLenLT(i int) Int64Slice
- func Int64SliceNot(c Int64Slice, errMsg string) Int64Slice
- func Int64SliceOr(chkFuncs ...Int64Slice) Int64Slice
- type MapStringBool
- func MapStringBoolAnd(chkFuncs ...MapStringBool) MapStringBool
- func MapStringBoolContains(sc String, condition string) MapStringBool
- func MapStringBoolLenBetween(low, high int) MapStringBool
- func MapStringBoolLenEQ(i int) MapStringBool
- func MapStringBoolLenGT(i int) MapStringBool
- func MapStringBoolLenLT(i int) MapStringBool
- func MapStringBoolNot(c MapStringBool, errMsg string) MapStringBool
- func MapStringBoolOr(chkFuncs ...MapStringBool) MapStringBool
- func MapStringBoolStringCheck(sc String) MapStringBool
- func MapStringBoolTrueCountBetween(low, high int) MapStringBool
- func MapStringBoolTrueCountEQ(i int) MapStringBool
- func MapStringBoolTrueCountGT(i int) MapStringBool
- func MapStringBoolTrueCountLT(i int) MapStringBool
- type String
- func StringAnd(chkFuncs ...String) String
- func StringEquals(s string) String
- func StringHasPrefix(prefix string) String
- func StringHasSuffix(suffix string) String
- func StringLenBetween(low, high int) String
- func StringLenEQ(limit int) String
- func StringLenGT(limit int) String
- func StringLenLT(limit int) String
- func StringMatchesPattern(re *regexp.Regexp, reDesc string) String
- func StringNot(c String, errMsg string) String
- func StringOr(chkFuncs ...String) String
- type StringSlice
- func StringSliceAnd(chkFuncs ...StringSlice) StringSlice
- func StringSliceContains(sc String, condition string) StringSlice
- func StringSliceLenBetween(low, high int) StringSlice
- func StringSliceLenEQ(i int) StringSlice
- func StringSliceLenGT(i int) StringSlice
- func StringSliceLenLT(i int) StringSlice
- func StringSliceNot(c StringSlice, errMsg string) StringSlice
- func StringSliceOr(chkFuncs ...StringSlice) StringSlice
- func StringSliceStringCheck(sc String) StringSlice
- func StringSliceStringCheckByPos(scs ...String) StringSlice
- type Time
- func TimeAnd(chkFuncs ...Time) Time
- func TimeBetween(start, end time.Time) Time
- func TimeEQ(t time.Time) Time
- func TimeGT(t time.Time) Time
- func TimeIsOnDOW(dow time.Weekday, otherDOW ...time.Weekday) Time
- func TimeLT(t time.Time) Time
- func TimeNot(c Time, errMsg string) Time
- func TimeOr(chkFuncs ...Time) Time
- type TimeLocation
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FileInfoIsDir ¶
FileInfoIsDir will check that the file info describes a directory
func FileInfoIsRegular ¶
FileInfoIsRegular will check that the file info describes a regular file
func FileInfoOwnedBySelf ¶ added in v1.1.0
FileInfoOwnedBySelf tests that the file is owned by the calling user
func Int64SliceNoDups ¶
Int64SliceNoDups checks that the list contains no duplicates
func StringOK ¶ added in v1.4.0
StringOK always returns a nil error - it can be of use with a StringSliceStringCheckByPos to allow any value in certain slice positions
func StringSliceNoDups ¶
StringSliceNoDups checks that the list contains no duplicates
Types ¶
type Duration ¶
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 ¶
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 ¶
DurationBetween returns a function that will check that the value lies between the upper and lower limits (inclusive)
func DurationGE ¶
DurationGE returns a function that will check that the value is greater than or equal to the limit
func DurationGT ¶
DurationGT returns a function that will check that the value is greater than the limit
func DurationLE ¶
DurationLE returns a function that will check that the value is less than or equal to the limit
func DurationLT ¶
DurationLT returns a function that will check that the value is less than the limit
func DurationNot ¶
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 ¶
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 ¶
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 ¶
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 ¶
FileInfoGidEQ returns a FileInfo (func) that tests that the file is owned by the specified user
func FileInfoModTime ¶
FileInfoModTime returns a function that will check that the file modification time passes the test specified by the passed Time check
func FileInfoMode ¶
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 ¶
FileInfoName returns a function that will check that the file name passes the test specified by the passed String check
func FileInfoNot ¶
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 ¶
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 ¶
FileInfoPerm returns a function that will check that the file permissions pass the test specified by the passed FilePerm check
func FileInfoSize ¶
FileInfoSize returns a function that will check that the file size passes the test specified by the passed Int64 check
func FileInfoUidEQ ¶
FileInfoUidEQ returns a FileInfo (func) that tests that the file is owned by the specified user
type FilePerm ¶
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 ¶
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 ¶
FilePermEQ returns a function that will check that the file permission is set to the value of the perms parameter
func FilePermHasAll ¶
FilePermHasAll returns a function that will check that the file permission has all of the permissions set in the perms parameter
func FilePermHasNone ¶
FilePermHasNone returns a function that will check that the file permission has none of the permissions set in the perms parameter
func FilePermNot ¶
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 ¶
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 ¶
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 ¶
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 ¶
Float64Between returns a function that will check that the value lies between the upper and lower limits (inclusive)
func Float64GE ¶
Float64GE returns a function that will check that the value is greater than or equal to the limit
func Float64GT ¶
Float64GT returns a function that will check that the value is greater than the limit
func Float64LE ¶
Float64LE returns a function that will check that the value is less than or equal to the limit
func Float64Not ¶
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".
type Int64 ¶
Int64 is the type of the check function for int64 values. It takes an int64 parameter and returns an error
func Int64And ¶
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 ¶
Int64Between returns a function that will check that the value lies between the upper and lower limits (inclusive)
func Int64Divides ¶
Int64Divides returns a function that will check that the value is a divisor of d
func Int64GE ¶
Int64GE returns a function that will check that the value is greater than or equal to the limit
func Int64IsAMultiple ¶
Int64IsAMultiple returns a function that will check that the value is a multiple of d
func Int64LE ¶
Int64LE returns a function that will check that the value is less than or equal to the limit
func Int64Not ¶
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".
type Int64Slice ¶
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
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 ¶
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 ¶
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
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 ¶
StringHasPrefix returns a function that checks that the string has the supplied string as a prefix
func StringHasSuffix ¶
StringHasSuffix returns a function that checks that the string has the supplied string as a suffix
func StringLenBetween ¶
StringLenBetween returns a function that will check that the length of the string is between the two limits (inclusive)
func StringLenEQ ¶
StringLenEQ returns a function that will check that the length of the string is equal to the limit
func StringLenGT ¶
StringLenGT returns a function that will check that the length of the string is less than the limit
func StringLenLT ¶
StringLenLT returns a function that will check that the length of the string is less than the limit
func StringMatchesPattern ¶
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 ¶
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'".
type StringSlice ¶
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 ¶
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 ¶
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 ¶
TimeBetween returns a function that will check that the time is between the start and end times
func TimeEQ ¶
TimeEQ returns a function that will check that the time is equal to the value of the t parameter
func TimeGT ¶
TimeGT returns a function that will check that the time is after the value of the t parameter
func TimeIsOnDOW ¶
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 ¶
TimeLT returns a function that will check that the time is before the value of the t parameter
func TimeNot ¶
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.
type TimeLocation ¶
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