Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lang/rust/avro/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,10 @@ pub enum Error {
GetLong(ValueKind),

#[error("Double expected, got {0:?}")]
GetDouble(ValueKind),
GetDouble(Value),

#[error("Float expected, got {0:?}")]
GetFloat(ValueKind),
GetFloat(Value),

#[error("Bytes expected, got {0:?}")]
GetBytes(ValueKind),
Expand Down
60 changes: 48 additions & 12 deletions lang/rust/avro/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -908,11 +908,11 @@ impl Value {
Value::Long(n) => Ok(Value::Float(n as f32)),
Value::Float(x) => Ok(Value::Float(x)),
Value::Double(x) => Ok(Value::Float(x as f32)),
Value::String(x) => match Self::parse_special_float(&x) {
Value::String(ref x) => match Self::parse_special_float(x) {
Some(f) => Ok(Value::Float(f)),
None => Err(Error::GetFloat(ValueKind::String)),
None => Err(Error::GetFloat(self)),
},
other => Err(Error::GetFloat(other.into())),
other => Err(Error::GetFloat(other)),
}
}

Expand All @@ -922,21 +922,21 @@ impl Value {
Value::Long(n) => Ok(Value::Double(n as f64)),
Value::Float(x) => Ok(Value::Double(f64::from(x))),
Value::Double(x) => Ok(Value::Double(x)),
Value::String(x) => match Self::parse_special_float(&x) {
Some(f) => Ok(Value::Double(f.into())),
None => Err(Error::GetDouble(ValueKind::String)),
Value::String(ref x) => match Self::parse_special_float(x) {
Some(f) => Ok(Value::Double(f64::from(f))),
None => Err(Error::GetDouble(self)),
},
other => Err(Error::GetDouble(other.into())),
other => Err(Error::GetDouble(other)),
}
}

/// IEEE 754 NaN and infinities are not valid JSON numbers.
/// So they are represented in JSON as strings.
fn parse_special_float(s: &str) -> Option<f32> {
match s.trim().to_ascii_lowercase().as_str() {
"nan" | "+nan" | "-nan" => Some(f32::NAN),
"inf" | "+inf" | "infinity" | "+infinity" => Some(f32::INFINITY),
"-inf" | "-infinity" => Some(f32::NEG_INFINITY),
fn parse_special_float(value: &str) -> Option<f32> {
match value {
"NaN" => Some(f32::NAN),
"INF" | "Infinity" => Some(f32::INFINITY),
"-INF" | "-Infinity" => Some(f32::NEG_INFINITY),
_ => None,
}
}
Expand Down Expand Up @@ -3138,4 +3138,40 @@ Field with name '"b"' is not a member of the map items"#,
)
);
}

#[test]
fn avro_4024_resolve_double_from_unknown_string_err() -> TestResult {
let schema = Schema::parse_str(r#"{"type": "double"}"#)?;
let value = Value::String("unknown".to_owned());
match value.resolve(&schema) {
Err(err @ Error::GetDouble(_)) => {
assert_eq!(
format!("{err:?}"),
r#"Double expected, got String("unknown")"#
);
}
other => {
panic!("Expected Error::GetDouble, got {other:?}");
}
}
Comment on lines +3146 to +3156
Copy link
Member

@xxchan xxchan Aug 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just FYI: there's a tool that can make testing a lot more happier https://github.com/rust-analyzer/expect-test

Basically it's just like

assert_eq!(format!("{:?}", value.resolve(&schema)), "balabala")

We don't manually match, but just print out the result.

What's cooler is that expect_test can automatically update the actual result for you. (You can see the video demo in that repo)

Ok(())
}

#[test]
fn avro_4024_resolve_float_from_unknown_string_err() -> TestResult {
let schema = Schema::parse_str(r#"{"type": "float"}"#)?;
let value = Value::String("unknown".to_owned());
match value.resolve(&schema) {
Err(err @ Error::GetFloat(_)) => {
assert_eq!(
format!("{err:?}"),
r#"Float expected, got String("unknown")"#
);
}
other => {
panic!("Expected Error::GetFloat, got {other:?}");
}
}
Ok(())
}
}
14 changes: 11 additions & 3 deletions lang/rust/avro/tests/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,22 @@ fn default_value_examples() -> &'static Vec<(&'static str, &'static str, Value)>
(r#""long""#, "5", Value::Long(5)),
(r#""float""#, "1.1", Value::Float(1.1)),
(r#""double""#, "1.1", Value::Double(1.1)),
(r#""float""#, r#"" +inf ""#, Value::Float(f32::INFINITY)),
(r#""float""#, r#""INF""#, Value::Float(f32::INFINITY)),
(r#""double""#, r#""INF""#, Value::Double(f64::INFINITY)),
(r#""float""#, r#""Infinity""#, Value::Float(f32::INFINITY)),
(
r#""float""#,
r#""-Infinity""#,
Value::Float(f32::NEG_INFINITY),
),
(r#""double""#, r#""Infinity""#, Value::Double(f64::INFINITY)),
(
r#""double""#,
r#""-Infinity""#,
Value::Double(f64::NEG_INFINITY),
),
(r#""float""#, r#""-NAN""#, Value::Float(f32::NAN)),
(r#""double""#, r#""-NAN""#, Value::Double(f64::NAN)),
(r#""float""#, r#""NaN""#, Value::Float(f32::NAN)),
(r#""double""#, r#""NaN""#, Value::Double(f64::NAN)),
(
r#"{"type": "fixed", "name": "F", "size": 2}"#,
r#""a""#,
Expand Down