options

package
v0.48.3 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package options implements config options. This package is currently a bit of an experiment. Objectives:

  • Options are key-value pairs.
  • Options can come from default config, individual source config, and flags.
  • Support the ability to edit config in $EDITOR, providing contextual information about the Opt instance.
  • Values are strongly typed (e.g. int, time.Duration)
  • An individual Opt instance can be specified near where it is used.
  • New types of Opt can be defined, near where they are used.

It is noted that these requirements could probably largely be met using packages such as spf13/viper. Again, this is largely an experiment.

Index

Constants

View Source
const (
	// TagSource indicates that an Opt with this tag applies to source config
	// (as opposed to applying only to base config). An opt with this tag
	// typically applies to both base and source config.
	TagSource = "source"

	// TagTuning indicates that the Opt is related to tuning behavior.
	TagTuning = "tuning"

	// TagSQL indicates that the Opt is related to SQL interaction.
	TagSQL = "sql"

	// TagOutput indicates the Opt is related to output/formatting.
	TagOutput = "output"

	// TagIngestMutate indicates the Opt may result in mutated data, particularly
	// during ingestion. This tag is significant in that its value may affect
	// data realization, and thus affect program aspects such as caching behavior.
	TagIngestMutate = "mutate"
)

Variables

This section is empty.

Functions

func NewContext added in v0.34.0

func NewContext(ctx context.Context, o Options) context.Context

NewContext returns a context that contains the given Options. Use FromContext to retrieve the Options.

Types

type BaseOpt added in v0.34.0

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

BaseOpt is a partial implementation of options.Opt that concrete types can build on.

func NewBaseOpt added in v0.34.0

func NewBaseOpt(key string, flag *Flag, usage, help string, tags ...string) BaseOpt

NewBaseOpt returns a new BaseOpt.

func (BaseOpt) DefaultAny added in v0.34.0

func (op BaseOpt) DefaultAny() any

DefaultAny implements options.Opt.

func (BaseOpt) Flag added in v0.36.0

func (op BaseOpt) Flag() Flag

Flag implements options.Opt.

func (BaseOpt) GetAny added in v0.34.0

func (op BaseOpt) GetAny(_ Options) any

GetAny is required by options.Opt. It needs to be implemented by the concrete type.

func (BaseOpt) HasTag added in v0.41.0

func (op BaseOpt) HasTag(tag string) bool

HasTag implements options.Opt.

func (BaseOpt) Help added in v0.34.0

func (op BaseOpt) Help() string

Help implements options.Opt.

func (BaseOpt) IsSet added in v0.34.0

func (op BaseOpt) IsSet(o Options) bool

IsSet implements options.Opt.

func (BaseOpt) Key added in v0.34.0

func (op BaseOpt) Key() string

Key implements options.Opt.

func (BaseOpt) Process added in v0.34.0

func (op BaseOpt) Process(o Options) (Options, error)

Process implements options.Opt.

func (BaseOpt) String added in v0.34.0

func (op BaseOpt) String() string

String implements options.Opt.

func (BaseOpt) Tags added in v0.34.0

func (op BaseOpt) Tags() []string

Tags implements options.Opt.

func (BaseOpt) Usage added in v0.34.0

func (op BaseOpt) Usage() string

Usage implements options.Opt.

type Bool added in v0.34.0

type Bool struct {
	BaseOpt
	// contains filtered or unexported fields
}

Bool is an options.Opt for type bool.

func NewBool added in v0.34.0

func NewBool(key string, flag *Flag, defaultVal bool, usage, help string, tags ...string) Bool

NewBool returns an options.Bool instance. If arg flag is non-nil and [Flag.Invert] is true, the flag's boolean value is inverted to set the option. For example, if [Opt.Key] is progress, and [Flag.Name] is "--no-progress", then [Flag.Invert] should be true.

func (Bool) Default added in v0.34.0

func (op Bool) Default() bool

Default returns the default value of op.

func (Bool) DefaultAny added in v0.34.0

func (op Bool) DefaultAny() any

DefaultAny implements options.Opt.

func (Bool) Get added in v0.34.0

func (op Bool) Get(o Options) bool

Get returns op's value in o. If o is nil, or no value is set, op's default value is returned.

func (Bool) GetAny added in v0.34.0

func (op Bool) GetAny(opts Options) any

GetAny implements options.Opt.

func (Bool) Process added in v0.34.0

func (op Bool) Process(o Options) (Options, error)

Process implements options.Opt. It converts matching string values in o into bool. If no match found, the input arg is returned unchanged. Otherwise, a clone is returned.

type Duration added in v0.34.0

type Duration struct {
	BaseOpt
	// contains filtered or unexported fields
}

Duration is an options.Opt for time.Duration.

func NewDuration added in v0.34.0

func NewDuration(key string, flag *Flag, defaultVal time.Duration,
	usage, help string, tags ...string,
) Duration

NewDuration returns an options.Duration instance.

func (Duration) Default added in v0.36.0

func (op Duration) Default() time.Duration

Default returns the default value of op.

func (Duration) DefaultAny added in v0.34.0

func (op Duration) DefaultAny() any

DefaultAny implements options.Opt.

func (Duration) Get added in v0.34.0

func (op Duration) Get(o Options) time.Duration

Get returns op's value in o. If o is nil, or no value is set, op's default value is returned.

func (Duration) GetAny added in v0.34.0

func (op Duration) GetAny(o Options) any

GetAny implements options.Opt.

func (Duration) Process added in v0.34.0

func (op Duration) Process(o Options) (Options, error)

Process implements options.Opt. It converts matching string values in o into time.Duration. If no match found, the input arg is returned unchanged. Otherwise, a clone is returned.

type Flag added in v0.47.4

type Flag struct {
	// Name is the flag name to use. Defaults to [Opt.Key].
	Name string

	// Usage is the flag's usage text. Defaults to [Opt.Usage], but can be
	// overridden if the flag usage text should differ from the [Opt] usage text.
	// This is typically only the case when [Flag.Invert] is true.
	Usage string

	// Short is the short flag name, e.g. 'v' for "verbose". The zero value
	// indicates no short name.
	Short rune

	// Invert indicates that the flag's boolean value is inverted vs the flag
	// name. For example, if [Opt.Key] is "progress", but [Flag.Name] is
	// "no-progress", then [Flag.Invert] should be true. This field is ignored for
	// non-boolean [Opt] types.
	Invert bool
}

Flag describe an Opt's behavior as a command-line flag. It can be passed to the "NewX" Opt constructor functions, e.g. NewBool, to override the Opt's flag configuration. The computed Flag value is available via Opt.Flag. It is common to pass a nil *Flag to the Opt constructors; the value returned by Opt.Flag will be appropriately populated with default values.

type Int added in v0.34.0

type Int struct {
	BaseOpt
	// contains filtered or unexported fields
}

Int is an options.Opt for type int.

func NewInt added in v0.34.0

func NewInt(key string, flag *Flag, defaultVal int, usage, help string, tags ...string) Int

NewInt returns an options.Int instance.

func (Int) Default added in v0.34.0

func (op Int) Default() int

Default returns the default value of op.

func (Int) DefaultAny added in v0.34.0

func (op Int) DefaultAny() any

DefaultAny implements options.Opt.

func (Int) Get added in v0.34.0

func (op Int) Get(o Options) int

Get returns op's value in o. If o is nil, or no value is set, op's default value is returned.

func (Int) GetAny added in v0.34.0

func (op Int) GetAny(o Options) any

GetAny implements options.Opt.

func (Int) Process added in v0.34.0

func (op Int) Process(o Options) (Options, error)

Process implements options.Opt. It converts matching values in o into bool. If no match found, the input arg is returned unchanged. Otherwise, a clone is returned.

type Opt added in v0.34.0

type Opt interface {
	// Key returns the Opt key, such as "ping.timeout".
	Key() string

	// Flag is the computed flag config for the Opt.
	Flag() Flag

	// Usage is a one-line description of the Opt. Additional detail can be
	// found in Help. It is typically the case that [Flag.Usage] is the same value
	// as Usage, but it can be overridden if the flag usage text should differ
	// from the Opt usage text.
	Usage() string

	// Help returns the Opt's help text, which typically provides more detail
	// than Usage. The text must be plaintext (not markdown). Linebreaks are
	// recommended at 100 chars.
	Help() string

	// String returns a log/debug-friendly representation.
	String() string

	// IsSet returns true if this Opt is set in o.
	IsSet(o Options) bool

	// GetAny returns the value of this Opt in o. Generally, prefer
	// use of the concrete strongly-typed Get method. If o is nil or
	// empty, or the Opt is not in o, the Opt's default value is
	// returned.
	GetAny(o Options) any

	// DefaultAny returns the default value of this Opt. Generally, prefer
	// use of the concrete strongly-typed Default method.
	DefaultAny() any

	// Tags returns any tags on this Opt instance. For example, an Opt might
	// have tags [source, csv].
	Tags() []string

	// HasTag returns true if the result of Opt.Tags contains tag.
	HasTag(tag string) bool

	// Process processes o. The returned Options may be a new instance,
	// with mutated values. This is typ
	Process(o Options) (Options, error)
}

Opt is an option type. Concrete impls exist for various types, such as options.Int or options.Duration. Each concrete type must implement a "Get(o Options) T" method that returns the appropriate type T. The value returned by that Get method will be the same as that returned by the generic Opt.GetAny method. The impl should also provide a NewT(...) T constructor. The caller typically registers the new Opt in a options.Registry via Registry.Add.

An impl should implement the Process method to appropriately munge the backing value. For example, options.Duration converts a string such as "1m30s" into a time.Duration.

type Options

type Options map[string]any

Options is a map of Opt.Key to a value.

func DeleteNil added in v0.34.0

func DeleteNil(o Options) Options

DeleteNil returns a new Options that has any nil values removed.

func Effective added in v0.34.0

func Effective(o Options, opts ...Opt) Options

Effective returns a new Options containing the effective values of each Opt. That is to say, the returned Options contains either the actual value of each Opt in o, or the default value for that Opt, but it will not contain values for any Opt not in opts.

func FromContext added in v0.34.0

func FromContext(ctx context.Context) Options

FromContext returns the Options stored in ctx by NewContext, or nil if no such Options.

func Merge added in v0.34.0

func Merge(base Options, overlays ...Options) Options

Merge overlays each of overlays onto base, returning a new Options. It is acceptable for base to be nil.

func (Options) Clone

func (o Options) Clone() Options

Clone clones o.

func (Options) IsSet added in v0.34.0

func (o Options) IsSet(opt Opt) bool

IsSet returns true if opt is set on o.

func (Options) Keys added in v0.34.0

func (o Options) Keys() []string

Keys returns the sorted set of keys in o.

func (Options) LogValue added in v0.34.0

func (o Options) LogValue() slog.Value

LogValue implements slog.LogValuer.

type Processor added in v0.34.0

type Processor interface {
	// Process processes o. The returned Options may be a new instance,
	// with mutated values.
	Process(o Options) (Options, error)
}

Processor performs processing on o.

type Registry added in v0.34.0

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

Registry is a registry of Opt instances.

func (*Registry) Add added in v0.34.0

func (r *Registry) Add(opts ...Opt)

Add adds opts to r. It panics if any element of opts is already registered.

func (*Registry) Get added in v0.34.0

func (r *Registry) Get(key string) Opt

Get returns the Opt registered in r using key, or nil.

func (*Registry) Keys added in v0.34.0

func (r *Registry) Keys() []string

Keys returns the keys of each Opt in r.

func (*Registry) LogValue added in v0.34.0

func (r *Registry) LogValue() slog.Value

LogValue implements slog.LogValuer.

func (*Registry) Opts added in v0.34.0

func (r *Registry) Opts() []Opt

Opts returns a new slice containing each Opt registered with r.

func (*Registry) Process added in v0.34.0

func (r *Registry) Process(o Options) (Options, error)

Process processes o, returning a new Options. Process should be invoked after the Options has been loaded from config, but before it is used by the program. Process iterates over the registered Opts, and invokes Process for each Opt that implements Processor. This facilitates munging of backing values, e.g. for options.Duration, a string "1m30s" is converted to a time.Duration.

func (*Registry) Visit added in v0.34.0

func (r *Registry) Visit(fn func(opt Opt) error) error

Visit visits each Opt in r. Be careful with concurrent access via this method.

type String added in v0.34.0

type String struct {
	BaseOpt
	// contains filtered or unexported fields
}

String is an options.Opt for type string.

func NewString added in v0.34.0

func NewString(key string, flag *Flag, defaultVal string, validFn func(string) error,
	usage, help string, tags ...string,
) String

NewString returns an options.String instance. If validFn is non-nil, it is called by String.Process.

func (String) Default added in v0.34.0

func (op String) Default() string

Default returns the default value of op.

func (String) DefaultAny added in v0.34.0

func (op String) DefaultAny() any

DefaultAny implements options.Opt.

func (String) Get added in v0.34.0

func (op String) Get(o Options) string

Get returns op's value in o. If o is nil, or no value is set, op's default value is returned.

func (String) GetAny added in v0.34.0

func (op String) GetAny(o Options) any

GetAny implements options.Opt.

func (String) Process added in v0.40.0

func (op String) Process(o Options) (Options, error)

Process implements options.Opt. If the String was constructed with validator function, it is invoked on the value of the Opt, if it is set. Otherwise the method is no-op.

Jump to

Keyboard shortcuts

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