viperfix

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 3, 2021 License: MIT Imports: 3 Imported by: 2

README

Viperfix

What is Viperfix?

Viperfix is a small tool with an aproach to fix the problems of Viper getting multiple values at once with (env)overrides and defaults.

Why Viperfix?

I like the idea of Viper. But as I started to get blocks of config into my structs, i noticed a big problem for me. It only uses my config-file and ignores defaults and overrides from env, which I need to work with docker.

Example

defaults

viper.SetDefault("log.filename", "logs/latest.log")
viper.SetDefault("log.compress", true)
viper.SetDefault("log.level", "info")
viper.SetDefault("log.max.size", 50)
viper.SetDefault("log.max.backups", 5)
viper.SetDefault("log.max.age", 31)

config.yml

log:
  filename: logs/latest.log
  compress: true
  level: info
  max:
    size: 50
    backups: 5

expected from viper.GetStringMap("log")

map[compress:true filename:logs/latest.log level:info max:map[age:31 backups:5 size:50]]

but its actual

map[compress:true filename:logs/latest.log level:info max:map[backups:5 size:50]]

There are a lot of issues at Viper about Sub(), GetStringMap and Unmarshal() but I didn't find a real solution.

How to use

First, there is a function to set the key delimter (didn't found a way to get it from viper).

func KeyDelimiter(delimiter string)

And for now, there are 3 Functions.

func GetStringMap(key string) map[string]interface{}
func Sub(key string) *viper.Viper
func UnmarshalKey(key string, rawVal interface{}, opts ...viper.DecoderConfigOption) error

They are like the viper functions with equal name but use all sources hirachical to get the values.

They use the default viper singleton instance but there are also versions of each function which takes a viper instance to work with.

func GetStringMap(key string) map[string]interface{}

func GetStringMapFrom(v *viper.Viper, key string) map[string]interface{}
Example

for example

viper.SetDefault("log.max.size", 50)
viper.SetDefault("log.max.backups", 5)
viper.SetDefault("log.max.age", 31)

viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.AutomaticEnv()
viper.SetTypeByDefaultValue(true)

os.Setenv("LOG.MAX.AGE", "15")

fmt.Println(viperfix.GetStringMap("log.max"))

prints

map[age:31 backups:5 size:50]

Documentation

Overview

Package viperfix provides functions to fix the missing of hirachical getting data when getting multiple values

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetStringMap

func GetStringMap(key string) map[string]interface{}

GetStringMap returns the value associated with the key as a map of interfaces.

func GetStringMapFrom

func GetStringMapFrom(v *viper.Viper, key string) map[string]interface{}

GetStringMapFrom uses given viperinstance and returns the value associated with the key as a map of interfaces.

func KeyDelimiter

func KeyDelimiter(delimiter string)

KeyDelimiter sets the Delimiter for keys

func Sub

func Sub(key string) *viper.Viper

Sub returns new Viper instance representing a sub tree of this instance. Sub is case-insensitive for a key.

func SubFrom

func SubFrom(v *viper.Viper, key string) *viper.Viper

SubFrom uses given viperinstance and returns new Viper instance representing a sub tree of this instance. Sub is case-insensitive for a key.

func UnmarshalKey

func UnmarshalKey(key string, rawVal interface{}, opts ...viper.DecoderConfigOption) error

UnmarshalKey takes a single key to unmarshals its values into a Struct.

func UnmarshalKeyFrom

func UnmarshalKeyFrom(v *viper.Viper, key string, rawVal interface{}, opts ...viper.DecoderConfigOption) error

UnmarshalKeyFrom uses given viperinstance and takes a single key to unmarshals its values into a Struct.

Types

This section is empty.

Jump to

Keyboard shortcuts

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