kellpossible/toml-env

Integer Strings Parsed as Float in Environment Variables

Open

#1 opened on Jun 21, 2024

 (1 comment) (0 reactions) (0 assignees)Rust (2 forks)auto 404
buggood first issuehelp wanted

Repository metrics

Stars
 (7 stars)
PR merge metrics
 (PR metrics pending)

Description

Description: The toml-env library appears to be parsing integer strings into floats, which is not the intended usage. This issue occurs when environment variables are used to override configuration values.

Steps to Reproduce:

  1. Define a configuration with environment variable mapping using the toml-env library.
  2. Set an environment variable with an integer string value.
  3. Run the application.

Example Code:

let config: Option<ConfigFileResponse> = initialize(Args {
    auto_map_env: Some(AutoMapEnvArgs {
        divider: "__",
        prefix: Some("TAGOIO"), // Prefix for environment variables
        transform: Box::new(|name| name.to_lowercase()),
    }),
    // logging: Logging::StdOut,
    config_path: Some(&config_path),
    ..Args::default()
})
.unwrap_or_else(|err| {
    log::error!(target: "error", "Failed to initialize configuration: {}", err);
    std::process::exit(1);
});

Error Message: When running the following command:

TAGOIO__RELAY__DOWNLINK_PORT="1883" cargo run start 

The following error is triggered:

[2024-06-21T19:06:21Z ERROR error] Failed to initialize configuration: Error merging configuration environment variables into config TOML file "./config.toml": Incompatible types at path "$.relay.downlink_port", expected "string" received "float".

Expected Behavior: The environment variable TAGOIO__RELAY__DOWNLINK_PORT should be parsed as an integer string and not converted to a float.

Actual Behavior: The environment variable TAGOIO__RELAY__DOWNLINK_PORT is being parsed as a float, causing a type mismatch error.

Additional Context:

  • toml-env library version: 1.2.0
  • Operating System: macos-sonoma

Proposed Solution: Ensure that integer strings in environment variables are parsed correctly as integers and not converted to floats.

Contributor guide