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

Set but empty Environment Variables don't raise an error #118

Closed
YoungOak opened this issue Mar 17, 2023 · 1 comment
Closed

Set but empty Environment Variables don't raise an error #118

YoungOak opened this issue Mar 17, 2023 · 1 comment

Comments

@YoungOak
Copy link

YoungOak commented Mar 17, 2023

Found that even if my struct has the env-required or env-default set for the fields, if the environment variable was set but left empty, no error is rised, default value is not set either if used. Eg:

Lets say this var was set like this:
export MY_VAR=""

If my struct were:

type MyVars struct {
    MyVar string `env:"MY_VAR" env-required:"true"`
}

No error is raised at all, and i get and empty struct. If otherwise my struct had a default:

type MyVars struct {
    MyVar string `env:"MY_VAR" env-default:"something"`
}

Id get an empty struct again rather than one with the default value.

Is this an expected behavior or could it be improved on?

@YoungOak YoungOak changed the title Set but empty Environment Variables dont rise an error Set but empty Environment Variables don't raise an error Mar 17, 2023
@saddit
Copy link
Contributor

saddit commented Mar 29, 2023

It's an expected behavior. When reading variables from env, the program determines whether a value exists by determining whether the pointer is nil.

var envVal *string

if rawVal, ok := env.Lookup("foo"); ok {
    envVal = &rawVal
}

@YoungOak YoungOak closed this as not planned Won't fix, can't repro, duplicate, stale May 1, 2024
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

2 participants