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

Env Error conversion number to string #42

Closed
Gabriel-Paulucci opened this issue Feb 20, 2022 · 4 comments
Closed

Env Error conversion number to string #42

Gabriel-Paulucci opened this issue Feb 20, 2022 · 4 comments

Comments

@Gabriel-Paulucci
Copy link

Hello, I found a problem in the sending system, where I enter a number, and I expect to receive a string and I end up getting an error, and I believe it was supposed to be a conversion

use figment::{providers::Env, Figment};

#[derive(Deserialize, Debug)]
pub struct Config {
    pub secret: String,
}

impl Config {
    pub fn figment() -> Figment {
        Figment::new().merge(Env::prefixed("APP_").global())
    }
}

Error

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Error { tag: Tag(Global, 1), profile: Some(Profile(Uncased { string: "global" })), metadata: Some(Metadata { name: "`APP_` environment variable(s)", source: None, provide_location: Some(Location { file: "src\\config.rs", line: 10, col: 24 }), interpolater:  }), path: ["secret"], kind: InvalidType(Unsigned(123), "a string"), prev: None }', src\main.rs:9:54
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Reproduction: https://github.com/Gabriel-Paulucci/FigmentTest

@SergioBenitez
Copy link
Owner

Sorry, there aren't enough details in your issue to understand what you think should be happening vs. what's happening. Can you post a complete program, how you're executing it, and what input you're providing?

@Gabriel-Paulucci
Copy link
Author

Well I had already put a way to reproduce this error but I will make another code, explaining better, but in a simple way it is, if my code is expecting a string and I put a number and that gives an error

@Gabriel-Paulucci
Copy link
Author

@SergioBenitez
Copy link
Owner

Ah, got it. I missed your link in your initial commit, actually. My apologies.

The syntax for values from environment variables is discussed in detail in the Env docs. You'll see that FOO=123 is serialized as an integer, not a string. For a string you'd need to use quotes, which might be FOO=\"123\", depending on your terminal. If you want to allow other kinds of values to becomes strings, implement a custom deserializer for String using serde's #[serde(deserialize_with = "path")].

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