viper

package module
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2024 License: MIT Imports: 5 Imported by: 0

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 ConfigFile

type ConfigFile string

ConfigFile allows specifying the config file to be used.

type ConfigName

type ConfigName string

ConfigName allows specifying the config file name.

type ConfigPaths

type ConfigPaths []string

ConfigPaths allows specifying the config file search paths.

type ConfigType

type ConfigType string

ConfigType allows specifying the config file type.

type Loader

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

Loader is a xload.Loader implementation that uses viper.

func New

func New(options ...Option) (*Loader, error)

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

func (l *Loader) ConfigFileUsed() string

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:

func (*Loader) Load

func (l *Loader) Load(ctx context.Context, key string) (string, error)

Load returns the value for the given key.

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option allows configuring the Loader.

func From

func From(v *viper.Viper) Option

From allows passing a pre-configured Viper instance.

Example
package main

import (
	vpr "github.com/spf13/viper"

	"github.com/gojekfarm/xtools/xload/providers/viper"
)

func main() {
	_, err := viper.New(viper.From(vpr.New()))
	if err != nil {
		panic(err)
	}
}
Output:

type Transformer

type Transformer func(v *viper.Viper, next xload.Loader) xload.Loader

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

type ValueMapper func(map[string]any) map[string]string

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:

Jump to

Keyboard shortcuts

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