Documentation
¶
Overview ¶
Package options provides a flexible framework for defining and managing configuration options for application components in the Happy SDK or custom libraries. It supports type-safe option storage, validation, and sealing, with features like readonly options, default values, and prefix-based extraction. Options are backed by vars.Map for thread-safe access and integrate with application components via named option sets.
Index ¶
- Constants
- Variables
- type Arg
- type Flag
- type Flags
- type Option
- func (o Option) Default() Value
- func (o Option) Description() string
- func (o Option) HasFlag(flag Flag) bool
- func (o Option) IsSet() bool
- func (o Option) Key() string
- func (o Option) ReadOnly() bool
- func (o Option) String() string
- func (o Option) Value() Value
- func (o Option) Variable() vars.Variable
- type OptionSpec
- type Options
- func (opts *Options) Accepts(key string) bool
- func (opts *Options) All() iter.Seq[Option]
- func (opts *Options) Describe(key string) string
- func (opts *Options) Get(key string) Option
- func (opts *Options) IsSet(key string) bool
- func (opts *Options) Len() int
- func (opts *Options) Load(key string) (opt Option, loaded bool)
- func (opts *Options) Name() string
- func (opts *Options) Range(cb func(opt Option) bool)
- func (opts *Options) Set(key string, val any) error
- func (opts *Options) String() string
- type Spec
- func (spec *Spec) Accepts(key string) bool
- func (spec *Spec) Add(opts ...*OptionSpec) error
- func (spec *Spec) AllowWildcard()
- func (spec *Spec) Extend(others ...*Spec) error
- func (spec *Spec) Get(key string) Option
- func (spec *Spec) Seal() (*Options, error)
- func (spec *Spec) Set(key string, val any) error
- type Value
- type ValueParser
- type ValueValidator
Constants ¶
const UndefinedOptionDescription = "undefined"
Variables ¶
var ( ErrOptions = errors.New("option error") ErrOption = fmt.Errorf("%w", ErrOptions) ErrOptionNotExists = fmt.Errorf("%w: no such option", ErrOption) ErrOptionReadOnly = fmt.Errorf("%w: readonly option", ErrOption) ErrOptionOnce = fmt.Errorf("%w: already set", ErrOption) ErrOptionValidation = fmt.Errorf("%w: validation failed", ErrOption) )
var OptionNoopValueValidator = func(opt Option, newval Value) error { return nil }
OptionNoopValueValidator provides no validation for option value.
var OptionValidatorNotEmpty = func(opt Option, newval Value) error { if opt.Value().Empty() { return fmt.Errorf("%w: %s value can not be empty", ErrOption, opt.Key()) } return nil }
OptionValidatorNotEmpty validates that option value is not empty.
Functions ¶
This section is empty.
Types ¶
type Flag ¶ added in v0.6.0
type Flag uint
Flags is a bitmask for option kind. It defines option behavior.
type Option ¶
type Option struct {
// contains filtered or unexported fields
}
func (Option) Description ¶ added in v0.6.0
Description returns the description of the option.
type OptionSpec ¶
type OptionSpec struct {
// contains filtered or unexported fields
}
func NewOption ¶
func NewOption(key string, defval any) *OptionSpec
func (*OptionSpec) Description ¶ added in v0.6.0
func (o *OptionSpec) Description(desc string) *OptionSpec
func (*OptionSpec) Flags ¶ added in v0.6.0
func (o *OptionSpec) Flags(flags Flags) *OptionSpec
func (*OptionSpec) Parser ¶ added in v0.6.0
func (o *OptionSpec) Parser(parser ValueParser) *OptionSpec
func (*OptionSpec) Validator ¶ added in v0.6.0
func (o *OptionSpec) Validator(validator ValueValidator) *OptionSpec
type Options ¶
type Options struct {
// contains filtered or unexported fields
}
func (*Options) Get ¶
Get returns the Option stored in the Options for a key, It returns empty Option if key is not found. Use Load to check if Option by key was found in the Options.
func (*Options) Load ¶
Load returns the Option stored in the Options for a key, The loaded result indicates whether Option by key was found in the Options.
type Spec ¶ added in v0.2.0
type Spec struct {
// contains filtered or unexported fields
}
func (*Spec) Add ¶ added in v0.6.0
func (spec *Spec) Add(opts ...*OptionSpec) error
func (*Spec) AllowWildcard ¶ added in v0.6.0
func (spec *Spec) AllowWildcard()