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 ¶
- Variables
- func ValidateBool(value string) error
- func ValidateDuration(value string) error
- func ValidateFloat(bitsize int) func(string) error
- func ValidateIP(value string) error
- func ValidateInt(bitsize int) func(string) error
- func ValidateIntRange(min, max int64) func(value string) error
- func ValidateSlice(validator ParamValidator) func(value string) error
- func ValidateUint(bitsize int) func(string) error
- func ValidateUintRange(min, max uint64) func(value string) error
- type Collection
- type DescCollection
- type Param
- func (p *Param) AsAny() any
- func (p *Param) AsBool() bool
- func (p *Param) AsBytes() []byte
- func (p *Param) AsDuration() time.Duration
- func (p *Param) AsFloat32() float32
- func (p *Param) AsFloat64() float64
- func (p *Param) AsIP() net.IP
- func (p *Param) AsInt() int
- func (p *Param) AsInt16() int16
- func (p *Param) AsInt32() int32
- func (p *Param) AsInt64() int64
- func (p *Param) AsInt64Slice() []int64
- func (p *Param) AsInt8() int8
- func (p *Param) AsString() string
- func (p *Param) AsStringSlice() []string
- func (p *Param) AsUint() uint
- func (p *Param) AsUint16() uint16
- func (p *Param) AsUint16Slice() []uint16
- func (p *Param) AsUint32() uint32
- func (p *Param) AsUint64() uint64
- func (p *Param) AsUint64Slice() []uint64
- func (p *Param) AsUint8() uint8
- func (p *Param) IsDefault() bool
- func (p *Param) IsSet() bool
- func (p *Param) Set(val string) error
- func (p *Param) String() string
- type ParamDesc
- type ParamDescs
- type ParamValidator
- type Params
- func (p *Params) Add(other ...*Param)
- func (p *Params) AddKeyValuePair(key, value string)
- func (p *Params) CopyFromMap(source map[string]string, prefix string) error
- func (p *Params) CopyToMap(target map[string]string, prefix string)
- func (p *Params) Get(key string) *Param
- func (p *Params) ParamMap() (res map[string]string)
- func (p *Params) Set(key, val string) error
- func (p *Params) ValidateStringMap(cfg map[string]string) error
- type TypeHint
- type ValueHint
Constants ¶
This section is empty.
Variables ¶
var ErrNotFound = errors.New("not found")
Functions ¶
func ValidateBool ¶
func ValidateDuration ¶ added in v0.16.0
func ValidateFloat ¶ added in v0.22.0
func ValidateIP ¶ added in v0.18.0
func ValidateInt ¶
func ValidateIntRange ¶
func ValidateSlice ¶
func ValidateSlice(validator ParamValidator) func(value string) error
func ValidateUint ¶
func ValidateUintRange ¶
Types ¶
type Collection ¶
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
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) AsDuration ¶ added in v0.16.0
func (*Param) AsInt64Slice ¶
func (*Param) AsStringSlice ¶
func (*Param) AsUint16Slice ¶
AsUint16Slice is useful for handling network ports.
func (*Param) AsUint64Slice ¶
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 ¶
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 ¶
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 Params ¶
type Params []*Param
func (*Params) AddKeyValuePair ¶
func (*Params) CopyFromMap ¶
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" )