value

package
v0.19.4 Latest Latest
Warning

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

Go to latest
Published: May 8, 2024 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNoFlagDefined = vterrors.New(vtrpc.Code_INVALID_ARGUMENT, "flag not defined")

ErrNoFlagDefined is returned when a Value has a FlagName set, but the given FlagSet does not define a flag with that name.

Functions

func BindFlags

func BindFlags(fs *pflag.FlagSet, values ...Registerable)

BindFlags creates bindings between each value's registry and the given flag set. This function will panic if any of the values defines a flag that does not exist in the flag set.

Types

type Base

type Base[T any] struct {
	KeyName    string
	DefaultVal T

	GetFunc      func(v *viper.Viper) func(key string) T
	BoundGetFunc func(key string) T

	Aliases  []string
	FlagName string
	EnvVars  []string
}

Base is the base functionality shared by Static and Dynamic values. It implements viperutil.Value.

func (*Base[T]) Default

func (val *Base[T]) Default() T

func (*Base[T]) Flag

func (val *Base[T]) Flag(fs *pflag.FlagSet) (*pflag.Flag, error)

Flag is part of the Registerable interface. If the given flag set has a flag with the name of this value's configured flag, that flag is returned, along with a nil error. If no flag exists on the flag set with that name, an error is returned.

If the value is not configured to correspond to a flag (FlagName == ""), then (nil, nil) is returned.

func (*Base[T]) Get

func (val *Base[T]) Get() T

func (*Base[T]) Key

func (val *Base[T]) Key() string

type Dynamic

type Dynamic[T any] struct {
	*Base[T]
}

Dynamic is a dynamic value. Dynamic values register to the Dynamic registry, and respond to changes to watched config files. Their Get() methods will return whatever value is currently live in the config, in a threadsafe manner.

func NewDynamic

func NewDynamic[T any](base *Base[T]) *Dynamic[T]

NewDynamic returns a dynamic value derived from the given base value, after binding it to the dynamic registry and wrapping its GetFunc to be threadsafe with respect to config reloading.

func (*Dynamic[T]) Registry

func (val *Dynamic[T]) Registry() registry.Bindable

func (*Dynamic[T]) Set

func (val *Dynamic[T]) Set(v T)

type Registerable

type Registerable interface {
	Key() string
	Registry() registry.Bindable
	Flag(fs *pflag.FlagSet) (*pflag.Flag, error)
}

Registerable is the subset of the interface exposed by Values (which is declared in the public viperutil package).

We need a separate interface type because Go generics do not let you define a function that takes Value[T] for many, different T's, which we want to do for BindFlags.

type Static

type Static[T any] struct {
	*Base[T]
}

Static is a static value. Static values register to the Static registry, and do not respond to changes to config files. Their Get() method will return the same value for the lifetime of the process.

func NewStatic

func NewStatic[T any](base *Base[T]) *Static[T]

NewStatic returns a static value derived from the given base value, after binding it to the static registry.

func (*Static[T]) Registry

func (val *Static[T]) Registry() registry.Bindable

func (*Static[T]) Set

func (val *Static[T]) Set(v T)

Jump to

Keyboard shortcuts

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