Documentation ¶
Overview ¶
Package checksetter provides a generic Setter that can be used to construct lists of check functions. It also provides several pre-constructed Parsers for converting a string into a slice of check.ValCk functions of the appropriate type. To construct the Setter you should set the Value as with other setters but you will also need to set the Parser to use. You can retrieve a Parser value with which to initialise the Setter with the FindParser func which you will need to call with the type set to the type of value to be checked and the checker name set to one of the const ...CheckerName values.
If you choose to write your own Parser you should do it by calling the MakeParser func which will register the Parser so that it can be retrieved with the FindParser func. This will then also allow the Setter to provide correct AllowedValues.
Index ¶
- Constants
- func AllowedValues(checkerName string, makerFuncs map[string][]string) string
- func ParsersAvailable() []string
- type MakerFunc
- type MakerInfo
- type Parser
- func (p Parser[T]) Args(makerName string) ([]string, error)
- func (p Parser[T]) CallExprMaker(e *ast.CallExpr) (check.ValCk[T], error)
- func (p Parser[T]) CheckerName() string
- func (p Parser[T]) IdentMaker(e *ast.Ident) (check.ValCk[T], error)
- func (p Parser[T]) MakerFuncs() map[string][]string
- func (p Parser[T]) Makers() []string
- func (p Parser[T]) Parse(s string) ([]check.ValCk[T], error)
- func (p Parser[T]) ParseExpr(elt ast.Expr) (cf check.ValCk[T], err error)
- type Setter
Examples ¶
Constants ¶
const Float64CheckerName = "float64-checker"
const Int64CheckerName = "int64-checker"
const IntCheckerName = "int-checker"
const StringCheckerName = "string-checker"
const StringSliceCheckerName = "string-slice-checker"
Variables ¶
This section is empty.
Functions ¶
func AllowedValues ¶
AllowedValues returns a string descibing the allowed values for the given class of Check functions
func ParsersAvailable ¶
func ParsersAvailable() []string
ParsersAvailable returns a sorted list of all the available parsers
Types ¶
type MakerFunc ¶
MakerFunc is the type of a function that converts a CallExpr into a check func. The string should contain the name of the function to be generated.
type MakerInfo ¶
MakerInfo holds details of the function used to generate a check func. It holds the function value and a list of the arguments that it should take. Note that the Args member is present purely for documentation purposes; all the functions generated should be created by makers taking the listed args.
type Parser ¶
type Parser[T any] struct { // contains filtered or unexported fields }
Parser records the available maker functions for generating the named checker functions. Note that a given parser will generate checker functions all of the same type.
func FindParser ¶
FindParser finds a pre-registered parser with the given checker name. It will return nil if there is no such Parser already registered. Note that the return type is 'any'; it is the caller's responsibility to check that it is of the type required.
func FindParserOrPanic ¶
FindParserOrPanic finds a pre-registered parser with the given checker name or else it panics
func MakeParser ¶
MakeParser[T any] creates a new parser and adds it to the Parser register. It will return an error if there is already an entry with the same name.
func (Parser[T]) Args ¶
Args returns the args that the named maker function takes.
This can be used to construct the Allowed Values message for a setter.
func (Parser[T]) CallExprMaker ¶
CallExprMaker finds the function name using the information given in the CallExpr and then calls the maker func to build the check.ValCk function. It returns whatever that returns with no further processing
func (Parser[T]) CheckerName ¶
CheckerName returns the name of the checkers that will be generated
func (Parser[T]) IdentMaker ¶
IdentMaker finds the function name using the information given in the Ident and then calls the maker func to build the check.ValCk function. It returns whatever that returns with no further processing
func (Parser[T]) MakerFuncs ¶
MakerFuncs returns a map of all the functions that the parser recognises and the arguments they each expect.
This can be used to construct the Allowed Values message for a setter.
func (Parser[T]) Makers ¶
Makers returns a slice holding the names of the checker-makers. The names are in alphabetical order.
This can be used to construct the Allowed Values message for a setter.
type Setter ¶
type Setter[T any] struct { psetter.ValueReqMandatory Parser *Parser[T] Value *[]check.ValCk[T] // contains filtered or unexported fields }
Setter satisfies the param.Setter interface. Important points of difference are that you need to provide both the Parser (use the checksetter.FindParserOrPanic func) and the Value when initialising the Setter. Also, you will need to pass the address of the Setter rather than the Setter itself, this is because the SetWithVal method takes a pointer receiver.
Example ¶
ExampleSetter demonstrates how the Setter should be used with the param package
package main import ( "fmt" "github.com/nickwells/check.mod/v2/check" "github.com/nickwells/checksetter.mod/v4/checksetter" "github.com/nickwells/param.mod/v6/paramset" ) func main() { chkFuncs := []check.ValCk[string]{} ps := paramset.NewOrPanic() ps.Add("checks", &checksetter.Setter[string]{ Value: &chkFuncs, Parser: checksetter.FindParserOrPanic[string]( checksetter.StringCheckerName), }, "help-text") ps.Parse([]string{"-checks", "OK, Length(GT(1))"}) fmt.Printf("%d checks provided\n", len(chkFuncs)) }
Output: 2 checks provided
func (Setter[T]) AllowedValues ¶
AllowedValues returns a description of the allowed values. It includes the separator to be used
func (Setter[T]) CheckSetter ¶
CheckSetter panics if the setter has not been properly created - if the Value is nil or the Parser is nil.
func (Setter[T]) CurrentValue ¶
CurrentValue returns the current setting of the parameter value