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

Add serialize and deserialize support for DateTime<Utc> and secret #619

Merged
merged 5 commits into from
Jan 5, 2023
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
3 changes: 3 additions & 0 deletions scylla-cql/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ byteorder = "1.3.4"
bytes = "1.0.1"
num_enum = "0.5"
tokio = { version = "1.12", features = ["io-util", "time"] }
secrecy = "0.7.0"
snap = "1.0"
uuid = "1.0"
thiserror = "1.0"
Expand All @@ -31,3 +32,5 @@ criterion = "0.3"
name = "benchmark"
harness = false

[features]
secret = []
42 changes: 41 additions & 1 deletion scylla-cql/src/frame/response/cql_to_rust.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
use super::result::{CqlValue, Row};
use crate::frame::value::Counter;
use bigdecimal::BigDecimal;
use chrono::{Duration, NaiveDate};
use chrono::{DateTime, Duration, NaiveDate, TimeZone, Utc};
use num_bigint::BigInt;
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::hash::Hash;
use std::net::IpAddr;
use thiserror::Error;
use uuid::Uuid;

#[cfg(feature = "secret")]
use secrecy::{Secret, Zeroize};

#[derive(Error, Debug, Clone, PartialEq, Eq)]
pub enum FromRowError {
#[error("{err} in the column with index {column}")]
Expand Down Expand Up @@ -36,6 +39,8 @@ pub enum FromCqlValError {
BadCqlType,
#[error("Value is null")]
ValIsNull,
#[error("Bad Value")]
BadVal,
}

/// This trait defines a way to convert CQL Row into some rust type
Expand Down Expand Up @@ -149,6 +154,23 @@ impl FromCqlVal<CqlValue> for crate::frame::value::Timestamp {
}
}

impl FromCqlVal<CqlValue> for DateTime<Utc> {
fn from_cql(cql_val: CqlValue) -> Result<Self, FromCqlValError> {
let timestamp = cql_val.as_bigint().ok_or(FromCqlValError::BadCqlType)?;
match Utc.timestamp_millis_opt(timestamp) {
chrono::LocalResult::Single(datetime) => Ok(datetime),
_ => Err(FromCqlValError::BadVal),
}
}
}

#[cfg(feature = "secret")]
impl<V: FromCqlVal<CqlValue> + Zeroize> FromCqlVal<CqlValue> for Secret<V> {
fn from_cql(cql_val: CqlValue) -> Result<Self, FromCqlValError> {
Ok(Secret::new(FromCqlVal::from_cql(cql_val)?))
}
}

// Vec<T>::from_cql<CqlValue>
impl<T: FromCqlVal<CqlValue>> FromCqlVal<CqlValue> for Vec<T> {
fn from_cql(cql_val: CqlValue) -> Result<Self, FromCqlValError> {
Expand Down Expand Up @@ -477,6 +499,24 @@ mod tests {
);
}

#[test]
fn datetime_from_cql() {
use chrono::{DateTime, Duration, Utc};
let naivedatetime_utc = NaiveDate::from_ymd_opt(2022, 12, 31)
.unwrap()
.and_hms_opt(2, 0, 0)
.unwrap();
let datetime_utc = DateTime::<Utc>::from_utc(naivedatetime_utc, Utc);

assert_eq!(
datetime_utc,
DateTime::<Utc>::from_cql(CqlValue::Timestamp(Duration::milliseconds(
datetime_utc.timestamp_millis()
)))
.unwrap()
);
}

#[test]
fn uuid_from_cql() {
let test_uuid: Uuid = Uuid::parse_str("8e14e760-7fa8-11eb-bc66-000000000001").unwrap();
Expand Down
18 changes: 18 additions & 0 deletions scylla-cql/src/frame/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ use uuid::Uuid;
use super::response::result::CqlValue;
use super::types::vint_encode;

#[cfg(feature = "secret")]
use secrecy::{ExposeSecret, Secret, Zeroize};

/// Every value being sent in a query must implement this trait
/// serialize() should write the Value as [bytes] to the provided buffer
pub trait Value {
Expand Down Expand Up @@ -380,6 +383,21 @@ impl Value for Time {
}
}

impl Value for DateTime<Utc> {
fn serialize(&self, buf: &mut Vec<u8>) -> Result<(), ValueTooBig> {
buf.put_i32(8);
buf.put_i64(self.timestamp_millis());
Ok(())
}
}

#[cfg(feature = "secret")]
impl<V: Value + Zeroize> Value for Secret<V> {
fn serialize(&self, buf: &mut Vec<u8>) -> Result<(), ValueTooBig> {
self.expose_secret().serialize(buf)
}
}

impl Value for bool {
fn serialize(&self, buf: &mut Vec<u8>) -> Result<(), ValueTooBig> {
buf.put_i32(1);
Expand Down
23 changes: 23 additions & 0 deletions scylla-cql/src/frame/value_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,29 @@ fn timestamp_serialization() {
}
}

#[test]
fn datetime_serialization() {
use chrono::{DateTime, NaiveDateTime, Utc};
// Datetime is milliseconds since unix epoch represented as i64
let max_time: i64 = 24 * 60 * 60 * 1_000_000_000 - 1;

for test_val in &[0, 1, 15, 18463, max_time, max_time + 16] {
let native_datetime = NaiveDateTime::from_timestamp_opt(
*test_val / 1000,
((*test_val % 1000) as i32 * 1_000_000) as u32,
)
.expect("invalid or out-of-range datetime");
let test_datetime = DateTime::<Utc>::from_utc(native_datetime, Utc);
let bytes: Vec<u8> = serialized(test_datetime);

let mut expected_bytes: Vec<u8> = vec![0, 0, 0, 8];
expected_bytes.extend_from_slice(&test_val.to_be_bytes());

assert_eq!(bytes, expected_bytes);
assert_eq!(expected_bytes.len(), 12);
}
}

#[test]
fn timeuuid_serialization() {
// A few random timeuuids generated manually
Expand Down