value

package
v0.0.16 Latest Latest
Warning

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

Go to latest
Published: Jan 2, 2023 License: MIT Imports: 1 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EmptyConstructor

type EmptyConstructor func() (Value, error)

EmptyConstructur just builds a new value. Useful to create new values as well as initialize them

type ErrInvalidChoice added in v0.0.16

type ErrInvalidChoice[T comparable] struct {
	Choices []T
}

func (ErrInvalidChoice[T]) Error added in v0.0.16

func (e ErrInvalidChoice[T]) Error() string

type TypeContainer added in v0.0.14

type TypeContainer int64
const (
	TypeContainerScalar TypeContainer = iota + 1
	TypeContainerSlice
	TypeContainerMap
)

These constants describe the container type of a Value.

type Value

type Value interface {

	// Choices for this value to contain (represented as strings)
	Choices() []string

	// DefaultString returns the default underlying value (represented as a string)
	DefaultString() string

	// DefaultStringSlice returns the default underlying value for slice values and nil for others
	DefaultStringSlice() []string

	// Description of the type. useful for help messages. Should not be used as an ID.
	Description() string

	// Get returns the underlying value. It's meant to be type asserted against
	// Example: myInt := v.(int)
	Get() interface{}

	// HasDefault returns true if this value has a default
	HasDefault() bool

	// ReplaceFromInterface replaces a value with one found in an interface.
	// Useful to update a Value from a config.
	ReplaceFromInterface(interface{}) error

	// String returns a string ready to be printed!
	String() string

	// StringSlice returns a []string ready to be printed for slice values and nil for others
	StringSlice() []string

	// TypeContainer specifies whether what "overall" type of value this is - scalar, slice, etc.
	TypeContainer() TypeContainer

	// Update appends to container type Values from a string (useful for CLI flags, env vars, default values)
	// and replaces scalar Values
	Update(string) error

	// UpdateFromDefault updates the Value from a pre-set default, if one exists. use HasDefault to check whether a default exists
	UpdateFromDefault()

	// UpdateFromInterface updates a container type Value from an interface (useful for configs)
	// and replaces scalar values (for scalar values, UpdateFromInterface is the same as ReplaceFromInterface).
	// Note that UpdateFromInterface must be called with the "contained" type for container type Values
	// For example, the StringSlice.UpdateFromInterface
	// must be called with a string, not a []string
	// It returns ErrIncompatibleInterface if the interface can't be decoded
	UpdateFromInterface(interface{}) error
}

Value is a "generic" type to store different types into flags Inspired by https://golang.org/src/flag/flag.go . There are two underlying "type" families designed to fit in Value: scalar types (Int, String, ...) and container types (IntSlice, StringMap, ...).

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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