Values are not loaded from environment with SetDefaultsForZeroValuesOnly
- Dominant language
- Go
- Stars
- 6.3k
- Forks
- 284
- Avg merge
- 1d 17h
- Merged PRs (30d)
- 1
Description
The change introduced in 2be9960e75cc5f053dcb89c64e43de19765fd154 causes values to no longer load from the environment in the presence of `SetDefaultsForZeroValuesOnly: true` and a non-zero value for the corresponding field.
The following is a quick example to recreate the problem:
``` go
type Config struct {
Username string `env:"USERNAME" envDefault:"admin"`
}
cfg := Config{Username: "root"}
err := env.ParseWithOptions(&cfg, env.Options{
Environment: map[string]string{"USERNAME": "user1"},
SetDefaultsForZeroValuesOnly: true,
})
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("%+v", cfg) // prints `{Username:root}`, but should print `{Username:user1}`
```
The relevant logic is here:
https://github.com/caarlos0/env/blob/56a09d295d9321b1f3b537fd23df1527011cd83d/env.go#L510-L512
It does not check to ensure that `value` was loaded from `envDefault` rather than from the actual environment.
Thank you for the great library!
Contributor guide
No contributing guide indexed for this repository
Research direction
Start with the ParseWithOptions path in env.go around lines 510-512 and reproduce the provided example using SetDefaultsForZeroValuesOnly. Trace whether the value came from envDefault or the environment; done means the environment value is loaded despite the pre-existing field value, with the existing Go tests passing.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100