This repository has been archived by the owner on Aug 6, 2020. It is now read-only.
forked from iamolegga/enviper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenviper.go
104 lines (97 loc) · 2.8 KB
/
enviper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Package enviper is a helper/wrapper for http://github.com/spf13/viper with the same API.
// It makes it possible to unmarshal config to struct
// considering environment variables.
//
// Problem
//
// Viper package (https://github.com/spf13/viper) doesn't consider environment variables while unmarshaling.
// Please, see:
// https://github.com/spf13/viper/issues/188
// and
// https://github.com/spf13/viper/issues/761
//
// Solution
//
// Just wrap viper instance and use the same `Unmarshal` method as you did before:
//
// e := enviper.New(viper.New())
// e.Unmarshal(&config)
//
// Credits
//
// Thanks to https://github.com/krak3n (https://github.com/spf13/viper/issues/188#issuecomment-399884438)
// and
// https://github.com/celian-garcia (https://github.com/spf13/viper/issues/761#issuecomment-626122696)
// for inspiring.
package enviper
import (
"reflect"
"strings"
"github.com/spf13/viper"
)
// Enviper is a wrapper struct for viper,
// that makes it possible to unmarshal config to struct
// considering environment variables
type Enviper struct {
*viper.Viper
}
// New returns an initialized Enviper instance
func New(v *viper.Viper) *Enviper {
return &Enviper{v}
}
// Unmarshal unmarshals the config into a Struct just like viper does.
// The difference between enviper and viper is in automatic overriding data from file by data from env variables
func (e *Enviper) Unmarshal(rawVal interface{}) error {
if err := e.Viper.ReadInConfig(); err != nil {
switch err.(type) {
case viper.ConfigFileNotFoundError:
// do nothing
default:
return err
}
}
// We need to unmarshal before the env binding to make sure that keys of maps are bound just like the struct fields
// We silence errors here because we'll unmarshal a second time
_ = e.Viper.Unmarshal(rawVal)
e.readEnvs(rawVal)
return e.Viper.Unmarshal(rawVal)
}
func (e *Enviper) readEnvs(rawVal interface{}) {
e.Viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
e.bindEnvs(rawVal)
}
func (e *Enviper) bindEnvs(in interface{}, prev ...string) {
ifv := reflect.ValueOf(in)
if ifv.Kind() == reflect.Ptr {
ifv = ifv.Elem()
}
for i := 0; i < ifv.NumField(); i++ {
fv := ifv.Field(i)
t := ifv.Type().Field(i)
tv, ok := t.Tag.Lookup("mapstructure")
if ok {
if tv == ",squash" {
e.bindEnvs(fv.Interface(), prev...)
continue
}
} else {
tv = t.Name
}
switch fv.Kind() {
case reflect.Struct:
e.bindEnvs(fv.Interface(), append(prev, tv)...)
case reflect.Map:
iter := fv.MapRange()
for iter.Next() {
if key, ok := iter.Key().Interface().(string); ok {
e.bindEnvs(iter.Value().Interface(), append(prev, tv, key)...)
}
}
default:
env := strings.Join(append(prev, tv), ".")
// Viper.BindEnv will never return error
// because env is always non empty string
_ = e.Viper.BindEnv(env)
}
}
}