checksetter

package
v4.0.14 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2023 License: MIT Imports: 10 Imported by: 2

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

Examples

Constants

View Source
const Float64CheckerName = "float64-checker"
View Source
const Int64CheckerName = "int64-checker"
View Source
const IntCheckerName = "int-checker"
View Source
const StringCheckerName = "string-checker"
View Source
const StringSliceCheckerName = "string-slice-checker"

Variables

This section is empty.

Functions

func AllowedValues

func AllowedValues(checkerName string, makerFuncs map[string][]string) string

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

type MakerFunc[T any] func(*ast.CallExpr, string) (check.ValCk[T], error)

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

type MakerInfo[T any] struct {
	Args []string
	MF   MakerFunc[T]
}

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

func FindParser[T any](checkerName string) (*Parser[T], error)

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

func FindParserOrPanic[T any](checkerName string) *Parser[T]

FindParserOrPanic finds a pre-registered parser with the given checker name or else it panics

func MakeParser

func MakeParser[T any](checkerName string, makers map[string]MakerInfo[T]) (
	Parser[T], error,
)

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

func (p Parser[T]) Args(makerName string) ([]string, error)

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

func (p Parser[T]) CallExprMaker(e *ast.CallExpr) (check.ValCk[T], error)

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

func (p Parser[T]) CheckerName() string

CheckerName returns the name of the checkers that will be generated

func (Parser[T]) IdentMaker

func (p Parser[T]) IdentMaker(e *ast.Ident) (check.ValCk[T], error)

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

func (p Parser[T]) MakerFuncs() map[string][]string

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

func (p Parser[T]) Makers() []string

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.

func (Parser[T]) Parse

func (p Parser[T]) Parse(s string) ([]check.ValCk[T], error)

Parse will parse the given string and return a slice of check.ValCk functions of the appropriate type and an error. The error will be nil if the parsing was successful, otherwise an error describing the problem and a nil slice will be returned.

func (Parser[T]) ParseExpr

func (p Parser[T]) ParseExpr(elt ast.Expr) (cf check.ValCk[T], err error)

ParseExpr parses an individual element from the list of functions. You should only need to call this if you are writing your own checker parser.

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

func (s Setter[T]) AllowedValues() string

AllowedValues returns a description of the allowed values. It includes the separator to be used

func (Setter[T]) CheckSetter

func (s Setter[T]) CheckSetter(name string)

CheckSetter panics if the setter has not been properly created - if the Value is nil or the Parser is nil.

func (Setter[T]) CurrentValue

func (s Setter[T]) CurrentValue() string

CurrentValue returns the current setting of the parameter value

func (*Setter[T]) SetWithVal

func (s *Setter[T]) SetWithVal(_ string, paramVal string) error

SetWithVal (called when a value follows the parameter) splits the value into a slice of check.Int64's and sets the Value accordingly.

Jump to

Keyboard shortcuts

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