Skip to content
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

attempting to load slice from env leads to errors #49

Open
alexandruionica opened this issue Sep 24, 2019 · 0 comments
Open

attempting to load slice from env leads to errors #49

alexandruionica opened this issue Sep 24, 2019 · 0 comments

Comments

@alexandruionica
Copy link

Hello,

With this commit 7dd23e5#diff-7d1c2a3334601b6c1958aae0a594cba8R240 , it looks lie behaviour for loading slices changed and now if a slice has 0 length in the config file then it is attempted to be loaded from the environment variables. What happens is that if the slice is made up of structs then a new struct is initialised and them attempted to be validate and this will lead to failure if the struct as any required elements which are not set in the environment.

For example, I have a configuration file made up of the below Cfg struct:

type Cfg struct {
	DataDir       string             `required:"true" yaml:"data_dir" json:"data_dir"`
	Email  []ConfigNotificationEmail  `yaml:"email,omitempty" json:"email,omitempty"`
}
type ConfigNotificationEmail struct {
	Server string   `required:"true" yaml:"server" json:"server"`
	User   string   `yaml:"user,omitempty" json:"user,omitempty"`
}

Loading a yaml file into the Cfg struct will now fail because the Email array which is defined as optional (omitempty) and which is made up of elements of type ConfigNotificationEmail fails because the Server element is not defined; the actual error is:

Server is required, but blank

I don't have in my configuration file any definition for a email entry but because now the env vars are looked up and during validation an empty struct of type ConfigNotificationEmail is created and then attempted to be validated, it then leads to failing to load the whole configuration.

Before the mentioned patch was applied, this was not an issue.

P.S. If I do add an email entry in my .yaml config file and this entry has the server key then things work as expected. The problem appears only when there is no email entry.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant