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

feat: support bools in DataType.as_f64 and DataType.as_i64 variants #399

Merged
merged 2 commits into from
Feb 6, 2024
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
2 changes: 2 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

- refactor: rename `DataType` enum to `Data` and `DataTypeRef` to `DataRef`
- feat: introduce a `DataType` trait implemented by both `Data` and `DataRef`.
- feat: `Data` and `DataType` now return `Some(0{.0})` and `Some(1{.0})` rather than `None` when `.as_i64` or `.as_f64`
is used on a Bool value

## 0.23.1

Expand Down
20 changes: 20 additions & 0 deletions src/datatype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ impl DataType for Data {
match self {
Data::Int(v) => Some(*v),
Data::Float(v) => Some(*v as i64),
Data::Bool(v) => Some(*v as i64),
Data::String(v) => v.parse::<i64>().ok(),
_ => None,
}
Expand All @@ -151,6 +152,7 @@ impl DataType for Data {
match self {
Data::Int(v) => Some(*v as f64),
Data::Float(v) => Some(*v),
Data::Bool(v) => Some((*v as i32).into()),
Data::String(v) => v.parse::<f64>().ok(),
_ => None,
}
Expand Down Expand Up @@ -450,6 +452,7 @@ impl DataType for DataRef<'_> {
match self {
DataRef::Int(v) => Some(*v),
DataRef::Float(v) => Some(*v as i64),
DataRef::Bool(v) => Some(*v as i64),
DataRef::String(v) => v.parse::<i64>().ok(),
DataRef::SharedString(v) => v.parse::<i64>().ok(),
_ => None,
Expand All @@ -460,6 +463,7 @@ impl DataType for DataRef<'_> {
match self {
DataRef::Int(v) => Some(*v as f64),
DataRef::Float(v) => Some(*v),
DataRef::Bool(v) => Some((*v as i32).into()),
DataRef::String(v) => v.parse::<f64>().ok(),
DataRef::SharedString(v) => v.parse::<f64>().ok(),
_ => None,
Expand Down Expand Up @@ -803,4 +807,20 @@ mod tests {
assert_eq!(Data::Bool(true), true);
assert_eq!(Data::Int(100), 100i64);
}

#[test]
fn test_as_i64_with_bools() {
assert_eq!(Data::Bool(true).as_i64(), Some(1));
assert_eq!(Data::Bool(false).as_i64(), Some(0));
assert_eq!(DataRef::Bool(true).as_i64(), Some(1));
assert_eq!(DataRef::Bool(false).as_i64(), Some(0));
}

#[test]
fn test_as_f64_with_bools() {
assert_eq!(Data::Bool(true).as_f64(), Some(1.0));
assert_eq!(Data::Bool(false).as_f64(), Some(0.0));
assert_eq!(DataRef::Bool(true).as_f64(), Some(1.0));
assert_eq!(DataRef::Bool(false).as_f64(), Some(0.0));
}
}