ffval

package
v4.0.0-alpha.1 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2023 License: Apache-2.0 Imports: 7 Imported by: 12

Documentation

Overview

Package ffval provides common flag value types and helpers.

The types defined by this package implement flag.Value, and are intended to be used as values in an [ff.CoreFlagConfig].

Value represents a single instance of any type T that can be parsed from a string. The package defines a set of values for common underlying types, like Bool, String, Duration, etc.

List and UniqueList represent a sequence of values of type T, where each call to Set (potentially) adds the value to the end of the list. The package defines a small set of lists for common underlying types, like IntList, StringSet, etc.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultStringFunc

func DefaultStringFunc[T any](vals []T) string

DefaultStringFunc is used by List and UniqueList if no StringFunc is explicitly provided. Each value is rendered to a string via fmt.Sprint, and the strings are joined via strings.Join with a separator of ", ".

Types

type Bool

type Bool = Value[bool]

Bool is a flag value representing a bool. Values are parsed by strconv.ParseBool.

type BoolList

type BoolList = List[bool]

BoolList is a List of bools. Duplicates are permitted.

type BoolSet

type BoolSet = UniqueList[bool]

BoolSet is a UniqueList of bools. Duplicates are silently dropped.

type Byte

type Byte = Value[ffbyte]

Byte is a flag value representing a byte. Values are parsed with a custom function that expects a string containing a single byte.

type Complex64

type Complex64 = Value[complex64]

Complex64 is a flag value representing a complex64. Values are parsed by strconv.ParseComplex.

type Complex128

type Complex128 = Value[complex128]

Complex128 is a flag value representing a complex128. Values are parsed by strconv.ParseComplex.

type Duration

type Duration = Value[time.Duration]

Duration is a flag value representing a time.Duration. Values are parsed by time.ParseDuration.

type Float32

type Float32 = Value[float32]

Float32 is a flag value representing a float32. Values are parsed by strconv.ParseFloat.

type Float64

type Float64 = Value[float64]

Float64 is a flag value representing a float64. Values are parsed by strconv.ParseFloat.

type Int

type Int = Value[int]

Int is a flag value representing an int. Values are parsed by strconv.Atoi.

type Int8

type Int8 = Value[int8]

Int8 is a flag value representing an int8. Values are parsed by strconv.ParseInt.

type Int16

type Int16 = Value[int16]

Int16 is a flag value representing an int16. Values are parsed by strconv.ParseInt.

type Int32

type Int32 = Value[int32]

Int32 is a flag value representing an int32. Values are parsed by strconv.ParseInt.

type Int64

type Int64 = Value[int64]

Int64 is a flag value representing an int64. Values are parsed by strconv.ParseInt.

type IntList

type IntList = List[int]

IntList is a List of ints. Duplicates are permitted.

type IntSet

type IntSet = UniqueList[int]

IntSet is a UniqueList of ints. Duplicates are silently dropped.

type List

type List[T any] struct {
	// ParseFunc parses a string to the type T. If no ParseFunc is provided, and
	// T is a supported [ValueType], then a default ParseFunc will be assigned
	// lazily. If no ParseFunc is provided, and T is not a supported
	// [ValueType], then most method calls will panic.
	ParseFunc func(string) (T, error)

	// Pointer is the actual slice of type T which is managed and updated by the
	// list. If no Pointer is provided, a new slice is allocated lazily. For
	// this reason, callers should generally access the pointer via GetPointer,
	// rather than reading the field directly.
	Pointer *[]T

	// StringFunc is used by the String method to transform the underlying slice
	// of T to a string. If no StringFunc is provided, [DefaultStringFunc] is
	// used.
	StringFunc func([]T) string
	// contains filtered or unexported fields
}

List is a generic flag.Value that represents an ordered list of values. Every call to Set adds the successfully parsed value to the end of the list. To prevent duplicate values, see UniqueList.

func NewList

func NewList[T ValueType](ptr *[]T) *List[T]

NewList returns a list of underlying ValueType T, which updates the given pointer ptr when set.

func NewListParser

func NewListParser[T any](parseFunc func(string) (T, error)) *List[T]

NewListParser returns a list of any type T that can be parsed from a string.

This constructor is intended as a convenience function for tests; consumers who want to provide a parser are probably better served by constructing a list directly, so that they can also provide other fields in a single motion.

func (*List[T]) Get

func (v *List[T]) Get() []T

Get the current list of values.

func (*List[T]) GetPointer

func (v *List[T]) GetPointer() *[]T

GetPointer returns a pointer to the underlying slice of T.

func (*List[T]) Reset

func (v *List[T]) Reset() error

Reset the list of values to its default (empty) state.

func (*List[T]) Set

func (v *List[T]) Set(s string) error

Set parses the given string, and appends the successfully parsed value to the list. Duplicates are permitted.

func (*List[T]) String

func (v *List[T]) String() string

String returns a string representation of the list of values.

type Rune

type Rune = Value[ffrune]

Rune is a flag value representing a rune. Values are parsed with a custom function that expects a string containing a single valid rune.

type String

type String = Value[string]

String is a flag value representing a string.

type StringList

type StringList = List[string]

StringList a List of strings. Duplicates are permitted.

type StringSet

type StringSet = UniqueList[string]

StringSet is a UniqueList of strings. Duplicates are silently dropped.

type Uint

type Uint = Value[uint]

Uint is a flag value representing a uint. Values are parsed by strconv.ParseUint.

type Uint8

type Uint8 = Value[uint8]

Uint8 is a flag value representing a uint8. Values are parsed by strconv.ParseUint.

type Uint16

type Uint16 = Value[uint16]

Uint16 is a flag value representing a uint16. Values are parsed by strconv.ParseUint.

type Uint32

type Uint32 = Value[uint32]

Uint32 is a flag value representing a uint32. Values are parsed by strconv.ParseUint.

type Uint64

type Uint64 = Value[uint64]

Uint64 is a flag value representing a uint64. Values are parsed by strconv.ParseUint.

type UniqueList

type UniqueList[T comparable] struct {
	// ParseFunc parses a string to the type T. If no ParseFunc is provided, and
	// T is a supported [ValueType], then a default ParseFunc will be assigned
	// lazily. If no ParseFunc is provided, and T is not a supported
	// [ValueType], then most method calls will panic.
	ParseFunc func(string) (T, error)

	// Pointer is the actual slice of type T which is managed and updated by the
	// list. If no Pointer is provided, a new slice is allocated lazily.
	Pointer *[]T

	// StringFunc is used by the String method to transform the underlying slice
	// of T to a string. If no StringFunc is provided, [DefaultStringFunc] is
	// used.
	StringFunc func([]T) string

	// ErrDuplicate is returned by Set when it detects a duplicate value. By
	// default, ErrDuplicate is nil, so duplicate values are silently dropped.
	ErrDuplicate error
	// contains filtered or unexported fields
}

UniqueList is a List that doesn't allow duplicate values.

func NewUniqueList

func NewUniqueList[T ValueType](ptr *[]T) *UniqueList[T]

NewUniqueList returns a unique list of underlying ValueType T, which updates the given pointer ptr when set.

func NewUniqueListParser

func NewUniqueListParser[T comparable](parseFunc func(string) (T, error)) *UniqueList[T]

NewUniqueListParser returns a unique list of any comparable type T that can be parsed from a string.

This constructor is intended as a convenience function for tests; consumers who want to provide a parser are probably better served by constructing a unique list directly, so that they can also provide other fields in a single motion.

func (*UniqueList[T]) Get

func (v *UniqueList[T]) Get() []T

Get the current list of values.

func (*UniqueList[T]) GetPointer

func (v *UniqueList[T]) GetPointer() *[]T

GetPointer returns a pointer to the underlying slice of T.

func (*UniqueList[T]) Reset

func (v *UniqueList[T]) Reset() error

Reset the list of values to its default (empty) state.

func (*UniqueList[T]) Set

func (v *UniqueList[T]) Set(s string) error

Set parses the given string, and appends the successfully parsed value to the list. If the value already exists in the list, Set returns the UniqueList's ErrDuplicate field, which is nil by default.

func (*UniqueList[T]) String

func (v *UniqueList[T]) String() string

String returns a string representation of the list of values.

type Value

type Value[T any] struct {
	// ParseFunc parses a string to the type T. If no ParseFunc is provided, and
	// T is a supported [ValueType], then a default ParseFunc will be assigned
	// lazily. If no ParseFunc is provided, and T is not a supported
	// [ValueType], then most method calls will panic.
	ParseFunc func(string) (T, error)

	// Pointer is the actual instance of the type T which is managed and updated
	// by the value. If no Pointer is provided, a new T is allocated lazily. For
	// this reason, callers should generally access the pointer via GetPointer,
	// rather than reading the field directly.
	Pointer *T

	// Default value, which is the zero value of the type T by default.
	Default T
	// contains filtered or unexported fields
}

Value is a generic flag.Value that can be set from a string.

Most consumers should probably not need to use a value directly, and can instead use one of the specific value types defined by this package, like String or Duration.

func NewValue

func NewValue[T ValueType](ptr *T) *Value[T]

NewValue returns a Value of underlying ValueType T, which updates the given pointer ptr when set, and which has a default value of the zero value of the type T.

func NewValueDefault

func NewValueDefault[T ValueType](ptr *T, def T) *Value[T]

NewValueDefault returns a value of underlying ValueType T, which updates the given pointer ptr when set, and which has the given default value def.

func NewValueParser

func NewValueParser[T any](parseFunc func(string) (T, error)) *Value[T]

NewValueParser returns a value for any type T that can be parsed from a string.

This constructor is intended as a convenience function for tests; consumers who want to provide a parser are probably better served by constructing a value directly, so that they can also provide other fields in a single motion.

func (*Value[T]) Get

func (v *Value[T]) Get() T

Get the current value.

func (*Value[T]) GetPointer

func (v *Value[T]) GetPointer() *T

GetPointer returns a pointer to the underlying instance of type T which is managed by this value.

func (Value[T]) IsBoolFlag

func (v Value[T]) IsBoolFlag() bool

IsBoolFlag returns true if the underlying type T is bool.

func (Value[T]) IsSet

func (v Value[T]) IsSet() bool

IsSet returns true if the value has been explicitly set.

func (*Value[T]) Reset

func (v *Value[T]) Reset() error

Reset the value to its default state.

func (*Value[T]) Set

func (v *Value[T]) Set(s string) error

Set the value by parsing the given string.

func (*Value[T]) String

func (v *Value[T]) String() string

String returns a string representation of the value returned by Get.

type ValueType

type ValueType interface {
	bool | int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | float32 | float64 | string | complex64 | complex128 | time.Duration | ffbyte | ffrune
}

ValueType is a generic type constraint for a specific set of primitive types that are natively supported by this package. Each of them has a default parser, which will be used if a parser is not explicitly provided by the user. This permits the zero value of corresponding generic types to be useful, which in turn allows this package to provide common and useful types like Bool, String, StringSet, etc.

Jump to

Keyboard shortcuts

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