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

Support recursive required and friends for nested structs #55

Open
tmzane opened this issue Mar 13, 2024 · 2 comments
Open

Support recursive required and friends for nested structs #55

tmzane opened this issue Mar 13, 2024 · 2 comments

Comments

@tmzane
Copy link
Member

tmzane commented Mar 13, 2024

Something like:

type Config struct {
	Foo string `env:"FOO"`
}

var cfg struct {
	Config `env:"APP_,required"`
}

See the conversation from #52 for details.

@tmzane tmzane mentioned this issue Apr 4, 2024
@bunyk
Copy link
Contributor

bunyk commented Apr 4, 2024

I understood that embedding struct with env tag should prefix everything in embedded struct with that tag. So cfg.Foo would get it's value from APP_FOO, this makes total sense.

But I do not completely understand intention and semantics of required. It is for cases when users would want to have all the fields required and would want to just type that once? What would happen if with the following:

type Config struct {
	Foo string `env:"FOO,required"`
	Bar string `env:"BAR"`
	Baz string `env:"BAZ" default:"some value"`
}

var cfg struct {
	Config `env:"APP_,required"`
}

required on the root will make all the fields required and discard default? Or will just make Bar required, and Baz will keep it's default? Or in general, having a path of nested fields like A_B_C_D, if any of the fields has required variable A_B_C_D would be required?

@tmzane
Copy link
Member Author

tmzane commented Apr 5, 2024

It is for cases when users would want to have all the fields required and would want to just type that once?

Yes. Or just a group of variables:

type Config struct {
    // all DB_* variables are required:
    DB struct {
        Host string `env:"DB_HOST"`
        Port string `env:"DB_PORT"`
    } `env:",required"`
    // the rest of the fields...
}

required on the root will make all the fields required and discard default?

Currently, marking a variable as both required and default is an error. I think it should be the same in this case. We could make that a leaf would take precedence over the root, thus, overriding the root's modifier, but it seems poorly readable to me. An experimental PR is needed to see how good it works.

having a path of nested fields like A_B_C_D, if any of the fields has required variable A_B_C_D would be required?

Yes. In general, I'd like it to be a convenience option, so users could specify a modifier just once. Just like prefixing nested structs that you implemented recently.

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

No branches or pull requests

2 participants