Skip to content
Sergey Vilgelm edited this page May 15, 2023 · 2 revisions

Tips and tricks

Let's collect some more or less obvious tips here.

Customized unmarshaling

If you need to customize unmarshaling look at mapstructure config options and you can set it like that:

// if you need error on unknown fields in your config file
viper.Unmarshal(&f.d, func(config *mapstructure.DecoderConfig) {                                                          
        config.ErrorUnused = true                                                                                                    
    })
Using go text templates

If you need to store go templates in viper configs and then execute them, look at Viper Template library.

Here is an example:

package main

import (
	"fmt"
	"text/template"

	"github.com/spf13/viper"
	vipertemplate "github.com/sv-tools/viper-template"
)

func main() {
	v := viper.New()
	v.Set("foo", `{{ Get "bar" }}`)
	v.Set("bar", `{{ Mul . 2 }}`)

	type Data struct {
		Bar int
	}
	data := Data{
		Bar: 42,
	}

	funcs := template.FuncMap{
		"Mul": func(d *Data, v int) int {
			return d.Bar * v
		},
	}

	val, err := vipertemplate.Get(
		"foo",
		vipertemplate.WithViper(v),
		vipertemplate.WithData(&data),
		vipertemplate.WithFuncs(funcs),
	)
	if err != nil {
		panic(err)
	}

	fmt.Println(val)
	// Output: 84
}
Clone this wiki locally