params

package
v0.27.0 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2024 License: Apache-2.0 Imports: 12 Imported by: 3

Documentation

Overview

Package params provides a generic way to describe parameters used by gadgets, operators and runtimes including validation. They can easily be serialized and handed over to different frameworks like cobra for use in CLI or a webinterface using JSON.

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFound = errors.New("not found")

Functions

func ValidateBool

func ValidateBool(value string) error

func ValidateDuration added in v0.16.0

func ValidateDuration(value string) error

func ValidateFloat added in v0.22.0

func ValidateFloat(bitsize int) func(string) error

func ValidateIP added in v0.18.0

func ValidateIP(value string) error

func ValidateInt

func ValidateInt(bitsize int) func(string) error

func ValidateIntRange

func ValidateIntRange(min, max int64) func(value string) error

func ValidateSlice

func ValidateSlice(validator ParamValidator) func(value string) error

func ValidateUint

func ValidateUint(bitsize int) func(string) error

func ValidateUintRange

func ValidateUintRange(min, max uint64) func(value string) error

Types

type Collection

type Collection map[string]*Params

func (Collection) CopyFromMap

func (p Collection) CopyFromMap(source map[string]string, prefix string) error

func (Collection) CopyToMap

func (p Collection) CopyToMap(target map[string]string, prefix string)

func (Collection) Set

func (p Collection) Set(entry, key, val string) error

type DescCollection

type DescCollection map[string]*ParamDescs

func (DescCollection) ToParams

func (p DescCollection) ToParams() Collection

type Param

type Param struct {
	*ParamDesc
	// contains filtered or unexported fields
}

Param holds a ParamDesc but can additionally store a value

func (*Param) AsAny added in v0.22.0

func (p *Param) AsAny() any

AsAny returns the value of the parameter according to its type hint. If there is not any type hint, it returns the value as string.

func (*Param) AsBool

func (p *Param) AsBool() bool

func (*Param) AsBytes added in v0.17.0

func (p *Param) AsBytes() []byte

func (*Param) AsDuration added in v0.16.0

func (p *Param) AsDuration() time.Duration

func (*Param) AsFloat32

func (p *Param) AsFloat32() float32

func (*Param) AsFloat64

func (p *Param) AsFloat64() float64

func (*Param) AsIP added in v0.18.0

func (p *Param) AsIP() net.IP

func (*Param) AsInt

func (p *Param) AsInt() int

func (*Param) AsInt16

func (p *Param) AsInt16() int16

func (*Param) AsInt32

func (p *Param) AsInt32() int32

func (*Param) AsInt64

func (p *Param) AsInt64() int64

func (*Param) AsInt64Slice

func (p *Param) AsInt64Slice() []int64

func (*Param) AsInt8

func (p *Param) AsInt8() int8

func (*Param) AsString

func (p *Param) AsString() string

func (*Param) AsStringSlice

func (p *Param) AsStringSlice() []string

func (*Param) AsUint

func (p *Param) AsUint() uint

func (*Param) AsUint16

func (p *Param) AsUint16() uint16

func (*Param) AsUint16Slice

func (p *Param) AsUint16Slice() []uint16

AsUint16Slice is useful for handling network ports.

func (*Param) AsUint32

func (p *Param) AsUint32() uint32

func (*Param) AsUint64

func (p *Param) AsUint64() uint64

func (*Param) AsUint64Slice

func (p *Param) AsUint64Slice() []uint64

func (*Param) AsUint8

func (p *Param) AsUint8() uint8

func (*Param) IsDefault added in v0.22.0

func (p *Param) IsDefault() bool

func (*Param) IsSet added in v0.22.0

func (p *Param) IsSet() bool

func (*Param) Set

func (p *Param) Set(val string) error

Set validates and sets the new value; it is also a member of the pflag.Value interface, which is used by cobra

func (*Param) String

func (p *Param) String() string

String is a member of the pflag.Value interface, which is used by cobra

type ParamDesc

type ParamDesc struct {
	// Key is the name under which this param is registered; this will also be the key when
	// getting a key/value map
	Key string `json:"key" yaml:"key"`

	// Alias is a shortcut for this parameter, usually a single character used for command line
	// interfaces
	Alias string `json:"alias" yaml:"alias,omitempty"`

	// Title is an optional (pretty) alternative to key and used in user interfaces
	Title string `json:"title" yaml:"title,omitempty"`

	// DefaultValue is the value that will be used if no other value has been assigned
	DefaultValue string `json:"defaultValue" yaml:"defaultValue"`

	// Description holds an optional explanation for this parameter; shown in user interfaces
	Description string `json:"description" yaml:"description"`

	// IsMandatory will be considered when validating; if the param has no value assigned and
	// also no DefaultValue is set, validation will fail
	IsMandatory bool `json:"isMandatory" yaml:"isMandatory,omitempty"`

	// Tags can be used to skip parameters not needed for a specific environment
	Tags []string `json:"tags" yaml:"tags,omitempty"`

	// Validator is an optional function that will be called upon validation; may or may
	// not be called in user interfaces. Setting TypeHint is preferred, but can also be used
	// in combination with the Validator. Example: additionally to setting the TypeHint to
	// TypeInt, the validator could be used to make sure the given int is in a specific range.
	Validator ParamValidator `json:"-" yaml:"-"`

	// TypeHint is the preferred way to set the type of this parameter as it will invoke a
	// matching validator automatically; if unset, a value of "string" is assumed
	TypeHint TypeHint `json:"type" yaml:"type,omitempty"`

	// ValueHint can give a hint on what content is expected here - for example, it can be
	// used to hint that the param expects a kubernetes namespace, a list of nodes and so on.
	// This is helpful for frontends to provide autocompletion/selections or set defaults
	// accordingly.
	ValueHint ValueHint `json:"valueHint" yaml:"valueHint,omitempty"`

	// PossibleValues holds all possible values for this parameter and will be considered
	// when validating
	PossibleValues []string `json:"possibleValues" yaml:"possibleValues,omitempty"`
}

ParamDesc holds parameter information and validators

func (*ParamDesc) GetTitle

func (p *ParamDesc) GetTitle() string

GetTitle returns a human friendly title of the field; if no Title has been specified, the Key will be used with the first letter upper-cased

func (*ParamDesc) IsBoolFlag

func (p *ParamDesc) IsBoolFlag() bool

func (*ParamDesc) ToParam

func (p *ParamDesc) ToParam() *Param

func (*ParamDesc) Type

func (p *ParamDesc) Type() string

Type is a member of the pflag.Value interface, which is used by cobra

func (*ParamDesc) Validate

func (p *ParamDesc) Validate(value string) error

Validate validates a string against the given parameter

type ParamDescs

type ParamDescs []*ParamDesc

func (*ParamDescs) Add

func (p *ParamDescs) Add(other ...*ParamDesc)

func (*ParamDescs) Get

func (p *ParamDescs) Get(key string) *ParamDesc

Get returns the parameter with the given key or nil

func (ParamDescs) ToParams

func (p ParamDescs) ToParams() *Params

type ParamValidator

type ParamValidator func(value string) error

type Params

type Params []*Param

func (*Params) Add

func (p *Params) Add(other ...*Param)

func (*Params) AddKeyValuePair

func (p *Params) AddKeyValuePair(key, value string)

func (*Params) CopyFromMap

func (p *Params) CopyFromMap(source map[string]string, prefix string) error

func (*Params) CopyToMap

func (p *Params) CopyToMap(target map[string]string, prefix string)

func (*Params) Get

func (p *Params) Get(key string) *Param

Get returns the parameter with the given key or nil

func (*Params) ParamMap

func (p *Params) ParamMap() (res map[string]string)

func (*Params) Set

func (p *Params) Set(key, val string) error

func (*Params) ValidateStringMap

func (p *Params) ValidateStringMap(cfg map[string]string) error

type TypeHint

type TypeHint string
const (
	TypeUnknown  TypeHint = ""
	TypeBool     TypeHint = "bool"
	TypeString   TypeHint = "string"
	TypeBytes    TypeHint = "bytes"
	TypeInt      TypeHint = "int"
	TypeInt8     TypeHint = "int8"
	TypeInt16    TypeHint = "int16"
	TypeInt32    TypeHint = "int32"
	TypeInt64    TypeHint = "int64"
	TypeUint     TypeHint = "uint"
	TypeUint8    TypeHint = "uint8"
	TypeUint16   TypeHint = "uint16"
	TypeUint32   TypeHint = "uint32"
	TypeUint64   TypeHint = "uint64"
	TypeFloat32  TypeHint = "float32"
	TypeFloat64  TypeHint = "float64"
	TypeDuration TypeHint = "duration"
	TypeIP       TypeHint = "ip"
)

type ValueHint added in v0.15.0

type ValueHint string

Jump to

Keyboard shortcuts

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