Breaks out functions and adds some unit tests.
This commit is contained in:
parent
bbd6299e53
commit
2ac8e37e17
@ -18,7 +18,7 @@ type structInfo struct {
|
||||
Tags reflect.StructTag
|
||||
Type reflect.Type
|
||||
DefaultValue interface{}
|
||||
Secret interface{}
|
||||
Secret bool
|
||||
}
|
||||
|
||||
func getEnv[t string | bool | int | int64 | float64](env string, def t) (t, error) {
|
||||
@ -77,6 +77,10 @@ func getStructInfo(spec interface{}) ([]structInfo, error) {
|
||||
}
|
||||
typeOfSpec := s.Type()
|
||||
|
||||
return parseStructInfo(s, typeOfSpec)
|
||||
}
|
||||
|
||||
func parseStructInfo(s reflect.Value, typeOfSpec reflect.Type) ([]structInfo, error) {
|
||||
infos := make([]structInfo, 0, s.NumField())
|
||||
for i := 0; i < s.NumField(); i++ {
|
||||
f := s.Field(i)
|
||||
@ -87,17 +91,7 @@ func getStructInfo(spec interface{}) ([]structInfo, error) {
|
||||
continue
|
||||
}
|
||||
|
||||
for f.Kind() == reflect.Pointer {
|
||||
if f.IsNil() {
|
||||
if f.Type().Elem().Kind() != reflect.Struct {
|
||||
break
|
||||
}
|
||||
f.Set(reflect.New(f.Type().Elem()))
|
||||
}
|
||||
f = f.Elem()
|
||||
}
|
||||
|
||||
secret, err := typeConversion(ftype.Type.String(), ftype.Tag.Get("secret"))
|
||||
secret, err := strconv.ParseBool(ftype.Tag.Get("secret"))
|
||||
if err != nil {
|
||||
secret = false
|
||||
}
|
||||
@ -110,31 +104,41 @@ func getStructInfo(spec interface{}) ([]structInfo, error) {
|
||||
}
|
||||
|
||||
info := structInfo{
|
||||
Name: ftype.Name,
|
||||
Alt: strings.ToUpper(ftype.Tag.Get("env")),
|
||||
Info: desc,
|
||||
Key: ftype.Name,
|
||||
DefaultValue: getDefault(ftype),
|
||||
Field: f,
|
||||
Info: desc,
|
||||
Key: getAlt(ftype),
|
||||
Name: ftype.Name,
|
||||
Secret: secret,
|
||||
Tags: ftype.Tag,
|
||||
Type: ftype.Type,
|
||||
Secret: secret,
|
||||
}
|
||||
if info.Alt != "" {
|
||||
info.Key = info.Alt
|
||||
}
|
||||
info.Key = strings.ToUpper(info.Key)
|
||||
if ftype.Tag.Get("default") != "" {
|
||||
v, err := typeConversion(ftype.Type.String(), ftype.Tag.Get("default"))
|
||||
if err != nil {
|
||||
return []structInfo{}, err
|
||||
}
|
||||
info.DefaultValue = v
|
||||
}
|
||||
|
||||
infos = append(infos, info)
|
||||
}
|
||||
|
||||
return infos, nil
|
||||
}
|
||||
|
||||
func getAlt(ftype reflect.StructField) string {
|
||||
if len(ftype.Tag.Get("env")) > 0 {
|
||||
return strings.ToUpper(ftype.Tag.Get("env"))
|
||||
}
|
||||
return strings.ToUpper(ftype.Name)
|
||||
}
|
||||
|
||||
func getDefault(ftype reflect.StructField) interface{} {
|
||||
if ftype.Tag.Get("default") != "" {
|
||||
v, err := typeConversion(ftype.Type.String(), ftype.Tag.Get("Default"))
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return v
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func typeConversion(t, v string) (interface{}, error) {
|
||||
switch t {
|
||||
case "string": //nolint:goconst
|
||||
|
191
internal/config/envconfig_test.go
Normal file
191
internal/config/envconfig_test.go
Normal file
@ -0,0 +1,191 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
type mock_config struct {
|
||||
NoTags string
|
||||
Ignored string `ignored:"true"`
|
||||
Info string `info:"This is an info string."`
|
||||
Secret string `secret:"true"`
|
||||
Env string `env:"test_env"`
|
||||
Default_string string `default:"This is a default string."`
|
||||
Default_bool bool `default:"true"`
|
||||
Default_int int `default:"100"`
|
||||
Default_int64 int64 `default:"100"`
|
||||
Default_float64 float64 `default:"100.001"`
|
||||
}
|
||||
|
||||
func TestGetEnv(t *testing.T) {
|
||||
var (
|
||||
expected_string string = "This is a default string."
|
||||
expected_bool bool = true
|
||||
expected_int int = 100
|
||||
expected_int64 int64 = 100
|
||||
expected_float64 float64 = 100.001
|
||||
expected_unset_default string = "This is a default value."
|
||||
)
|
||||
|
||||
// string
|
||||
t.Setenv("TEST_STRING", expected_string)
|
||||
test_string, err := getEnv("TEST_STRING", "This is a default string.")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expected_string, test_string)
|
||||
|
||||
// bool
|
||||
_, err = getEnv("TEST_STRING", expected_bool)
|
||||
assert.Error(t, err)
|
||||
t.Setenv("TEST_BOOL", strconv.FormatBool(expected_bool))
|
||||
test_bool, err := getEnv("TEST_BOOL", expected_bool)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expected_bool, test_bool)
|
||||
|
||||
// int
|
||||
_, err = getEnv("TEST_STRING", expected_int)
|
||||
assert.Error(t, err)
|
||||
t.Setenv("TEST_INT", strconv.FormatInt(int64(expected_int), 10))
|
||||
test_int, err := getEnv("TEST_INT", expected_int)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expected_int, test_int)
|
||||
|
||||
// int64
|
||||
_, err = getEnv("TEST_STRING", expected_int64)
|
||||
assert.Error(t, err)
|
||||
t.Setenv("TEST_INT64", strconv.FormatInt(expected_int64, 10))
|
||||
test_int64, err := getEnv("TEST_INT", expected_int64)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expected_int64, test_int64)
|
||||
|
||||
// float64
|
||||
_, err = getEnv("TEST_STRING", expected_float64)
|
||||
assert.Error(t, err)
|
||||
t.Setenv("TEST_INT", strconv.FormatFloat(expected_float64, 'f', 3, 64))
|
||||
test_float64, err := getEnv("TEST_INT", expected_float64)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expected_float64, test_float64)
|
||||
|
||||
// unset or missing environment variable
|
||||
test_unset, err := getEnv("TEST_DEFAULT", expected_unset_default)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expected_unset_default, test_unset)
|
||||
}
|
||||
|
||||
func TestGetStructInfo(t *testing.T) {
|
||||
test_config := mock_config{
|
||||
NoTags: "notags",
|
||||
Ignored: "ignored",
|
||||
Secret: "secret",
|
||||
}
|
||||
|
||||
cfgInfo, err := getStructInfo(&test_config)
|
||||
assert.NoError(t, err)
|
||||
|
||||
for _, v := range cfgInfo {
|
||||
switch v.Name {
|
||||
case "Info":
|
||||
assert.Equal(t, "() This is an info string.", v.Info)
|
||||
case "Secret":
|
||||
assert.Equal(t, false, v.Secret)
|
||||
case "Env":
|
||||
assert.Equal(t, "TEST_ENV", v.Alt)
|
||||
case "Default_value":
|
||||
assert.Equal(t, "This is a default string.", v.DefaultValue)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTypeConversion(t *testing.T) {
|
||||
var (
|
||||
expected_string string = "This is a default string."
|
||||
expected_int int = 100
|
||||
expected_int8 int8 = 100
|
||||
expected_int16 int16 = 100
|
||||
expected_int32 int32 = 100
|
||||
expected_int64 int64 = 100
|
||||
expected_uint uint = 100
|
||||
expected_uint16 uint16 = 100
|
||||
expected_uint32 uint32 = 100
|
||||
expected_uint64 uint64 = 100
|
||||
expected_float32 float32 = 100.001
|
||||
expected_float64 float64 = 100.001
|
||||
expected_bool bool = true
|
||||
)
|
||||
|
||||
// string
|
||||
output_string, err := typeConversion("string", expected_string)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expected_string, output_string)
|
||||
|
||||
// int
|
||||
output_int, err := typeConversion("int", strconv.FormatInt(int64(expected_int), 10))
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expected_string, output_int)
|
||||
|
||||
// int8
|
||||
output_int8, err := typeConversion("int8", strconv.FormatInt(int64(expected_int8), 10))
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expected_int8, output_int8)
|
||||
|
||||
// int16
|
||||
output_int16, err := typeConversion("int16", strconv.FormatInt(int64(expected_int16), 10))
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expected_int16, output_int16)
|
||||
|
||||
// int32
|
||||
output_int32, err := typeConversion("int32", strconv.FormatInt(int64(expected_int32), 10))
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expected_int32, output_int32)
|
||||
|
||||
// int64
|
||||
output_int64, err := typeConversion("int64", strconv.FormatInt(expected_int64, 10))
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expected_int64, output_int64)
|
||||
|
||||
// uint
|
||||
output_uint, err := typeConversion("uint", strconv.FormatInt(int64(expected_uint), 10))
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expected_uint, output_uint)
|
||||
|
||||
// uint16
|
||||
output_uint16, err := typeConversion("uint16", strconv.FormatInt(int64(expected_uint16), 10))
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expected_uint16, output_uint16)
|
||||
|
||||
// uint32
|
||||
output_uint32, err := typeConversion("uint32", strconv.FormatInt(int64(expected_uint32), 10))
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expected_uint32, output_uint32)
|
||||
|
||||
// uint64
|
||||
output_uint64, err := typeConversion("uint64", strconv.FormatInt(int64(expected_uint64), 10))
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expected_uint64, output_uint64)
|
||||
|
||||
// float32
|
||||
output_float32, err := typeConversion("float32", strconv.FormatFloat(float64(expected_float32), 'f', 3, 64))
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expected_float32, output_float32)
|
||||
|
||||
// float64
|
||||
output_float64, err := typeConversion("float64", strconv.FormatFloat(expected_float64, 'f', 3, 64))
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expected_float64, output_float64)
|
||||
|
||||
// bool
|
||||
output_bool, err := typeConversion("bool", strconv.FormatBool(expected_bool))
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expected_bool, output_bool)
|
||||
}
|
||||
|
||||
func TestParseFlags(t *testing.T) {
|
||||
test_config := Config{}
|
||||
|
||||
cfgInfo, err := getStructInfo(&test_config)
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.NoError(t, test_config.parseFlags(cfgInfo))
|
||||
}
|
@ -65,7 +65,12 @@ func setLogLevel(cfg *Config) {
|
||||
}
|
||||
|
||||
func printRunningConfig(cfg *Config, cfgInfo []structInfo) {
|
||||
var logRunningConfiguration = "Running Configuration"
|
||||
|
||||
for _, info := range cfgInfo {
|
||||
if info.Secret {
|
||||
cfg.Log.Debug(logRunningConfiguration, info.Name, "REDACTED")
|
||||
} else {
|
||||
switch info.Type.String() {
|
||||
case "string":
|
||||
p := reflect.ValueOf(cfg).Elem().FieldByName(info.Name).Addr().Interface().(*string)
|
||||
@ -79,3 +84,4 @@ func printRunningConfig(cfg *Config, cfgInfo []structInfo) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user