aboutsummaryrefslogtreecommitdiff
path: root/envcfg.go
blob: b91ea134d7db1ae0d7afe2313f8a3cf1c76021b8 (plain)
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
// Package envcfg provides environment variable mapping to structs.
//
// Can be used to read configuration parameters from the environment.
//
// Fields for which environment variables can be found are overwritten, otherwise they are left to their previous
// value.
//
// Can be used, for example, after gcfg to override settings provided in a configuration file.
package envcfg

import (
	"errors"
	"reflect"
)

const (
	TAG     = "env"
	ABS_TAG = "absenv"
	SEP     = "_"
)

type node struct {
	parent     *node
	value      *reflect.Value
	properties *reflect.StructField
}

var ErrInvalidConfigStruct = errors.New("invalid parameter: must map to a struct")

func ReadInto(cfgStruct interface{}) (interface{}, []error) {
	s := reflect.ValueOf(cfgStruct).Elem()

	if s.Kind() != reflect.Struct {
		return nil, []error{ErrInvalidConfigStruct}
	}

	_, errs := setStructFields(node{nil, &s, nil})

	return cfgStruct, errs
}