value

package
v0.0.11 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2022 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrIncompatibleInterface = errors.New("could not decode interface into Value")

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

func StringEnum added in v0.0.3

func StringEnum(choices ...string) EmptyConstructor

StringEnum acts just like a string, except it only lets the user update from the choices provided when creating the EmptyConstructor for it.

type TypeInfo added in v0.0.3

type TypeInfo int64
const (
	TypeInfoScalar TypeInfo = iota + 1
	TypeInfoSlice
	TypeInfoMap
)

These constants describe the container type of a Value.

type Value

type Value interface {

	// 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{}

	// 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

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

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

	// 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, ...).

func Bool added in v0.0.5

func Bool() (Value, error)

Bool is updated from "true" or "false"

func Duration added in v0.0.6

func Duration() (Value, error)

Duration is updateable from a string parsed with https://pkg.go.dev/time#ParseDuration

func Int

func Int() (Value, error)

Int is updateable from a float or int. If a float is passed, it will be truncated. Example: 4.5 -> 4, 3.99 -> 3

func IntSlice

func IntSlice() (Value, error)

IntSlice is updateable from a float or int. If a float is passed, it will be truncated. Example: 4.5 -> 4, 3.99 -> 3

func Path

func Path() (Value, error)

Path autoexpands ~ when updated and otherwise behaves like a string

func PathSlice

func PathSlice() (Value, error)

PathSlice autoexpands ~ when updated and otherwise behaves like a []string

func String

func String() (Value, error)

String accepts a string from a user. Pretty self explanatory.

func StringSlice

func StringSlice() (Value, error)

StringSlice accepts a string from a user and adds it to a slice. Pretty self explanatory.

Jump to

Keyboard shortcuts

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