options

package
v0.23.1 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2024 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package oprions provides a way to define and manage options for application components.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrOption           = errors.New("option error")
	ErrOptionReadOnly   = fmt.Errorf("%w: readonly option", ErrOption)
	ErrOptionValidation = fmt.Errorf("%w: validation failed", ErrOption)
)
View Source
var NoopValueValidator = func(key string, val vars.Value) error {
	return nil
}

Noopvalidator provides no validation for option value.

View Source
var OptionValidatorNotEmpty = func(key string, val vars.Value) error {
	if val.Len() == 0 {
		return fmt.Errorf("%w: %s value can not be empty", ErrOption, key)
	}
	return nil
}

Functions

func MergeOptions

func MergeOptions(dest, src *Options) error

Types

type Arg

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

func NewArg

func NewArg(key string, value any) Arg

NewArg returns new Arg with given key and value. It is used to pass options to application components which accept options.

func (Arg) Key

func (a Arg) Key() string

func (Arg) Value

func (a Arg) Value() any

type Kind

type Kind uint

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

const (
	KindRuntime Kind = 1 << iota
	KindReadOnly
	KindSettings
	KindConfig
)

type Option

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

func (Option) Kind

func (o Option) Kind() vars.Kind

func (Option) Name

func (o Option) Name() string

func (Option) ReadOnly

func (o Option) ReadOnly() bool

func (Option) Value

func (o Option) Value() vars.Value

type OptionSpec

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

OptionSpec holds specification for given option.

func NewOption

func NewOption(key string, dval any, desc string, kind Kind, vfunc ValueValidator) OptionSpec

Option returns new option specificationwith given key, value, description and validator.

type Options

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

Options is general collection of options attached to specific application component.

func New

func New(name string, specs []OptionSpec) (*Options, error)

New returns new named options set.

func (*Options) Accepts

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

Accepts reports whether given option key is accepted by Options.

func (*Options) Add

func (opts *Options) Add(spec OptionSpec) error

func (*Options) Describe

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

func (*Options) Get

func (opts *Options) Get(key string) vars.Variable

func (*Options) Has

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

Has reports whether options has given key

func (*Options) Len

func (opts *Options) Len() int

func (*Options) Load

func (opts *Options) Load(key string) (vars.Variable, bool)

func (*Options) Name

func (opts *Options) Name() string

Name is name for this Option collection.

func (*Options) Range

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

func (*Options) Seal

func (opts *Options) Seal() error

Seal ensures that all required options are set. It will set default values for options that are not set. It will return error if options are already sealed.

func (*Options) Set

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

func (*Options) WithPrefix

func (opts *Options) WithPrefix(prefix string) *vars.Map

type ValueValidator

type ValueValidator func(key string, val vars.Value) error

OptionValueValidator is callback function to validate given value, it recieves copy of value for validation. It MUST return error if validation fails, returned boolean indicates shoulkd that option be marked as radonly if validation succeeds.

Jump to

Keyboard shortcuts

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