You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Using reflect.VisibleFields instead of looping over NumField should fix the issue with embedded structs.
func getFromEnvVariables(configuration interface{}) {
typ := reflect.TypeOf(configuration)
// if a pointer to a struct is passed, get the type of the dereferenced object
if typ.Kind() == reflect.Ptr {
typ = typ.Elem()
}
for _, field := range reflect.VisibleFields(typ) {
if !field.Anonymous {
// check if we've got a field name override for the environment
tagContent := field.Tag.Get(envTagName)
value := ""
if len(tagContent) > 0 {
value = os.Getenv(tagContent)
} else {
value = os.Getenv(field.Name)
}
if len(value) > 0 {
s := reflect.ValueOf(configuration).Elem()
if s.Kind() == reflect.Struct {
// exported field
f := s.FieldByName(field.Name)
setValue(f, value)
}
}
}
}
}
type MyConf struct
{
Nested struct{
NestedField string
env:"THIS_DOESNT_WORK"
}
}
The nested field does not get set by the environment variable
The text was updated successfully, but these errors were encountered: