forked from gotify/configor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecode.go
149 lines (127 loc) · 4 KB
/
decode.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package configor
import (
"fmt"
"io/ioutil"
"os"
"reflect"
"strings"
"github.com/BurntSushi/toml"
"github.com/go-yaml/yaml"
"github.com/pkg/errors"
)
// UnmarshalFile attempts to decode the given file as a yaml, toml or json based
// on the file extension. If the extension isn't one of those, it will error.
func UnmarshalFile(config interface{}, file string, errorOnUnmatchedKeys bool) (err error) {
data, err := ioutil.ReadFile(file)
if err != nil {
return err
}
switch {
case strings.HasSuffix(file, ".yaml") || strings.HasSuffix(file, ".yml"):
if errorOnUnmatchedKeys {
return yaml.UnmarshalStrict(data, config)
}
return yaml.Unmarshal(data, config)
case strings.HasSuffix(file, ".toml"):
return unmarshalToml(data, config, errorOnUnmatchedKeys)
case strings.HasSuffix(file, ".json"):
return unmarshalJSON(data, config, errorOnUnmatchedKeys)
default:
return errors.New("not a yaml, json or toml file")
}
}
func unmarshalToml(data []byte, config interface{}, errorOnUnmatchedKeys bool) (err error) {
metadata, err := toml.Decode(string(data), config)
if err == nil && len(metadata.Undecoded()) > 0 && errorOnUnmatchedKeys {
return &UnmatchedTomlKeysError{Keys: metadata.Undecoded()}
}
return err
}
// GetStringTomlKeys returns a string array of the names of the keys that are passed in as args
func GetStringTomlKeys(list []toml.Key) []string {
arr := make([]string, len(list))
for index, key := range list {
arr[index] = key.String()
}
return arr
}
func (configor *Configor) processTags(config interface{}, prefixes ...string) (err error) {
configValue := reflect.Indirect(reflect.ValueOf(config))
// if the field is a pointer, keep dereferencing it until it's a struct
for configValue.Kind() == reflect.Ptr {
configValue = configValue.Elem()
}
if configValue.Kind() != reflect.Struct {
return errors.Errorf("expected struct but got %v", configValue.Kind().String())
}
configType := configValue.Type()
for i := 0; i < configType.NumField(); i++ {
var (
envNames []string
fieldStruct = configType.Field(i)
field = configValue.Field(i)
envName = fieldStruct.Tag.Get("env") // read configuration from shell env
)
if !field.CanAddr() || !field.CanInterface() {
continue
}
if envName == "" {
envNames = append(envNames, strings.Join(append(prefixes, fieldStruct.Name), "_"))
envNames = append(envNames, strings.ToUpper(strings.Join(append(prefixes, fieldStruct.Name), "_")))
} else {
envNames = []string{envName}
}
// Load From Shell ENV
for _, env := range envNames {
if value := os.Getenv(env); value != "" {
if err = yaml.Unmarshal([]byte(value), field.Addr().Interface()); err != nil {
return err
}
break
}
}
if isBlank := reflect.DeepEqual(field.Interface(), reflect.Zero(field.Type()).Interface()); isBlank {
// Set default configuration if blank
if value := fieldStruct.Tag.Get("default"); value != "" {
if err = yaml.Unmarshal([]byte(value), field.Addr().Interface()); err != nil {
return err
}
} else if fieldStruct.Tag.Get("required") == "true" {
// return error if it is required but blank
return errors.New(fieldStruct.Name + " is required, but blank")
}
}
for field.Kind() == reflect.Ptr {
field = field.Elem()
}
if field.Kind() == reflect.Struct {
err = configor.processTags(
field.Addr().Interface(),
prefix(prefixes, &fieldStruct)...,
)
if err != nil {
return err
}
}
if field.Kind() == reflect.Slice {
for i := 0; i < field.Len(); i++ {
if reflect.Indirect(field.Index(i)).Kind() == reflect.Struct {
err = configor.processTags(
field.Index(i).Addr().Interface(),
append(prefix(prefixes, &fieldStruct), fmt.Sprint(i))...,
)
if err != nil {
return err
}
}
}
}
}
return nil
}
func prefix(prefixes []string, fieldStruct *reflect.StructField) []string {
if fieldStruct.Anonymous && fieldStruct.Tag.Get("anonymous") == "true" {
return prefixes
}
return append(prefixes, fieldStruct.Name)
}