config

package
v0.4.4 Latest Latest
Warning

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

Go to latest
Published: May 1, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package config provides a useful wrapper for the popular spf13/viper package.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LanguageType

type LanguageType string

LanguageType configures the expected language the source is encoded in.

const (
	YAML LanguageType = "yaml"
	JSON LanguageType = "json"
	TOML LanguageType = "toml"
)

type Manager

type Manager struct {
	*viper.Viper
}

Manager stores config values and provides helpers for bridging the raw config values into Go types.

func Merge

func Merge(m Manager, r io.Reader, opts ...ReadOption) (Manager, error)

Merge allows you to merge another config into an already existing one.

Example
base := strings.NewReader(`hello: world`)
m, err := Read(base, Language(YAML))
if err != nil {
	fmt.Println(err)
	return
}

r := strings.NewReader(`good: bye`)
m, err = Merge(m, r, Language(YAML))
if err != nil {
	fmt.Println(err)
	return
}

fmt.Println(m.GetString("hello"))
fmt.Println(m.GetString("good"))
Output:

world
bye
Example (Overwrite)
base := strings.NewReader(`hello: world`)
m, err := Read(base, Language(YAML))
if err != nil {
	fmt.Println(err)
	return
}

r := strings.NewReader(`hello: bye`)
m, err = Merge(m, r, Language(YAML))
if err != nil {
	fmt.Println(err)
	return
}

fmt.Println(m.GetString("hello"))
Output:

bye
Example (WithZeroValueBaseManager)
r := strings.NewReader(`hello: world`)

var cfg Manager
m, err := Merge(cfg, r, Language(YAML))
if err != nil {
	fmt.Println(err)
	return
}

fmt.Println(m.GetString("hello"))
Output:

world

func Read

func Read(r io.Reader, opts ...ReadOption) (Manager, error)

Read parses the data from r and stores the config values in the returned Manager.

Example (Json)
r := strings.NewReader(`{"hello": "world"}`)

m, err := Read(r, Language(JSON))
if err != nil {
	fmt.Println(err)
	return
}

fmt.Println(m.GetString("hello"))
Output:

world
Example (Toml)
r := strings.NewReader(`hello = "world"`)

m, err := Read(r, Language(TOML))
if err != nil {
	fmt.Println(err)
	return
}

fmt.Println(m.GetString("hello"))
Output:

world
Example (Yaml)
r := strings.NewReader(`hello: world`)

m, err := Read(r, Language(YAML))
if err != nil {
	fmt.Println(err)
	return
}

fmt.Println(m.GetString("hello"))
Output:

world

func (Manager) Unmarshal

func (m Manager) Unmarshal(v interface{}) error

Unmarshal unmarshals the config into the value pointed to by v.

Example
r := strings.NewReader(`hello: world
duration: 10s
n: 2
f: 3.14`)

m, err := Read(r, Language(YAML))
if err != nil {
	fmt.Println(err)
	return
}

var cfg struct {
	Hello    string        `config:"hello"`
	Duration time.Duration `config:"duration"`
	N        int           `config:"n"`
	F        float64       `config:"f"`
}
err = m.Unmarshal(&cfg)
if err != nil {
	fmt.Println(err)
	return
}
fmt.Println(cfg.Hello)
fmt.Println(cfg.Duration)
fmt.Println(cfg.N)
fmt.Println(cfg.F)
Output:

world
10s
2
3.14

type ReadOption

type ReadOption func(*reader)

ReadOption configures different properties of the reader.

func Language

func Language(lang LanguageType) ReadOption

Language sets which language the config source uses.

func TemplateFunc added in v0.3.0

func TemplateFunc(name string, f any) ReadOption

TemplateFunc allows you to register template functions which can be used in the config source template.

Directories

Path Synopsis
Package configtmpl provides template functions for ue in config source templates.
Package configtmpl provides template functions for ue in config source templates.

Jump to

Keyboard shortcuts

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