psetter

package
v3.6.0 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2020 License: MIT Imports: 10 Imported by: 3

Documentation

Overview

Package psetter contains a collection of useful types that can be used to set parameter values of a program.

Each type satisfies the param.Setter interface. These can be used to supply the second argument of a PSet Add or AddByPos method - the action associated with the parameter. When the parameter is found while parsing the params the appropriate Set method will be called.

A typical Setter is used to set the value of a parameter to the program. For example below, a bool variable

exitOnErrors

is set to true by the Bool object's Set method if the parameter

exit-on-error

is found among the command line arguments:

var exitOnErrors bool
ps, err := paramset.New()
p := ps.Add("exit-on-errors",
    psetter.Bool{Value: &exitOnErrors},
    "Errors make the program exit if this flag is set to true")

It is expected that the most common use of this package will be to pass instances of the various setters as a parameter to the Add(...) method.

Index

Constants

View Source
const (
	TimeFmtDefault   = "2006/Jan/02T15:04:05"
	TimeFmtHMS       = "15:04:05"
	TimeFmtHoursMins = "15:04"
	TimeFmtDateOnly  = "2006/Jan/02"
	TimeFmtTimestamp = "20060102.150405"
	TimeFmtISO8601   = "2006-01-02T15:04:05"
)

Time Formats given here can be used to set the Format member of the Time struct

View Source
const (
	StrListDefaultSep = ","
)

StrListDefaultSep is the default separator for a list of strings. it is set to a comma

Variables

This section is empty.

Functions

func HasChecks

func HasChecks(cc CheckCounter) string

HasChecks returns a string reporting whether or not the number of checks is zero. This is suitable for constructing a string to be returned by an AllowedValues function.

func NilValueMessage

func NilValueMessage(paramName, setterType string) string

NilValueMessage ...

Types

type Bool

type Bool struct {
	param.ValueReqOptional
	param.NilAVM

	Value *bool

	Invert bool
}

Bool is used to set boolean flags

The Invert flag is used to invert the normal meaning of a boolean parameter. It is useful where you want to have a parameter of the form 'dont-xxx' but use it to set the value of a bool variable (default value: true) such as 'xxx' which you can then test by saying:

if xxx { doXXX() }

rather than having to set the value of a variable which you would have to call dontXXX and then test by saying:

if !dontXXX { doXXX() }

The benefit is that you can avoid the ugly double negative

func (Bool) AllowedValues

func (s Bool) AllowedValues() string

AllowedValues returns a description of the allowed values.

func (Bool) CheckSetter

func (s Bool) CheckSetter(name string)

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

func (Bool) CurrentValue

func (s Bool) CurrentValue() string

CurrentValue returns the current setting of the parameter value

func (Bool) Set

func (s Bool) Set(_ string) error

Set sets the parameter value to true

func (Bool) SetWithVal

func (s Bool) SetWithVal(_, val string) error

SetWithVal should be called when a value is given for the parameter

type CheckCounter

type CheckCounter interface {
	CountChecks() int
}

CheckCounter can be used if you want to report the number of checks that a type has

type Duration

type Duration struct {
	param.ValueReqMandatory
	param.NilAVM

	Value  *time.Duration
	Checks []check.Duration
}

Duration allows you to specify a parameter that can be used to set a time.Duration value. You can also supply a check function that will validate the Value. See the check package for some common pre-defined checks.

func (Duration) AllowedValues

func (s Duration) AllowedValues() string

AllowedValues returns a string describing the allowed values

func (Duration) CheckSetter

func (s Duration) CheckSetter(name string)

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

func (Duration) CountChecks

func (s Duration) CountChecks() int

CountChecks returns the number of check functions this setter has

func (Duration) CurrentValue

func (s Duration) CurrentValue() string

CurrentValue returns the current setting of the parameter value

func (Duration) SetWithVal

func (s Duration) SetWithVal(_ string, paramVal string) error

SetWithVal (called when a value follows the parameter) checks that the value can be parsed to a duration, if it cannot be parsed successfully it returns an error. If there is a check and the check is violated it returns an error. Only if the value is parsed successfully and the check is not violated is the Value set.

type Enum

type Enum struct {
	param.ValueReqMandatory
	param.AVM

	Value *string
}

Enum allows you to specify a parameter that will only allow an enumerated range of values which are specified in the AllowedVals map which maps each allowed value to a description

func (Enum) AllowedValues

func (s Enum) AllowedValues() string

AllowedValues returns a string listing the allowed values

func (Enum) CheckSetter

func (s Enum) CheckSetter(name string)

CheckSetter panics if the setter has not been properly created - if the Value is nil or there are no allowed values.

func (Enum) CurrentValue

func (s Enum) CurrentValue() string

CurrentValue returns the current setting of the parameter value

func (Enum) SetWithVal

func (s Enum) SetWithVal(_ string, paramVal string) error

SetWithVal (called when a value follows the parameter) checks the value for validity and only if it is in the allowed values list does it set the Value. It returns an error if the value is invalid.

type EnumList

type EnumList struct {
	param.ValueReqMandatory
	param.AVM

	Value *[]string
	StrListSeparator
	Checks []check.StringSlice
}

EnumList sets the values in a slice of strings. The values must be in the allowed values map

func (EnumList) AllowedValues

func (s EnumList) AllowedValues() string

AllowedValues returns a string listing the allowed values

func (EnumList) CheckSetter

func (s EnumList) CheckSetter(name string)

CheckSetter panics if the setter has not been properly created - if the Value is nil or there are no allowed values or the initial value is not allowed.

func (EnumList) CountChecks

func (s EnumList) CountChecks() int

CountChecks returns the number of check functions this setter has

func (EnumList) CurrentValue

func (s EnumList) CurrentValue() string

CurrentValue returns the current setting of the parameter value

func (EnumList) SetWithVal

func (s EnumList) SetWithVal(_ string, paramVal string) error

SetWithVal (called when a value follows the parameter) splits the value using the list separator. It then checks all the values for validity and only if all the values are in the allowed values list does it add them to the slice of strings pointed to by the Value. It returns a error for the first invalid value or if a check is breached.

type EnumMap

type EnumMap struct {
	param.ValueReqMandatory
	param.AVM

	Value                 *map[string]bool
	AllowHiddenMapEntries bool
	StrListSeparator
}

EnumMap sets the entry in a map of strings. The values initially set in the map must be in the allowed values map unless AllowHiddenMapEntries is set to true. Only values with keys in the allowed values map can be set. If you allow hidden values then you can have entries in your map which cannot be set through this interface but this will still only allow values to be set which are in the allowed values map.

It is recommended that you should use string constants for setting and accessing the map entries and for initialising the allowed values map to avoid possible errors.

func (EnumMap) AllowedValues

func (s EnumMap) AllowedValues() string

AllowedValues returns a string listing the allowed values

func (EnumMap) CheckSetter

func (s EnumMap) CheckSetter(name string)

CheckSetter panics if the setter has not been properly created - if the Value is nil or the map has not been created yet or if there are no allowed values.

func (EnumMap) CurrentValue

func (s EnumMap) CurrentValue() string

CurrentValue returns the current setting of the parameter value

func (EnumMap) SetWithVal

func (s EnumMap) SetWithVal(_ string, paramVal string) error

SetWithVal (called when a value follows the parameter) splits the value using the list separator. It then checks all the values for validity and only if all the values are in the allowed values list does it set the entries in the map of strings pointed to by the Value. It returns a error for the first invalid value.

type Float64

type Float64 struct {
	param.ValueReqMandatory
	param.NilAVM

	Value  *float64
	Checks []check.Float64
}

Float64 allows you to specify a parameter that can be used to set an float64 value. You can also supply a check function that will validate the Value. There are some helper functions given below (called Float64Check...) which will return functions that can perform a few common checks. For instance you can ensure that the value is positive by setting one of the Checks to the value returned by Float64CheckGT(0)

func (Float64) AllowedValues

func (s Float64) AllowedValues() string

AllowedValues returns a string describing the allowed values

func (Float64) CheckSetter

func (s Float64) CheckSetter(name string)

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

func (Float64) CountChecks

func (s Float64) CountChecks() int

CountChecks returns the number of check functions this setter has

func (Float64) CurrentValue

func (s Float64) CurrentValue() string

CurrentValue returns the current setting of the parameter value

func (Float64) SetWithVal

func (s Float64) SetWithVal(_ string, paramVal string) error

SetWithVal (called when a value follows the parameter) checks that the value can be parsed to a float, if it cannot be parsed successfully it returns an error. If there is a check and the check is violated it returns an error. Only if the value is parsed successfully and the check is not violated is the Value set.

type Int64

type Int64 struct {
	param.ValueReqMandatory
	param.NilAVM

	Value  *int64
	Checks []check.Int64
}

Int64 allows you to specify a parameter that can be used to set an int64 value. You can also supply a check function that will validate the Value. See the check package for some helper functions which will return functions that can perform a few common checks. For instance you can ensure that the value is positive by setting one of the Checks to the value returned by check.Int64GT(0)

func (Int64) AllowedValues

func (s Int64) AllowedValues() string

AllowedValues returns a string describing the allowed values

func (Int64) CheckSetter

func (s Int64) CheckSetter(name string)

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

func (Int64) CountChecks

func (s Int64) CountChecks() int

CountChecks returns the number of check functions this setter has

func (Int64) CurrentValue

func (s Int64) CurrentValue() string

CurrentValue returns the current setting of the parameter value

func (Int64) SetWithVal

func (s Int64) SetWithVal(_ string, paramVal string) error

SetWithVal (called when a value follows the parameter) checks that the value can be parsed to an integer, if it cannot be parsed successfully it returns an error. If there are checks and any check is violated it returns an error. Only if the value is parsed successfully and no checks are violated is the Value set.

type Int64List

type Int64List struct {
	param.ValueReqMandatory
	param.NilAVM

	Value *[]int64
	StrListSeparator
	Checks []check.Int64Slice
}

Int64List allows you to specify a parameter that can be used to set a list (a slice) of int64's. You can override the list separator by setting the Sep value.

If you have a list of allowed values you should use EnumList

func (Int64List) AllowedValues

func (s Int64List) AllowedValues() string

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

func (Int64List) CheckSetter

func (s Int64List) CheckSetter(name string)

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

func (Int64List) CountChecks

func (s Int64List) CountChecks() int

CountChecks returns the number of check functions this setter has

func (Int64List) CurrentValue

func (s Int64List) CurrentValue() string

CurrentValue returns the current setting of the parameter value

func (Int64List) SetWithVal

func (s Int64List) SetWithVal(_ string, paramVal string) error

SetWithVal (called when a value follows the parameter) splits the value into a slice of int64's and sets the Value accordingly. It will return an error if a check is breached.

type Map

Map sets the entry in a map of strings. Each value from the parameter is used as a key in the map with the map entry set to true.

func (Map) AllowedValues

func (s Map) AllowedValues() string

AllowedValues returns a string listing the allowed values

func (Map) CheckSetter

func (s Map) CheckSetter(name string)

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

func (Map) CountChecks added in v3.3.0

func (s Map) CountChecks() int

CountChecks returns the number of check functions this setter has

func (Map) CurrentValue

func (s Map) CurrentValue() string

CurrentValue returns the current setting of the parameter value

func (Map) SetWithVal

func (s Map) SetWithVal(_ string, paramVal string) error

SetWithVal (called when a value follows the parameter) splits the value using the list separator.

type Nil

type Nil struct {
	param.ValueReqNone
	param.NilAVM
}

Nil is used if no value is to be set. It can be useful if the only effect is to be through the PostAction.

func (Nil) AllowedValues

func (s Nil) AllowedValues() string

AllowedValues returns a description of the allowed values.

func (Nil) CheckSetter

func (s Nil) CheckSetter(name string)

CheckSetter does nothing.

func (Nil) CurrentValue

func (s Nil) CurrentValue() string

CurrentValue returns the current setting of the parameter value, in this case there is never any current value.

func (Nil) Set

func (s Nil) Set(_ string) error

Set does nothing.

type Pathname

type Pathname struct {
	param.ValueReqMandatory
	param.NilAVM

	Value       *string
	Expectation filecheck.Provisos
	Checks      []check.String
}

Pathname allows you to specify a parameter that can be used to set an pathname value. You can specify some required attributes of the referenced file-system object by setting the AttributesNeeded member. You can also supply a check function that will validate the Value. For instance you could check that the value was in a particular directory

func (Pathname) AllowedValues

func (s Pathname) AllowedValues() string

AllowedValues returns a string describing the allowed values

func (Pathname) CheckSetter

func (s Pathname) CheckSetter(name string)

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

func (Pathname) CountChecks

func (s Pathname) CountChecks() int

CountChecks returns the number of check functions this setter has

func (Pathname) CurrentValue

func (s Pathname) CurrentValue() string

CurrentValue returns the current setting of the parameter value

func (Pathname) SetWithVal

func (s Pathname) SetWithVal(_ string, paramVal string) error

SetWithVal (called when a value follows the parameter) checks first that the value can be converted into a pathname. Then it confirms that the file conforms to the supplied provisos. If there are checks and any check is violated it returns an error. Only if the value is converted successfully and no checks are violated is the Value set and a nil error is returned.

type Regexp

type Regexp struct {
	param.ValueReqMandatory
	param.NilAVM

	Value **regexp.Regexp
}

Regexp allows you to specify a parameter that can be used to set an regexp value.

func (Regexp) AllowedValues

func (s Regexp) AllowedValues() string

AllowedValues returns a string describing the allowed values

func (Regexp) CheckSetter

func (s Regexp) CheckSetter(name string)

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

func (Regexp) CurrentValue

func (s Regexp) CurrentValue() string

CurrentValue returns the current setting of the parameter value

func (Regexp) SetWithVal

func (s Regexp) SetWithVal(_ string, paramVal string) error

SetWithVal (called when a value follows the parameter) checks that the value can be parsed to regular expression, if it cannot be parsed successfully it returns an error. Only if the value is parsed successfully is the Value set.

type StrList

type StrList struct {
	param.ValueReqMandatory
	param.NilAVM

	Value *[]string
	StrListSeparator
	Checks []check.StringSlice
}

StrList allows you to specify a parameter that can be used to set an list (a slice) of strings. You can override the list separator by setting the Sep value.

If you have a list of allowed values you should use EnumList

func (StrList) AllowedValues

func (s StrList) AllowedValues() string

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

func (StrList) CheckSetter

func (s StrList) CheckSetter(name string)

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

func (StrList) CountChecks

func (s StrList) CountChecks() int

CountChecks returns the number of check functions this setter has

func (StrList) CurrentValue

func (s StrList) CurrentValue() string

CurrentValue returns the current setting of the parameter value

func (StrList) SetWithVal

func (s StrList) SetWithVal(_ string, paramVal string) error

SetWithVal (called when a value follows the parameter) splits the value into a slice of strings and sets the Value accordingly. It will return an error if a check is breached.

type StrListAppender added in v3.2.0

type StrListAppender struct {
	param.ValueReqMandatory
	param.NilAVM

	Value *[]string
	StrListSeparator
	Checks []check.String
}

StrListAppender allows you to specify a parameter that can be used to add to a list (a slice) of strings.

The user of the program which has a parameter of this type can pass multiple parameters and each will add to the list of values rather than replacing it each time. Note that each value must be passed separately - there is no way to pass multiple values at the same time. Also note that there is no way to reset the value, if y=this feature is required another parameter could be set up that will do this.

If you have a list of allowed values you should use EnumList

func (StrListAppender) AllowedValues added in v3.2.0

func (s StrListAppender) AllowedValues() string

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

func (StrListAppender) CheckSetter added in v3.2.0

func (s StrListAppender) CheckSetter(name string)

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

func (StrListAppender) CountChecks added in v3.2.0

func (s StrListAppender) CountChecks() int

CountChecks returns the number of check functions this setter has

func (StrListAppender) CurrentValue added in v3.2.0

func (s StrListAppender) CurrentValue() string

CurrentValue returns the current setting of the parameter value

func (StrListAppender) SetWithVal added in v3.2.0

func (s StrListAppender) SetWithVal(_ string, paramVal string) error

SetWithVal (called when a value follows the parameter) splits the value into a slice of strings and sets the Value accordingly. It will return an error if a check is breached.

type StrListSeparator

type StrListSeparator struct {
	Sep string
}

StrListSeparator holds the separator value

func (StrListSeparator) GetSeparator

func (sls StrListSeparator) GetSeparator() string

GetSeparator returns the separator or the default value (a comma) if it is an empty string

func (StrListSeparator) ListValDesc

func (sls StrListSeparator) ListValDesc(name string) string

ListValDesc returns that fragment of the description of what values are allowed which explains how the list values are separated from one another.

type String

type String struct {
	param.ValueReqMandatory
	param.NilAVM

	Value  *string
	Checks []check.String
}

String is the type for setting string values from parameters

func (String) AllowedValues

func (s String) AllowedValues() string

AllowedValues simply returns "any string" since String does not check its value

func (String) CheckSetter

func (s String) CheckSetter(name string)

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

func (String) CountChecks

func (s String) CountChecks() int

CountChecks returns the number of check functions this setter has

func (String) CurrentValue

func (s String) CurrentValue() string

CurrentValue returns the current setting of the parameter value

func (String) SetWithVal

func (s String) SetWithVal(_ string, paramVal string) error

SetWithVal checks that the parameter value meets the checks if any. It returns an error if the check is not satisfied. Only if the check is not violated is the Value set.

type Time added in v3.6.0

type Time struct {
	param.ValueReqMandatory
	param.NilAVM

	Value  *time.Time
	Format string
	Checks []check.Time
}

Time allows you to specify a parameter that can be used to set a time.Time value. You can also supply check functions that will validate the Value. If no Format is given then the default format (see TimeFmtDefault) will be used to parse the time value

func (Time) AllowedValues added in v3.6.0

func (s Time) AllowedValues() string

AllowedValues returns a string describing the allowed values

func (Time) CheckSetter added in v3.6.0

func (s Time) CheckSetter(name string)

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

func (Time) CountChecks added in v3.6.0

func (s Time) CountChecks() int

CountChecks returns the number of check functions this setter has

func (Time) CurrentValue added in v3.6.0

func (s Time) CurrentValue() string

CurrentValue returns the current setting of the parameter value

func (Time) SetWithVal added in v3.6.0

func (s Time) SetWithVal(_ string, paramVal string) error

SetWithVal (called when a value follows the parameter) checks that the value can be parsed to a time, if it cannot be parsed successfully it returns an error. If there is a check and the check is violated it returns an error. Only if the value is parsed successfully and the check is not violated is the Value set.

type TimeLocation

type TimeLocation struct {
	param.ValueReqMandatory
	param.NilAVM

	Value  **time.Location
	Checks []check.TimeLocation
}

TimeLocation allows you to specify a parameter that can be used to set a time.Location pointer. You can also supply check functions that will validate the Value.

func (TimeLocation) AllowedValues

func (s TimeLocation) AllowedValues() string

AllowedValues returns a string describing the allowed values

func (TimeLocation) CheckSetter

func (s TimeLocation) CheckSetter(name string)

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

func (TimeLocation) CountChecks

func (s TimeLocation) CountChecks() int

CountChecks returns the number of check functions this setter has

func (TimeLocation) CurrentValue

func (s TimeLocation) CurrentValue() string

CurrentValue returns the current setting of the parameter value

func (TimeLocation) SetWithVal

func (s TimeLocation) SetWithVal(_ string, paramVal string) error

SetWithVal (called when a value follows the parameter) checks that the value can be parsed to a location, if it cannot be parsed successfully it returns an error. If there is a check and the check is violated it returns an error. Only if the value is parsed successfully and the check is not violated is the Value set. If the supplied value cannot be successfully translated into a time.Location then any embedded spaces will be converted to underscores and the value will be retried. If this also fails then the original error is returned.

Jump to

Keyboard shortcuts

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