Documentation ¶
Overview ¶
Package viper provides a xload.Loader implementation that uses github.com/spf13/viper.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AutoEnv ¶
type AutoEnv bool
AutoEnv allows enabling/disabling automatic environment variable loading.
Example (Disable) ¶
package main import ( "github.com/gojekfarm/xtools/xload/providers/viper" ) func main() { _, err := viper.New(viper.AutoEnv(false)) if err != nil { panic(err) } }
Output:
type ConfigPaths ¶
type ConfigPaths []string
ConfigPaths allows specifying the config file search paths.
type Loader ¶
type Loader struct {
// contains filtered or unexported fields
}
Loader is a xload.Loader implementation that uses viper.
func New ¶
New creates a new Loader instance.
Example ¶
package main import ( "context" "fmt" vpr "github.com/spf13/viper" "github.com/gojekfarm/xtools/xload" "github.com/gojekfarm/xtools/xload/providers/viper" ) type Config struct { Log LogConfig `env:",prefix=LOG_"` } type LogConfig struct { Level string `env:"LEVEL"` } func main() { v := vpr.New() v.Set("LOG_LEVEL", "debug") vl, err := viper.New(viper.From(v)) if err != nil { panic(err) } cfg := &Config{} if err := xload.Load(context.Background(), cfg, xload.WithLoader(vl)); err != nil { panic(err) } fmt.Println(cfg.Log.Level) }
Output: debug
Example (FileOptions) ¶
package main import ( "github.com/gojekfarm/xtools/xload/providers/viper" ) func main() { _, err := viper.New( viper.ConfigName("config"), viper.ConfigType("toml"), viper.ConfigPaths([]string{"./", "/etc/<program/"}), ) if err != nil { panic(err) } }
Output:
func (*Loader) ConfigFileUsed ¶
ConfigFileUsed returns the config file used, if any.
Example ¶
package main import ( "fmt" vpr "github.com/spf13/viper" "github.com/gojekfarm/xtools/xload/providers/viper" ) func main() { _, err := viper.New(viper.ConfigFile("<path-to-config-file>.ext")) if err != nil { panic(err) } fmt.Println(vpr.ConfigFileUsed()) // <path-to-config-file>.ext }
Output:
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
Option allows configuring the Loader.
type Transformer ¶
Transformer allows specifying a custom transformer function.
Example ¶
package main import ( vpr "github.com/spf13/viper" "github.com/gojekfarm/xtools/xload" "github.com/gojekfarm/xtools/xload/providers/viper" ) func main() { _, err := viper.New(viper.Transformer(func(v *vpr.Viper, next xload.Loader) xload.Loader { return xload.PrefixLoader("ENV_", next) })) if err != nil { panic(err) } }
Output:
type ValueMapper ¶
ValueMapper allows specifying a custom value mapper function that will be used to flatten the config for xload.Loader from Viper.
Example ¶
package main import ( "github.com/gojekfarm/xtools/xload" "github.com/gojekfarm/xtools/xload/providers/viper" ) func main() { _, err := viper.New(viper.ValueMapper(func(m map[string]any) map[string]string { return xload.FlattenMap(m, "__") })) if err != nil { panic(err) } }
Output:
Click to show internal directories.
Click to hide internal directories.