config

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2022 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package config is a wrapper around viper that provides a consistent way to access configuration values.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidInput is returned when the input is not a pointer to a struct or a struct.
	ErrInvalidInput = errors.New("input must be a struct or a pointer to a struct")
)

Functions

func Attach

func Attach(config *viper.Viper, input interface{}) error

Attach attaches the config to all fields of the provided struct. The input must be a pointer to a struct. To attach a config to a field, the field must be of attachable interface and be exported.

func Decode

func Decode(input interface{}, output interface{}) error

Decode takes a struct as input end transforms the Value fields into the output struct fields. It uses mapstructure to decode the map into the output struct. It is important to remember that the input field's key should be the same as the `config` tag of the output field. Example:

type config struct {
	ApiKey Value[string]
}
type concreteConfig struct {
	ApiKey string `config:"api_key"`
}
var cfg config = config{
	ApiKey: config.String("api_key"),
}
var concreteCfg concreteConfig
err := Flatten(cfg, &concreteCfg)

func Traverse

func Traverse(input interface{}, f func(AnyValue) error) error

Traverse traverses the input struct recursively and calls the provided function for each AnyValue field. It stops if the function returns an error. Input must be a pointer to a struct or a struct.

Types

type AnyValue

type AnyValue interface {
	Key() string
	IsSet() bool

	GetAny() any
	SetAny(any)
}

AnyValue is a weakly typed Value interface.

type Option

type Option[T any] func(*configValue[T])

func WithDefault

func WithDefault[T any](value T) Option[T]

WithDefault sets the default value for the config value.

func WithFlag

func WithFlag[T any](f *pflag.FlagSet, name string, value T, usage string) Option[T]

WithFlag defines a flag with specified name, default value, and usage string.

func WithFlagP

func WithFlagP[T any](f *pflag.FlagSet, name, shorthand string, value T, usage string) Option[T]

WithFlagP is like WithFlag, but accepts a shorthand letter that can be used after a single dash.

type Value

type Value[T any] interface {
	Key() string
	IsSet() bool
	Get() T
	Set(T)
}

Value is an interface that can be used to get values from a config.

func Float64

func Float64(key string, options ...Option[float64]) Value[float64]

Float64 creates a new config configValue of type float64.

func Int

func Int(key string, options ...Option[int]) Value[int]

Int creates a new config configValue of type int.

func String

func String(key string, options ...Option[string]) Value[string]

String creates a new config configValue of type string.

Directories

Path Synopsis
Package flags provides a wrapper around pflag that provides a consistent way to access configuration values.
Package flags provides a wrapper around pflag that provides a consistent way to access configuration values.

Jump to

Keyboard shortcuts

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