-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigparser.go
118 lines (106 loc) · 3.3 KB
/
configparser.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
// Copyright (C) 2018 Sebastian Stauch
//
// This software may be modified and distributed under the terms
// of the MIT license. See the LICENSE file for details.
package configparser
import (
"encoding/json"
"os"
"reflect"
"strconv"
"strings"
"gopkg.in/yaml.v3"
)
func setMemberValue(config interface{}, field reflect.StructField, fieldidx int, value string) {
switch field.Type.Kind().String() {
case "string":
reflect.Indirect(reflect.ValueOf(config)).Field(fieldidx).SetString(value)
case "int":
intval, _ := strconv.Atoi(value)
reflect.Indirect(reflect.ValueOf(config)).Field(fieldidx).SetInt(int64(intval))
case "bool":
boolval := false
if value == "true" {
boolval = true
} else if value == "false" {
boolval = false
}
reflect.Indirect(reflect.ValueOf(config)).Field(fieldidx).SetBool(boolval)
case "float64":
floatvalue, err := strconv.ParseFloat(value, 64)
if err != nil {
return
}
reflect.Indirect(reflect.ValueOf(config)).Field(fieldidx).SetFloat(floatvalue)
case "float32":
floatvalue, err := strconv.ParseFloat(value, 32)
if err != nil {
return
}
reflect.Indirect(reflect.ValueOf(config)).Field(fieldidx).SetFloat(floatvalue)
}
}
// SetValuesFromEnvironmentTag iterates over the struct and checks for "env" tags. If an "env" tag was found,
// it will set the value of the struct member to the value from the specified env var.
func SetValuesFromEnvironmentTag(config interface{}) {
t := reflect.Indirect(reflect.ValueOf(config)).Type()
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
tag := field.Tag.Get("env")
value := os.Getenv(tag)
if len(value) > 0 {
setMemberValue(config, field, i, value)
}
if field.Type.Kind().String() == "struct" {
s := reflect.Indirect(reflect.ValueOf(config)).Field(i)
SetValuesFromEnvironmentTag(s.Addr().Interface())
}
}
}
// SetValuesFromEnvironment iterates over the struct and checks if environment variables were set.
// It will transform the name of the field to upper and join the the prefix with an _.
// The resulting string will be looked up with os.Getenv if found it will set the value of the struct member
// to the value from the env var.
func SetValuesFromEnvironment(prefix string, config interface{}) {
if len(prefix) > 0 {
prefix = prefix + "_"
}
t := reflect.Indirect(reflect.ValueOf(config)).Type()
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
name := strings.ToUpper(field.Name)
checkvar := prefix + name
value := os.Getenv(checkvar)
if len(value) > 0 {
setMemberValue(config, field, i, value)
}
if field.Type.Kind().String() == "struct" {
s := reflect.Indirect(reflect.ValueOf(config)).Field(i)
SetValuesFromEnvironment(checkvar, s.Addr().Interface())
}
}
}
// ParseYaml reads the file at filename and unmarshals the content to config
func ParseYaml(filename string, config interface{}) error {
configbytes, err := os.ReadFile(filename)
if err != nil {
return err
}
err = yaml.Unmarshal(configbytes, config)
if err != nil {
return err
}
return nil
}
// ParseJSON reads the file at filename and unmarshals the content to config
func ParseJSON(filename string, config interface{}) error {
configbytes, err := os.ReadFile(filename)
if err != nil {
return err
}
err = json.Unmarshal(configbytes, config)
if err != nil {
return err
}
return nil
}