Skip to content

Commit

Permalink
Merge pull request #399 from lukapeschke/support-bools-as_i64-as_f64
Browse files Browse the repository at this point in the history
feat: support bools in DataType.as_f64 and DataType.as_i64 variants
  • Loading branch information
tafia authored Feb 6, 2024
2 parents 53a75f2 + 440b6a8 commit 7aa2086
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
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));
}
}

0 comments on commit 7aa2086

Please sign in to comment.