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
Cannot get config-rs to read env variables #391
Comments
|
Hi! Thanks for reporting this! Please have a look at the code in #392 - maybe I am missing something, because that example works for me (assuming you use config-rs from master). The only difference I spot is that you're using the |
|
FWIW, I also cannot get this working with 0.13.2 and nested structs in my config. When I use However, when I use
Something about |
Your example is exactly what I have, but it does not work for me. As for |
|
Any chance you can share the original code? Maybe a link to a git repository? So I can investigate a bit better? |
|
@matthiasbeyer Sure thing, sent an invitation for: https://github.com/brsnik/actix-config-rs |
|
Okay so, it works in lowercase |
|
meh. All this is really inconvenient. I hope I can make this better in the future with the rewrite I am planning. |
|
Here is the problem: https://github.com/mehcode/config-rs/blob/master/src/env.rs#L222 // If separator is given replace with `.`
if !separator.is_empty() {
key = key.replace(separator, ".");
}If I have a struct in my config with a snake-case field (e.g. The (bad) solution that immediately springs to mind is to only replace the first occurrence of the separator with a dot using
Hard saying what a good solution would be for this, as I can't think of a way for this crate to know for sure if the environment variable |
|
I honestly fail to see the need for the separator whatsoever. Keys like |
|
This crate allows you to parse a config into an actual struct using My use case is ultimately setting values in my config struct using environment variables, but this gets messy in the cases I mentioned above due to some ambiguous combinations of dots and underscores. |
|
This is something I'm running into now myself and I'm having to resort to using In this vein, though probably not possible, if we could get config to know what kind of env to fill a field with in maybe some sort of more custom deserialization, that'd could allow people to specify the full env string a field could match to. (Originally published at: https://jacky.wtf/2022/11/Uqih) |
That is how the popular Go module That said, Go's syntax works a little better for this, in my opinion, since it's all on one line, vs having an additional |
|
What if for |
|
It will work as expected if used like this: then |
|
@nahuakang Great idea. A little weird at first, but it is a perfectly good solution to the problem @ivan-mudrak and @ianling mention above with snake_case struct field names. I'll take that over the serde field name aliasing approach. |
|
It would be good to surface this in the docs, leaving a comment here so I remember to come back later and do this. I just spent half an hour running a debugger getting to the bottom of this, only to realize that fundamentally It may also be good to default the separator to |
Getting the following error when trying to get an environmental variable from config-rs:
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: configuration property "DEV_SERVER_ADDRESS" not found', src/app/config.rs:14:27 note: run with `RUST_BACKTRACE=1` environment variable to display a backtraceENV variable:
Trying to get the variable in main:
What am I doing wrong?
The text was updated successfully, but these errors were encountered: