Skip to content

Commit

Permalink
fix(config): Fix TOML parsing of compression levels (vectordotdev#18173)
Browse files Browse the repository at this point in the history
TOML parses as as an i64.

Signed-off-by: Jesse Szwedko <jesse.szwedko@datadoghq.com>
  • Loading branch information
jszwedko committed Aug 7, 2023
1 parent 3c535ec commit 8fc574f
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions src/sinks/util/buffer/compression.rs
Expand Up @@ -431,7 +431,21 @@ impl<'de> de::Deserialize<'de> for CompressionLevel {
where
E: de::Error,
{
Ok(CompressionLevel::Val(v as u32))
u32::try_from(v).map(CompressionLevel::Val).map_err(|err| {
de::Error::custom(format!(
"unsigned integer could not be converted to u32: {}",
err
))
})
}

fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E>
where
E: de::Error,
{
u32::try_from(v).map(CompressionLevel::Val).map_err(|err| {
de::Error::custom(format!("integer could not be converted to u32: {}", err))
})
}
}

Expand Down Expand Up @@ -490,7 +504,7 @@ mod test {
use super::{Compression, CompressionLevel};

#[test]
fn deserialization() {
fn deserialization_json() {
let fixtures_valid = [
(r#""none""#, Compression::None),
(r#""gzip""#, Compression::Gzip(CompressionLevel::default())),
Expand Down Expand Up @@ -545,7 +559,7 @@ mod test {
),
(
r#"{"algorithm": "gzip", "level": -1}"#,
r#"invalid type: integer `-1`, expected unsigned number or string at line 1 column 33"#,
r#"integer could not be converted to u32: out of range integral type conversion attempted at line 1 column 33"#,
),
(
r#"{"algorithm": "gzip", "level": "good"}"#,
Expand Down Expand Up @@ -575,6 +589,22 @@ mod test {
}
}

#[test]
fn deserialization_toml() {
let fixtures_valid = [
// TOML differs from YAML and JSON by always parsing integers as signed
(
r#"algorithm = "gzip"
level = 8"#,
Compression::Gzip(CompressionLevel::Val(8)),
),
];
for (sources, result) in fixtures_valid.iter() {
let deserialized: Result<Compression, _> = toml::from_str(sources);
assert_eq!(deserialized.expect("valid source"), *result);
}
}

#[test]
fn from_and_to_value() {
let fixtures_valid = [
Expand Down

0 comments on commit 8fc574f

Please sign in to comment.