-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pointers not being mapped correctly with only ENV variables #6
Comments
Hi, thanks for reporting. I'll try to find some time to check this |
|
@iamolegga, took a stab at a fix on this. Let me know what your thoughts are! |
@jguttman94 as I've said before in your PR, everything looks good, but there is only one bug with |
@iamolegga, I took a look, and I'll be honest, I'm struggling with this one... I believe this to be a separate issue that is more challenging to address. The issue with the unmarshalling of the pointers was easier to solve as the reflection used in the recursive loop over the raw configuration value just needed knowledge of a new pointer element. With that provided, and without mutating the original raw config value, the rest of the recursion loop was able to successfully destructure the environment variable into the pointer. For the maps, it's a little different. It's easy enough to create a new map if the raw config value is nil, but the rest of the logic is dependent on iterating over each provided map value and recursively invoking I may need some extra support on this one, as I'm quite new to golang reflection. Here is some example code I was playing around with to create a new map in case reflect.Map:
if fv.IsNil() {
var mapType = reflect.MapOf(fv.Type().Key(), fv.Type().Elem())
fv = reflect.MakeMap(mapType)
}
iter := fv.MapRange()
for iter.Next() { // failing to destructure here, as the new map is empty, and we do not know the potential contents at this time
if key, ok := iter.Key().Interface().(string); ok {
e.bindEnvs(iter.Value().Interface(), append(prev, tv, key)...)
}
} |
Thanks for the investigation @jguttman94 Let's move forward then and I'll change the test case for only env variables without config file to pass tests. Anyway, I believe this will work for 90% of usage and as you've said we can simply open a new issue for that. |
Just made a release v1.3.0 with basic support of unmarshaling only from env vars, and opened an issue for |
Thanks for the release, @iamolegga. Just verified it works within my cobra CLI! |
Hi,
First thanks for the contribution.
I'm checking this package becase I've experienced the issue with viper, however seems like the example you provide in the documentation does not work as expected when there's no configuration file that fills values for pointers and maps.
If I execute the example without any config file (just adding environment values) the value for
Barries map[string]barry
andQuxxy *quxxy
is allwaysnil
. I have pushed some code in a personal repo to illustrate this issue: https://github.com/afdecastro879/enviper-test If you run the main program you will find that I'm setting the environment variables like this:But when printing the struct the result for the values explained above are
nil
:The text was updated successfully, but these errors were encountered: