options

package module
v0.8.2 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2025 License: Apache-2.0 Imports: 7 Imported by: 0

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

View Source
const UndefinedOptionDescription = "undefined"

Variables

View Source
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)
)
View Source
var OptionNoopValueValidator = func(opt Option, newval Value) error {
	return nil
}

OptionNoopValueValidator provides no validation for option value.

View Source
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 Arg

type Arg struct {
	// contains filtered or unexported fields
}

func NewArg

func NewArg(key string, value any) *Arg

func (*Arg) Key

func (arg *Arg) Key() string

func (*Arg) Value

func (arg *Arg) Value() any

type Flag added in v0.6.0

type Flag uint

Flags is a bitmask for option kind. It defines option behavior.

const (
	Mutable Flag = 1 << iota
	ReadOnly
	Once
)

type Flags added in v0.6.0

type Flags = Flag

type Option

type Option struct {
	// contains filtered or unexported fields
}

func (Option) Default added in v0.6.0

func (o Option) Default() Value

func (Option) Description added in v0.6.0

func (o Option) Description() string

Description returns the description of the option.

func (Option) HasFlag added in v0.6.0

func (o Option) HasFlag(flag Flag) bool

func (Option) IsSet added in v0.6.0

func (o Option) IsSet() bool

func (Option) Key added in v0.6.0

func (o Option) Key() string

Key returns the key of the option.

func (Option) ReadOnly

func (o Option) ReadOnly() bool

ReadOnly returns true if the option is read-only.

func (Option) String added in v0.6.0

func (o Option) String() string

func (Option) Value

func (o Option) Value() Value

Value returns the value of the option.

func (Option) Variable added in v0.6.0

func (o Option) Variable() vars.Variable

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) Accepts

func (opts *Options) Accepts(key string) bool

Accepts reports whether given option key is accepted by Options.

func (*Options) All added in v0.6.0

func (opts *Options) All() iter.Seq[Option]

func (*Options) Describe

func (opts *Options) Describe(key string) string

func (*Options) Get

func (opts *Options) Get(key string) Option

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) IsSet added in v0.6.0

func (opts *Options) IsSet(key string) bool

func (*Options) Len

func (opts *Options) Len() int

func (*Options) Load

func (opts *Options) Load(key string) (opt Option, loaded bool)

Load returns the Option stored in the Options for a key, The loaded result indicates whether Option by key was found in the Options.

func (*Options) Name

func (opts *Options) Name() string

Name returns name for this Option collection. It is alias for String().

func (*Options) Range

func (opts *Options) Range(cb func(opt Option) bool)

func (*Options) Set

func (opts *Options) Set(key string, val any) error

func (*Options) String added in v0.5.0

func (opts *Options) String() string

String returns name and satisfies fmt.Stringer interface.

type Spec added in v0.2.0

type Spec struct {
	// contains filtered or unexported fields
}

func New

func New(name string, opts ...*OptionSpec) (spec *Spec, err error)

func (*Spec) Accepts added in v0.6.0

func (spec *Spec) Accepts(key string) bool

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()

func (*Spec) Extend added in v0.6.0

func (spec *Spec) Extend(others ...*Spec) error

func (*Spec) Get added in v0.6.0

func (spec *Spec) Get(key string) Option

func (*Spec) Seal added in v0.6.0

func (spec *Spec) Seal() (*Options, error)

func (*Spec) Set added in v0.6.0

func (spec *Spec) Set(key string, val any) error

type Value added in v0.6.0

type Value = vars.Value

func NewValue added in v0.6.0

func NewValue(v any) (Value, error)

type ValueParser added in v0.6.0

type ValueParser func(opt Option, newval Value) (parsed Value, err error)

type ValueValidator

type ValueValidator func(opt Option) error

Jump to

Keyboard shortcuts

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